git rebase example

Assume the following history exists and the current branch is “master”.

1
2
3
      A---B---C topic
/
D---E---F---G master

From this point, the result of either of the following commands:

1
2
git rebase master
git rebase master topic

would be:

1
2
3
              A'--B'--C' topic
/
D---E---F---G master

NOTE:

git rebase master topic == git checkout topic & git rebase master.
When rebase exists, topic will remain the checkout-out branch

Example

  1. Create a A.txt file in master branch
  2. Create and change to branch test and create a new file B.txt.
  3. Back to branch master and then create a new file C.txt
1
2
3
master: A---C
\
test: B

The result of following command:(now we are in branch master)

1
2
git 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 commit C