Fork me on GitHub

Git branch design lesson: Rebase vs. merge

Overview

Teaching: 10 min
Exercises: 0 min
Questions
  • What is the advantage of merging?
  • What is the advantage of rebasing?
Objectives
  • Obtain a mental representation of the rebase model.

Rebase vs. merge

To illustrate rebasing we consider the following situation - we wish to merge master into devel:

Now you know how to do it:

$ git checkout devel
$ git merge master

This creates a merge commit:

But there is an alternative for integrating master commits into the devel branch:

$ git checkout devel
$ git rebase master

  • git rebase replays the branch commits b1 to b3 on top of master.
  • As if they were committed after c5.
  • This changes history (notice that the commits b1 to b3 have been replaced by b1* to b3*).
  • Discuss the advantages and disadvantages.

Advantages and disadvantages

  • git rebase makes “merges” producing a linear history.
  • git merge resolves all conflicts in a single commit, with git rebase each commit may need conflict resolution.
  • git rebase may invalidate tests.
  • git merge preserves chronology of commits and creates explicit merge commits (unless fast-forward).
  • git rebase can change chronology of commits.
  • When working with others do not rebase commits that other people depend on (history has changed).
  • Reference: “Treehouse of Horror V: Time and Punishment”, The Simpsons (1994).




Question

  • How does rebasing affect testing?

Key points

  • Rebasing creates nice linear history without merge commits, but is associated with potential risks.