Git Rebase vs Merge: What Each Actually Does, and the One Rule That Keeps Rebase Safe
Every team eventually has the argument: should feature branches merge or rebase onto main? The honest answer is that both are right, for different jobs. Merge preserves history exactly as it happened. Rebase rewrites history into a straight line. The trouble starts when someone rebases a branch other people have pulled, or merges everything and turns the log into spaghetti. This guide explains what each one actually does to your commits, when to use which, and the one rule that keeps rebase safe.
What a merge really does
git merge feature takes the two branch tips and their common ancestor and creates a merge commit with two parents. Nothing that already existed changes: every commit keeps its original hash, author, and timestamp. The history is a faithful record, including the fact that work happened in parallel.
The cost is shape. On a busy repo where everyone merges their feature branches into main, the log becomes a braid of crossing lines, and git log fills up with "Merge branch 'x' into main" commits that carry no information. git log --first-parent helps, but the underlying graph is still noisy.
What a rebase really does
git rebase main takes your branch's commits, temporarily sets them aside, fast-forwards your branch to main's tip, then replays your commits one by one on top. Each replayed commit is a new commit: same changes and same message, but a new hash and a new committer date. When it finishes, your branch looks like you started from the latest main all along.
The payoff is a linear history: every feature lands as a clean sequence of commits, git log reads like a changelog, and git bisect works without navigating merge commits. The danger is exactly the thing that makes it work: it rewrites commits.
The golden rule of rebase
Never rebase commits that exist anywhere outside your local repo. If you pushed a branch and a teammate pulled it, rebasing it gives the same logical commits new hashes. Their copy and yours now disagree about history, and the next pull merges the old and new versions together, duplicating every commit. This is the accident that gives rebase its scary reputation.
The safe scope for rebase is work only you have: your local branch, or a feature branch that is genuinely yours alone. If you do rebase a branch you already pushed, the push needs --force-with-lease, which refuses to overwrite the remote if someone else added commits since your last fetch. Plain --force skips that check; there is almost never a reason to use it.
A practical workflow that uses both
The common pattern in teams that like clean history:
- While you work: rebase your feature branch onto main regularly (
git fetch && git rebase origin/main) so it stays current and conflicts arrive in small, understandable doses. - Before opening the PR: optionally
git rebase -ito squash fixup commits, so reviewers see the story, not your typos. - Landing the PR: merge it (or squash-merge, which is a different beast: GitHub builds one new commit from the whole branch) so the main branch records when the feature actually landed.
- On shared long-lived branches (main, release branches): merge only. Rebase never touches these.
Conflicts: the real difference in feel
With a merge you resolve every conflict once, in the merge commit. With a rebase you resolve conflicts per replayed commit, in the order they were written. If three of your commits all touch the same lines that changed on main, you may resolve the same hunk three times, but each resolution is small and sits next to the commit that caused it. git rerere (reuse recorded resolution) remembers how you resolved a conflict and applies it automatically next time; turn it on if you rebase often.
Mid-rebase you have three exits: fix and git rebase --continue, skip the commit with --skip, or bail out entirely with git rebase --abort, which puts your branch back exactly as it was. Abort is the feature that makes rebasing safe to learn on.
Interactive rebase in three commands
git rebase -i HEAD~5 opens the last five commits in your editor. Change pick to reword to fix a message, squash to fold a commit into the one above it, or drop to delete it. Save and git rewrites the sequence. This is how "wip", "fix", "actually fix" becomes one presentable commit, and it only touches commits that are already yours.
Quick reference
| Question | Merge | Rebase |
|---|---|---|
| Rewrites existing commits? | Never | Yes (new hashes) |
| History shape | True graph, can get noisy | Linear, reads like a changelog |
| Safe on shared branches? | Yes | No, never |
| Conflict resolution | Once, at the merge | Per replayed commit |
| Undo path | Revert the merge commit | --abort mid-way, reflog after |
| Best for | Landing PRs, shared branches | Updating and cleaning your own branch |
When it goes wrong anyway
If you rebased something you should not have, or lost a commit in an interactive rebase, git reflog is the safety net: it records everywhere HEAD has been, including the state before the rebase. Find the pre-rebase entry and git reset --hard to it. Commits are almost never truly lost; they are just unreferenced for a while.
Ever wondered what those commit hashes actually are? Our Hash Generator shows the SHA family git uses, and SHA-256 vs MD5 explains why git moved to SHA-256. Related reading: the curl cheat sheet for the CLI commands that pair with git in daily work, and semantic versioning for what a clean history should produce at release time.
Try it yourself
Use the free browser-based Hash Generator on DevBench — no signup, runs entirely in your browser.
Open Hash Generator