Rebase vs Merge
7 pages · ~5 min✓ Reviewed
Builds on Branches & Pull Requests for Teams. Next up: Resolving Merge Conflicts.
Rebase vs Merge: The Mental Model
1. Merge: Tie the Lines
- Merge makes a new commit with two parents
- Both histories stay exactly as they happened
- Non-destructive: it only adds history
2. Rebase: Replay
- Take commitsmain..feature
- Replay one by oneon the new tip
- New SHAslinear history
- Old commitsorphaned
bash
git switch feature
git rebase mainreplays feature onto main's tip
- Rebased commits are copies: same diff and message
- Different parent means a different hash
3. Side by Side
| Merge | Rebase | |
|---|---|---|
| History | adds a merge commit | rewritten, linear |
| Old SHAs | kept | orphaned |
| Safety | non-destructive | rewrites history |
Mental model
Merge = tie the two lines together. Rebase = pretend I branched from the latest tip.
- Why do rebased commits get new hashes? The parent changed, so the commit identity changed.
- Which one rewrites history? Rebase. Merge only adds.
Fast-Forward vs Merge Commit
1. Fast-Forward
- Target has no new commits
- Git only moves the branch pointer
- No merge commit is created
2. Merge Commit and Flags
| Case | Result |
|---|---|
| No new commits on target | fast-forward |
| Branches diverged | merge commit, two parents |
--no-ff | merge commit even if ff possible; branch stays visible |
--ff-only | refuses unless ff; fails safely on divergence |
3. Reading the Graph
- Straight line = fast-forward or rebase
- Diamond = merge commit
Rebase in Practice and Conflicts
1. Replay and Conflicts
- Rebase replays feature's commits onto the tip of main
- Replayed commits are new copies with new SHAs
bash
git switch feature
git rebase mainrun from feature, not main
- Conflictstops at one commit
- Fix filesremove markers
- git addmark resolved
- --continuenext commit
| Command | Does |
|---|---|
git rebase --continue | next commit, after git add |
git rebase --skip | drops the current commit |
git rebase --abort | restores the pre-rebase state |
2. Undo and Publish
Interactive Rebase: Squash, Reword, Drop
1. The Todo List
git rebase -i HEAD~Nopens the last N commits- Listed oldest first, edited before replay
- Reorder lines = reorder commits
- Delete a line = drop the commit
text
pick a1b2 add parser fixup c3d4 typo reword e5f6 bad subject
git runs the list top to bottom
2. The Verbs
| Verb | Effect | Message |
|---|---|---|
pick | keep commit as is | kept |
reword (r) | keep changes, edit message | you edit |
squash (s) | melt into previous commit | combined |
fixup (f) |
The Golden Rule and pull --rebase
1. The Golden Rule
- Never rebase pushed commits of a shared branch
- Rebase rewrites hashes, so everyone else must reconcile
- Safe: your own unpushed or private feature branch
- Never rebase
mainormaster
2. pull vs pull --rebase
git pull | git pull --rebase | |
|---|---|---|
| Is | fetch + merge | fetch + rebase |
| On divergence | noisy merge commit | local commits replayed on origin |
pull.rebase truemakes rebase the default pullpull.ff onlymakes a pull fail instead of merging
When to Use Which: Cheat Sheet
1. Rebase or Merge?
privatepublic
| Rebase | Merge | |
|---|---|---|
| Use for | Tidy private branch, update from main before PR | Shared or long-lived branches |
| History | Linear, clean | True history preserved |
2. Squash Merge
- Squash merge collapses a whole PR into one commit on main
- Pro: clean main history
- Con: individual commit detail is lost
3. Command Cheat Sheet
Check yourself
main has no new commits since feature branched. What does git merge feature on main do? What changes with --no-ff?
- Fast-forward: the main pointer moves, no new commit.
--no-ffforces a two-parent merge commit instead.
You rebased a pushed feature. Now git push is rejected. Why, and what is the fix?
- Rebase made new SHAs, so local and remote diverged.
- Use
git push --force-with-lease, never plain--force. - Only safe if nobody else builds on the branch.
The todo list reads: pick A, squash B, fixup C. What do you get?
- One commit holding A, B and C changes.
- Message combines A and B; C's message is discarded.
pick a1b2 A squash c3d4 B fixup e5f6 C