Skip to content

Rebase vs Merge

The war between rebase and merge is a false one. They’re different tools for different situations.

gitGraph
   commit id: "A"
   commit id: "B"
   branch feature
   commit id: "F1"
   commit id: "F2"
   checkout main
   commit id: "C"
   merge feature id: "M"

Preserves the shape of history — you can see the branch existed.

Interactive rebase — cleaning up before PR

Section titled “Interactive rebase — cleaning up before PR”
Terminal window
git fetch origin
git rebase -i origin/main

Then in the editor:

rebase-todo
pick a1b2c3 Add config parser
squash d4e5f6 Fix typo
squash g7h8i9 Fix another typo
reword j0k1l2 WIP notes
  1. Check git reflog — every ref update is logged for ~90 days.
    Terminal window
    git reflog
    # a1b2c3d HEAD@{0}: rebase (finish): returning to refs/heads/feature
    # e4f5g6h HEAD@{5}: commit: Add config parser
  2. Reset back to the pre-rebase state.
    Terminal window
    git reset --hard HEAD@{5}
  3. Force-push — but only if the branch is yours alone.
    Terminal window
    git push --force-with-lease origin feature
StrategyBest forDownside
Squash and mergeSingle-purpose PRs, keeps main linearLoses per-commit context
Rebase and mergeMulti-commit PRs, linear historyRewrites SHAs — CI must re-run
Merge commitLong-lived feature branches with historyAdds merge nodes