Git rebase vs merge, and when to use each
What git merge and git rebase each do to your history, the one rule that keeps rebase safe, and how to get out of a rebase that went wrong.
Checked 2026-11-19You worked on a branch feature while main moved on. Both commands bring main's new commits into your work; they leave different history behind.
merge: join the two lines
git switch feature
git merge main
Git adds one merge commit with two parents. Nothing that already existed changes, so it is always safe, even on a shared branch. The cost is a history with a bump for every merge.
rebase: replay your commits on top
git switch feature
git rebase main
Git takes each of your commits, in order, and re-applies it on the tip of main. The result is a straight line, as if you had started from today's main. The commits get new ids, because their parent changed.
The rule
Rebase only commits nobody else has. Rebasing a branch other people have pulled gives them a different history from yours, and their next pull turns into a tangle. On your own feature branch, before a pull request, rebase freely. If you already pushed it, push with:
git push --force-with-lease
which refuses if someone else pushed in the meantime (plain --force would overwrite their work).
Conflicts during a rebase
A rebase stops at each commit that conflicts:
# fix the files, then
git add <file>
git rebase --continue
# or give up and go back to where you started:
git rebase --abort
You may resolve the same area more than once, once per commit that touches it.
Tidy up before sharing: interactive rebase
git rebase -i main
opens a list of your commits where pick can become squash (fold into the previous one), reword (new message) or drop. Again: only for commits you have not shared.
Which to choose
- Bringing
maininto your own branch: rebase, for a clean line. - Landing a finished branch on
main: whatever your team does. Many use a merge commit or "squash and merge" in the pull request. - Anything already on a shared branch: merge (or
revert), never rebase.