Assume the following history exists and the current branch is “master”.
1 | A---B---C topic |
From this point, the result of either of the following commands:
1 | git rebase master |
1 | A'--B'--C' topic |
NOTE:
git rebase master topic == git checkout topic & git rebase master.
When rebase exists,topic
will remain the checkout-out branch
Example
- Create a
A.txt
file inmaster
branch - Create and change to branch
test
and create a new fileB.txt
. - Back to branch
master
and then create a new fileC.txt
1 | master: A---C |
The result of following command:(now we are in branch master
)1
2git rebase test master
git rebase test
would be:(we still in branch master
)1
2
3
4
5 master
|
A---C---B
|
test
It works by going to the commen ancestor of the two branches(here is A
), getting the diff introduced by each commit of the branch you’re on(here branch test
), saving those diffs to temporary files, resetting the current branch to the same commit as the branch you are rebasing onto(branch master
, set the HEAD to commit C
), then applying each change in turn. Finally go back to branch master
and do a fast-forward merge
NOTE
Since there exists not diffs, commit
B
is simply appended to commitC