Merge vs Rebase
Course 1 introduced git merge as the way to bring a branch's work back together. There's a second tool for the same general goal — git rebase — and the two produce genuinely different history, not just different commands for the same outcome. Understanding that difference is what lets you choose deliberately instead of by habit.
The Setup — A Diverged Branch
Both examples in this chapter start from the same situation: a feature branch with two commits, while main has gained one commit of its own in the meantime.
Option A: git merge
Merging creates a brand-new commit with two parents — one pointing back into main's history, one pointing back into feature's history. Nothing about either branch's existing commits changes; the merge commit simply ties them together.
$ git merge feature
Merge made by the 'ort' strategy.
Option B: git rebase
Rebasing takes a completely different approach: it replays your branch's commits, one at a time, as if they'd been written starting from the tip of main all along — rather than tying two histories together, it rewrites feature to look like it always branched off the newest point.
$ git rebase main
Successfully rebased and updated refs/heads/feature.
git reflog — Course 3, Chapter 3), but as far as any branch or remote is concerned, feature now points at a different, rewritten history. This is exactly why rebasing a branch others have already pulled is dangerous — covered in the warning below.
Comparing the Results
Safety: Always safe, even on shared/pushed branches — never rewrites existing commits.
Downside: Can produce a tangled-looking history with many merge commits on an active project.
Safety: Safe only on branches nobody else has pulled yet — rewrites commit hashes.
Downside: Loses the literal record of when divergence happened; rewritten hashes can conflict with collaborators' copies.
When to Use Each
| Situation | Use |
|---|---|
| Bringing a finished feature branch into main via a PR | merge (often via the PR's own "Merge" button — Course 1, Ch7) |
| Catching your local feature branch up with main before continuing work | rebase — keeps your branch's history clean as you go |
| The branch has already been pushed and others have pulled it | merge only — never rebase shared history |
| Cleaning up your own messy local commits before opening a PR | interactive rebase (full coverage in Course 3, Ch2) |
| Unsure, or working solo on a small project | merge — fewer ways to get into trouble |
A common middle ground: rebase locally, merge for the PR
Many developers rebase their own feature branch against main repeatedly while working on it solo (keeping their personal history clean, before anyone else has seen it), then let the actual PR merge into main happen as a normal merge — getting a clean personal workflow without ever rebasing anything that's been shared.
Chapter 2 Quick Reference
- git merge — creates a new commit with two parents; preserves exact history; always safe
- git rebase — replays your commits on top of the target branch; creates new commit hashes; linear, clean history
- Critical distinction: rebase rewrites commits — the originals are effectively gone from that branch's perspective
- Golden rule: never rebase a branch that's already been pushed and pulled by someone else
- Common pattern: rebase your own unshared branch to stay current with main; merge for the final PR into main
- When unsure: default to merge — it's never the wrong choice, just sometimes the less tidy one
- Next chapter: resolving conflicts properly — tools, strategies, and aborting safely when it goes wrong