Git merge vs. rebase: choose the right history
Compare merge and rebase by history shape, collaboration risk, conflict workflow, and practical commands for published and unpublished work.
Merge and rebase both integrate commits, but they tell different stories. Merge joins histories with a commit that has two parents. Rebase copies commits onto a new base, producing new commit identities and a linear history.
Quick decision
| Situation | Prefer | Why |
|---|---|---|
| The branch is shared or already published | Merge | Preserves commit identities others may reference |
| Your local commits are unpublished | Rebase may fit | Produces a focused linear series before review |
| The repository requires merge commits | Merge | Matches the project’s history policy |
| You are unsure who uses the branch | Merge | Avoids rewriting shared history |
What merge does
git switch feature/example
git merge main
If a fast-forward is impossible, Git creates a merge commit after conflicts are resolved. The topology records that two lines of work existed. Abort an unresolved merge with git merge --abort.
What rebase does
git switch feature/example
git rebase main
Git finds commits unique to the current branch and replays their changes on top of main. Continue after each conflict with git rebase --continue, or return to the original state with git rebase --abort.
Risk: Do not rebase shared commits casually. The rewritten commits have different IDs, so a later push may require a coordinated force update.
Before rewriting valuable work, create a pointer:
git branch backup/before-rebase
If your team explicitly approves the rewritten push, prefer --force-with-lease over --force, and fetch immediately before pushing. The lease reduces accidental overwrites but does not make history rewriting harmless.
Conflicts are not avoided
Both strategies can conflict. Merge usually asks you to resolve each overlapping file once for the integration. Rebase can surface conflicts commit by commit. Run tests after either workflow and inspect the graph:
git log --oneline --graph --decorate --all -20
The best policy is the one your team understands and applies consistently. Use the branching cheat sheet for the surrounding workflow.