principlegitTip
Rebase vs merge: choosing the right integration strategy
Viewed 0 times
rebase vs mergelinear historymerge commitgit rebaseintegration strategy
Problem
Teams use merge and rebase interchangeably without understanding the tradeoffs, resulting in messy history or unexpected SHA changes that break collaborators' branches.
Solution
Merge preserves history as it happened — use for integrating shared/public branches:
git checkout main && git merge feature/my-feature
Rebase rewrites history for a linear timeline — use for local cleanup before merging:
git checkout feature/my-feature && git rebase main
Golden rule: never rebase commits that exist on a remote shared branch. Only rebase your local unshared work.
git checkout main && git merge feature/my-feature
Rebase rewrites history for a linear timeline — use for local cleanup before merging:
git checkout feature/my-feature && git rebase main
Golden rule: never rebase commits that exist on a remote shared branch. Only rebase your local unshared work.
Why
Merge creates a new merge commit tying two histories together. Rebase replays your commits on top of another branch, rewriting their SHAs. Rebasing shared commits causes divergence for everyone who has the old SHAs.
Gotchas
- After rebasing a feature branch, you must force-push it — use --force-with-lease
git pull --rebaseavoids merge commits when updating from remote — many teams prefer this for pull operations- Merge is safer in open-source contexts where you don't control other contributors' workflows
Code Snippets
Merge vs rebase patterns
# Merge: preserves branch topology, creates merge commit
git checkout main
git merge feature/my-feature
# Rebase: linear history, replays commits on top of main
git checkout feature/my-feature
git rebase main
# Then fast-forward merge
git checkout main
git merge feature/my-feature
# Pull with rebase (avoid merge commits on pull)
git pull --rebase origin mainContext
Deciding how to integrate a feature branch back into main or keep it up to date
Revisions (0)
No revisions yet.