Merge and rebase produce the same file contents in most cases. They differ in what they do to history, and that difference is the whole argument.
Merge preserves topology
git checkout main
git merge feature
Git creates a merge commit with two parents: the tip of main and the tip of feature. The branch structure is visible in git log --graph. Nothing that already happened is rewritten. This is the safe default for shared branches.
The cost is a non-linear history. A long-lived feature branch merged repeatedly produces a dense graph that is hard to read. Some teams accept that; others do not.
Rebase replays commits
git checkout feature
git rebase main
Git takes each commit on feature that is not on main, replays it on top of main, and rewrites the commit IDs. The result is a straight line: feature commits appear as if they were written after the latest main.
The benefit is readability and a clean git log. The cost is that the original commits are replaced. Anyone who based work on them now has divergent history.
The safety rule
Rebase only commits that nobody else has pulled. On a personal feature branch, that is usually true. On main, it is almost never true. If you must rebase a shared branch, coordinate first, and use:
git push --force-with-lease
--force-with-lease refuses to overwrite if the remote has moved since your last fetch. Plain --force will overwrite a teammate's push without warning. The lease is not a guarantee that nobody else is working; it is a guard against the most common accident.
Interactive rebase is a separate tool
git rebase -i HEAD~4
This opens a list of the last four commits with actions: pick, reword, edit, squash, fixup, drop. It is useful for cleaning up a branch before review: combine work-in-progress commits, fix messages, reorder changes.
Two cautions:
- Reordering commits can produce conflicts that did not exist before.
- Squashing removes the individual commit messages; make sure the final message explains the change.
If a rebase goes wrong:
git rebase --abort
If it already completed and you want the old branch back:
git reflog
git reset --hard <sha-before-rebase>
Merge strategies
git merge --no-ffforces a merge commit even when a fast-forward is possible. Useful for preserving the feature boundary.git merge --ff-onlyrefuses to merge unless it can fast-forward. Useful onmainwhen you want linear history without merge commits.git merge --squashstages the combined changes without creating a merge commit; you then commit once. This loses individual commit history but keepsmainclean.
Squash merging is common on platforms like GitHub. It combines the branch into one commit on main, which is a form of history rewriting from the feature branch's perspective but does not rewrite main.
What actually matters
| Concern | Merge | Rebase |
|---|---|---|
| Shared branch safety | Safe | Risky |
| History readability | Branchy | Linear |
| Conflict resolution | Once at merge | Possibly per commit |
| Commit IDs | Preserved | Rewritten |
| Audit trail | Full topology | Flattened |
Teams often adopt a hybrid: rebase the feature branch onto main to resolve conflicts locally, then merge with --no-ff to preserve the boundary. Or squash-merge the feature and keep main linear. Both are workable if applied consistently.
A practical policy
- Never rebase
mainor any branch others have checked out. - Rebase your own feature branch before opening a pull request, not after review has started.
- Use
--force-with-lease, never--force. - Prefer merge or squash-merge for integration; choose based on whether the individual commits are meaningful.
- If a rebase produces conflicts on every commit, abort and merge instead.
The goal is not a perfect graph. It is a history that a teammate can read while debugging a regression six months later. Both strategies can achieve that; inconsistency cannot.
The rebase recipe covers conflict resolution and --onto, and the branch cleanup page covers deleting merged branches safely.