Rebase as an Alternative to Merge in Git: while merging is definitely the easiest and most common way to integrate changes, it's not the only one: "Rebase" is an alternative means of integration.
2. Re-what?
$ git rebase branch-B
is neither better nor
worse - but different
integrates code from one
branch into another
is an alternative
to “git merge”
4. Merge
merge commit
C1 branch-A
branch-BC4C2
C3 C5
creates an automatically
generated merge commit
keeps branch histories separate & only
“melts” together with merge commit
$ git checkout branch-A
$ git merge branch-B
5. Rebase
creates no additional
commits (no merge commit)
during integration
C1
branch-B
branch-A
C3C4C2
history of “branch-A” looks like
development had never happened
in multiple branches
$ git checkout branch-A
$ git rebase branch-B
8. Rebase in Slow-Motion
Step 1
C1 branch-A
branch-BC4C2
C3
all commits on branch-A that happened
after the common ancestor commit (C1)
are “saved away temporarily”
9. Rebase in Slow-Motion
Step 2
new commits from
branch-B are applied
C1 branch-A branch-BC4C2
C3
at this point, both branches
look exactly the same
10. Rebase in Slow-Motion
Step 3
C1
branch-B
branch-A
C3C4C2
the “temporarily saved away” commits from
branch-A are re-applied - but on a new position:
on top of the integrated commits from branch-B
11. Rebase in Slow-Motion
Result
history for branch-A looks as if all
development had happened on this branch,
no relation to branch-B is visible
C1
branch-B
branch-A
C3C4C2
12. Attention
Rebase Rewrites History!
C1
branch-B
branch-A
C3C4C2
re-applied commits (C3) are effectively
new commits (with new SHA-1 IDs)
because they were re-based onto a
new parent commit (C4)
The Golden Rule of Rebasing
“Never rebase commits that have already
been pushed to a remote repository!”
13. Learn Git with our free online book on
www.git-tower.com/learn