Git merge vs rebase: did the commit leave your machine?
Git merge vs rebase turns on one question: did the commit leave your machine? On git 2.47.3 merge kept every SHA and added a node; rebase turned 5d48940 into 6b2be0a.
Git merge vs rebase is one question: did the commit leave your machine? Unpushed gets rebased; anything someone may have pulled gets merged. On our bench — git 2.47.3 — merge kept every SHA and added one node; rebase replayed the commits under new SHAs. Same tree, different story.
Do not rebase commits that exist outside your repository that people may have based work on. — Pro Git, 2nd edition, "Git Branching — Rebasing"
git merge vs rebase: the two histories
git log --oneline --graph tells you which one ran: a |\ node means merge, a straight line means rebase won. Fast-forward blurs the picture — if main never moved, there is nothing to merge, just a label slid forward.
* 67c0442 c4 feature 2 # rebased, then merged — fast-forward * 6b2be0a c3 feature 1 # new SHAs — 5d48940 before the rebase * 1398836 c5 main moved on * facd5a4 c2 main work * f45b000 c1 base
Five commits, zero merge nodes: rebase your branch onto main, then git merge --ff-only feature — "Fast-forward" appears and no merge commit is created. Those marked SHAs now exist only on your machine; push them and a teammate's copy keeps the original.
Which commits are still yours?
git log origin/main..HEAD lists what the remote never saw. Only those are safe to replay. An open PR or a branch you force-pushed before means the local graph is lying to you. Fetch first, then check git status -sb.
| Case | Command | Why it wins |
|---|---|---|
| Your feature branch, never pushed | git rebase main | Linear history, one clean review sequence |
| Branch others pull from | git merge | No SHA rewrite — no duplicate commits in anyone's copy |
| Local main diverged, nothing pushed | git pull --rebase | Syncs without a node; your commits stay on top |
| Fork catching up to upstream | git merge upstream/main | The PR stays attached to its commits |
| Urgent hotfix that must land now | git merge --no-ff | Visible marker of where the fix entered |
| Rebase went sideways | git reset --hard HEAD@{1} | The reflog still holds the pre-rebase tip |
Rewinding a rebase
$ git reflog | head -41398836 HEAD@{0}: reset: moving to HEAD~267c0442 HEAD@{1}: merge feature: Fast-forward1398836 HEAD@{2}: checkout: moving from feature to main67c0442 HEAD@{3}: rebase (finish): returning to refs/heads/feature$ git reset --hard HEAD@{1}HEAD is now at 67c0442 c4 feature 2
Rebase moves pointers; it deletes no objects. One line restores the branch with staged and unstaged work intact. git reflog | head -3 puts the pre-rebase tip at HEAD@{1}, one reset away — but entries expire after about 90 days by default.
what breaks on a shared branch
Every copy holding the old commits disagrees about reality: no shared ancestry, a forced next push, duplicate commits in every later merge, and review comments detached from their SHAs.
--squash lands a branch's changes as one undigested block — no merge commit, no link to branch history. It rewrites nothing; it only refuses to record internal steps.
- bench: git 2.47.3, single-use platform — every graph and SHA above run there · first published 2026-10-05
- SHA rewrite, fast-forward and reflog recovery are bench outputs; the Pro Git warning is quoted from the official book
- git itself was written from scratch in a ten-day sprint in April 2005 — the merge vs rebase argument has run ever since