Coding Tricks

Git Merge vs Rebase Explained With Examples

Git merge vs rebase is one of those arguments that never ends, partly because most explanations stop at a diagram. So we did the boring thing instead. We built small throwaway repositories on a Windows PC with Git 2.45.2, ran merge, rebase, squash, pull and a few rescue commands against the same starting history, and copied the terminal output into this post. Every hash, every error and every count below came from that run. Every command is shown, so you can repeat any of it in a spare folder.

Quick answer: git merge joins two branches with a new merge commit and leaves existing commits alone. git rebase replays your commits on top of another branch, which gives them new hashes and a straight-line history. The files end up the same. Rebase your own local work; merge anything other people have already pulled.

  • Same starting repo: merge left 6 commits on main (one of them a merge commit), rebase plus fast-forward left 5.
  • Rebase changed both feature commit hashes (9d36ae7 became 2bf6bec). That’s why it breaks shared branches.
  • On a conflict, merge stopped once. Rebase stopped once in one test and three times in another, depending on how we resolved the first one.

Git merge vs rebase: the difference in one table

Both commands answer the same question: “main moved on while I was working, how do I bring the two lines of work together?” They just record the answer differently. The Pro Git book’s rebasing chapter puts it plainly: “There is no difference in the end product of the integration, but rebasing makes for a cleaner history.” The table sums up what we saw.

git mergegit rebase (then fast-forward)
Commits on main afterwards65
Extra merge commitYes (329d50d)No
Your commit hashesUnchangedRewritten
History shapeBranch and joinOne straight line
Conflict stops in our tests11 or 3
Safe on a pushed, shared branchYesNo, needs a force push
Git merge vs rebase vs squash results from the same test repo: commit count, merge commit, hashes and conflict stops

The test repo we used

Every example starts from the same small story. Main has two commits. A feature branch adds a cart in two commits. Meanwhile someone adds a login commit to main, so the branches have split. We fixed the author dates, which means the same Git version should give you the same hashes.

$ git log --oneline --graph --all
* 847ad7b Add login
| * bb4d108 Add remove to cart
| * b4f095f Add cart
|/
* 3b89578 Add README
* 5708d8a Initial app

That fork in the graph is the whole problem. Your feature was built on 3b89578, but main is now at 847ad7b.

What git merge does

From main, git merge feature created a new commit with two parents: the tip of main and the tip of feature. Git 2.45 used its default “ort” strategy and finished without asking anything, because the branches touched different files.

$ git merge feature -m "Merge branch 'feature'"
Merge made by the 'ort' strategy.
 cart.py | 2 ++
 1 file changed, 2 insertions(+)

$ git log --oneline --graph
*   329d50d Merge branch 'feature'
|\
| * bb4d108 Add remove to cart
| * b4f095f Add cart
* | 847ad7b Add login
|/
* 3b89578 Add README
* 5708d8a Initial app

Look at the hashes. bb4d108 and b4f095f are exactly what they were before. Nothing was rewritten, so anyone who already pulled the feature branch is fine. The price is one extra commit (329d50d) and a graph that shows the fork. On a busy repo with dozens of merged branches a week, that graph gets hard to read, and that’s the main complaint people have about merging.

Git merge vs rebase vs fast forward

A fast-forward isn’t a third strategy. It’s what merge does when main hasn’t moved since you branched. There’s nothing to join, so Git just slides the main label forward. In a second repo where main stayed still, plain git merge feature printed “Fast-forward” and made no merge commit. Running it again with --no-ff forced a merge commit (f73a4a7) anyway. Some teams use --no-ff on purpose so every feature shows up as one bubble in the history. The git merge documentation lists both flags along with --ff-only, which refuses to do anything unless a fast-forward is possible.

What git rebase does

Same starting story, fresh repo. This time we switched to feature and ran git rebase main. Rebase takes your commits off, moves the branch to the tip of main, and replays them one at a time. Watch the hashes before and after.

$ git log --oneline feature -2
77c0cbb Add remove to cart
9d36ae7 Add cart

$ git rebase main
Rebasing (1/2)
Rebasing (2/2)
Successfully rebased and updated refs/heads/feature.

$ git log --oneline feature -2
d30282b Add remove to cart
2bf6bec Add cart

Same messages, same changes, brand new commits. That’s the part beginners miss. A rebase doesn’t move commits. It makes copies with a new parent, and the old ones are left behind (you can still reach them for a while, more on that below).

Now main can catch up with a fast-forward, because feature sits directly on top of it:

$ git switch main
$ git merge --ff-only feature
Updating e5374e5..d30282b
Fast-forward

$ git log --oneline --graph
* d30282b Add remove to cart
* 2bf6bec Add cart
* e5374e5 Add login
* 51e4d24 Add README
* b0c00c6 Initial app

Five commits, one straight line, no merge commit. If you read history with git log or hunt bugs with git bisect, this is easier to follow. It’s also a slight lie: it looks like the cart was written after the login, which isn’t what happened.

Git merge vs rebase vs squash

Squash merging is the third option you’ll see on GitHub and GitLab buttons. git merge --squash feature takes all the changes from the branch and stages them as one new commit on main. Git told us exactly that: “Squash commit — not updating HEAD”. After committing, main had four commits, with the whole cart feature in a single one.

Then something useful happened. git branch -d feature refused:

$ git branch -d feature
error: the branch 'feature' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature'

Git is right. The squash commit has none of the branch’s commits in it, only their combined changes, so as far as Git knows, feature was never merged. You need -D to delete it. It also means you shouldn’t keep working on a branch after it’s been squash-merged, because the next merge from it will try to apply the same changes again.

Cherry-pick, which also comes up in the “merge vs rebase vs cherry-pick” searches, is rebase’s little sibling. It copies one chosen commit onto your current branch with a new hash. Rebase is roughly a cherry-pick of every commit on your branch in order.

Conflicts: where rebase can cost more

Most guides say rebase can make you fix the same conflict over and over. We wanted to see when that’s true. Setup: main changes timeout=10 to 15, and the feature branch has three commits that raise it to 20, then 30, then 45. Every one of them touches the same line.

Merge stopped once, which is what you’d expect. It compares the two branch tips and you resolve the result in one go. Rebase also stopped only once. After we resolved commit one to timeout=20, commits two and three applied cleanly, because each one changed exactly the line the previous one left behind.

Then we changed one detail. Main’s hotfix kept the value but added a comment, # do not lower, see ticket 512, and we kept that comment while resolving. Now each later commit was trying to change a line that no longer looked the way it expected:

stop 1: Could not apply 76463e7... Raise timeout to 20
stop 2: Could not apply d9af98c... Raise timeout to 30
stop 3: Could not apply b3316e4... Raise timeout to 45
REBASE conflict stops: 3
MERGE conflict stops: 1

The part most comparisons skip: rebase doesn’t give you repeat conflicts every time. It gives you one stop per commit whose change no longer applies to the text you produced while resolving. If your resolution keeps something from main on a line your later commits also touch, every one of those commits will stop. Before a messy rebase, squash your own commits first (see the cleanup section) and you’ll get one conflict instead of many. Or just merge.

The golden rule of rebasing

Pro Git puts the rule in bold: “Do not rebase commits that exist outside your repository and that people may have based work on.” Here’s what breaking it looks like. We pushed feature to a local bare remote, rebased it, and pushed again:

$ git push origin feature
 ! [rejected]        feature -> feature (non-fast-forward)
error: failed to push some refs to '...origin.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart.

$ git push --force-with-lease origin feature
 + e1ce864...368cba2 feature -> feature (forced update)

The hint is misleading here. Your branch isn’t really “behind”; it has different commits. If you follow the hint and run git pull, you end up with both the old and the rebased copies of every commit in one branch. The force push worked, but anyone who already pulled the old feature branch now has commits that no longer exist on the remote. If you do need to force push, --force-with-lease is the safer flag. It has its own trap with background fetches, which we covered in the Git commands every developer should know.

Git pull: rebase vs merge

git pull is a fetch followed by a merge or a rebase, and recent Git versions won’t pick one for you when histories have split. We cloned a repo twice, pushed a commit from one clone, made a different commit in the other, then pulled. With no settings at all (we pointed Git at an empty config file so our own PC’s settings didn’t interfere), Git 2.45 refused:

$ git pull
hint: You have divergent branches and need to specify how to reconcile them.
hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only
fatal: Need to specify how to reconcile divergent branches.

$ git pull --rebase
Successfully rebased and updated refs/heads/main.

$ git log --oneline --graph
* dd6651c I add UI
* ae7b5e8 Teammate adds API
* a2ba473 Initial app

The current git pull documentation describes fast-forward-only as the default when you haven’t picked a method, so it fails rather than guessing. For pulling a shared branch you work on directly, git pull --rebase is usually the nicer choice. Your unpushed commits get replayed on top of your teammate’s, and you avoid the “Merge branch ‘main’ of …” noise commits. It’s safe because the commits being rewritten are yours and haven’t left your machine.

Undo a rebase and clean up commits

Rebase feels scary because it rewrites things. It’s less scary once you know the old commits stick around. Right after a rebase, Git stores where you were in ORIG_HEAD, and the reflog shows every step:

$ git reflog -5
c95f2ee HEAD@{0}: rebase (finish): returning to refs/heads/feature
c95f2ee HEAD@{1}: rebase (pick): Add remove to cart
c63b89a HEAD@{2}: rebase (pick): Add cart
42409a9 HEAD@{3}: rebase (start): checkout main
94c3113 HEAD@{4}: checkout: moving from main to feature

$ git reset --hard ORIG_HEAD
HEAD is now at 94c3113 Add remove to cart

The branch went straight back to 94c3113, the hash it had before the rebase. One warning: reset --hard throws away uncommitted changes, so commit or stash first. And ORIG_HEAD gets overwritten by the next reset, merge or rebase, so use it right away or read the hash from the reflog.

Safe git rebase workflow: fixup commit, autosquash, undo with ORIG_HEAD, never rebase pushed branches

Rebase also cleans up messy history before review. Say you notice a bug in an earlier commit. Commit the fix with git commit --fixup <hash>, then run git rebase -i --autosquash main. In our run, a log of “Add cart”, “Add total”, “fixup! Add cart” became two tidy commits, with the fix folded into “Add cart”. The git rebase documentation explains the todo list it opens; we accepted it unchanged. If you review AI-written commits before pushing, this is a good habit to pair with our checklist for reviewing AI-generated code.

When to use git merge vs rebase

Here’s the rule set we’d hand a new developer. It matches Pro Git’s own advice: “rebase local changes before pushing to clean up your work, but never rebase anything that you’ve pushed somewhere.”

  • Your own branch, not pushed yet: rebase onto main as often as you like. Clean it up with --autosquash.
  • A branch other people pull from: merge. Don’t rewrite it.
  • Pulling a shared branch you commit to directly: git pull --rebase, or set pull.rebase true.
  • Finishing a pull request: follow your team’s setting. Squash if the branch’s commits are noise; merge with --no-ff if you want each feature visible as a group.
  • A long conflict-heavy branch: merge, or squash your commits first and then rebase.

Our verdict: learn both, default to rebasing private work, and don’t let anyone shame you for a merge commit. A clean graph is nice. A teammate losing an afternoon to a rewritten branch is not.

FAQ

What does git rebase do vs merge?

Merge adds one new commit that joins two histories and leaves every existing commit untouched. Rebase copies your commits onto a new base, so they get new hashes and the history becomes a straight line. The final files are the same either way.

Does git merge rebase?

No. A plain git merge never rewrites commits. It either fast-forwards (when the target hasn’t moved) or creates a merge commit. Only git rebase, git pull --rebase or a pull with pull.rebase set will replay your commits.

Is git rebase risky?

On your own unpushed commits, not really. The reflog keeps the old commits, and git reset --hard ORIG_HEAD undid our rebase in one command. It becomes risky on branches other people have pulled, because you then have to force push and their copies stop matching.

Should I use rebase or merge for pull requests?

Rebase your PR branch onto main while it’s still yours, so reviewers see a clean diff. For the final step into main, use whatever your team agreed on: merge commit, squash or rebase-and-merge. Consistency matters more than the choice.

Next: new to the basics behind all this? Our Python tricks for beginners guide is the starting point for the Coding Tricks section, and the Git commands guide covers reflog, revert and bisect in more depth.

How we tested: a small Python script built every repo from scratch with fixed commit dates and an empty Git config, then ran the commands above with Git 2.45.2.windows.1 on Windows, on 17 November 2026. Outputs are copied as printed, with long temp paths shortened.

Leave a Comment