Rebase는 커밋을 새로운 부모 커밋으로 옮기는 기능을 제공한다.

(경고) Push된 history를 rebase 하지 않도록 주의한다. 이 경우 history를 기반으로 작업하고있는 개발자들에게 영향을 주게된다.

다음 명령은 현재 working branch의 커밋을 <base> branch로 옮긴다.

git rebase <base>

git rebase의 기본 동작은 non-interactive이며, interactive mode (--interactive, -i)로 editor를 이용할 수 있다.

git rebase -i new-base-branch

editor 화면:

pick 2231360 some old commit
pick ee2adc2 Adds new feature
# Rebase 2cf755d..ee2adc2 onto 2cf755d (9 commands)
#
# Commands:
# p, pick = use commit (커밋 사용)
# r, reword = use commit, but edit the commit message (커밋 메시지 변경)
# e, edit = use commit, but stop for amending (커밋 수정을 위해 자동 수행 멈춤)
# s, squash = use commit, but meld into previous commit (이전 커밋에 포함)
# f, fixup = like "squash", but discard this commit's log message (이전 커밋에 포함)
# x, exec = run command (the rest of the line) using shell (명령 수행)
# d, drop = remove commit (커밋 삭제)

editor를 종료하면 선택한 명령이 일괄 수행된다.

--on-to 옵션은 브랜치을 rebase 하는 기능을 제공한다.

git rebase --onto <newbase> <oldbase>

featureB branch는 featureA와 의존성의 없어서 master로 rebase하고 싶은 경우,

   o---o---o---o---o master
        \
         o---o---o---o---o featureA
              \
               o---o---o featureB


git rebase --onto master featureA featureB

rebase 결과:

                      o---o---o featureB
                     /
    o---o---o---o---o master
     \
      o---o---o---o---o featureA