Sub topic 3
Merge versus rebase: two ways to integrate work
This chapter assumes you can commit, branch, switch, and push. It also assumes you can read git log --graph output and know that origin/main is your local copy of the remote main branch. The chapter compares the two ways Git combines divergent work and shows how each changes the commit graph.
The situation
You branched feature from main. Since then, main has gained a new commit. The two branches have diverged: each has commits the other lacks. To ship your work, you must integrate the changes. Git offers two tools for this, and they produce different histories.
Merge: join the histories
git merge creates a new commit with two parents. It records that two lines of work came together. Existing commits are not touched.
git switch feature
git merge origin/mainThe history is accurate about what happened and when. The cost is extra merge commits, which can make git log --graph hard to read on busy branches.
Rebase: replay the commits
git rebase takes the commits unique to your branch and replays them on top of another commit. Git applies each change again, one at a time, starting from the new base.
git switch feature
git fetch origin
git rebase origin/mainThe result is a linear history with no merge commit. Reading it, and running tools like git bisect, becomes simpler.
The key difference: new hashes
A commit's hash is computed from its content, its message, and its parent. When rebase replays F1 on top of C, the parent changes, so the hash changes. The replayed commits are new commits with the same changes and messages. The originals remain in the repository until garbage collection removes them, but your branch no longer points to them.
Merge never rewrites anything. Rebase always does. That one fact explains the rules in later chapters about sharing and recovering history.
Comparing the two
| Aspect | Merge | Rebase |
|---|---|---|
| Resulting history | Branching, with a merge commit | Linear |
| Existing commits | Unchanged | Replaced by new commits |
| Records when work was integrated | Yes | No |
| Conflicts | Resolved once, in the merge commit | May be resolved once per replayed commit |
| Safe on shared branches | Yes | No, if others already have the commits |
Choosing between them
Neither is correct in every case. These criteria usually decide it:
- Use rebase to update a private feature branch with the latest
main, so the branch reads as a clean sequence before review. - Use merge when the branch is shared or when your team wants the integration point recorded.
- Use merge when you want to avoid rewriting commits that someone else may have based work on.
- Follow your team's convention. Many teams rebase locally and then merge the finished branch into
main.
A quick check on your own repository
Run git log --oneline --graph before and after a rebase on a throwaway branch. Compare the hashes of your commits in both listings. They differ, even though the messages match.
Exercise
A teammate says rebasing "moves" your commits onto the new base. Why is it more accurate to say Git copies them, and what visible sign confirms this?
Show answer
Rebase replays each change on a new parent, and a commit's hash depends on its parent. So each replayed commit has a new hash, while the original commits still exist until garbage collection. You can confirm this by comparing hashes in git log --oneline before and after the rebase.
Exercise
Your feature branch is private and main has moved ahead. You want a straight-line history with no merge commit. Which command do you run after git fetch origin?
Show answer
git rebase origin/main, run while feature is checked out.