5. Rewriting History
Amend, rebase, cherry-pick, squash — all the same primitive in disguise
Commits in Git are immutable. Once an object is written to the store with hash
3e8f4c7, that hash will always refer to those exact bytes. So
what do people mean when they say they "rewrote" history?
They mean: they created new commits that look like edited versions of old ones, then moved a branch ref to point at the new tip instead of the old one. The old commits are still in the object store — they're just orphaned, kept alive by the reflog for 90 days before garbage collection. Every history-rewriting tool in Git is some variation on "build new commits, move the ref."
git commit --amend: the gateway drug
git commit --amend looks like it modifies the last commit. It doesn't.
It creates a brand new commit whose parent is the old commit's parent (not the old
commit itself), with your amended content, and updates the current branch to point
at it.
This is why amending a commit changes its hash. Different content → different hash,
every time. That matters: if you've already pushed c3 to a shared
branch, amending creates a parallel c3' that the remote has no knowledge
of, and the next push is non-fast-forward.
git cherry-pick: transplanting a single commit
Cherry-picking takes a commit from somewhere else in the DAG and replays its changes on top of your current HEAD, producing a new commit with the same message and patch-semantics but a different hash (because the parent is different).
Mechanically: Git computes the patch (diff between 3e8f4c7 and its
parent), applies it to your working tree, and creates a new commit. If the patch
doesn't apply cleanly you get a conflict, same machinery as merge.
This is the building block for a lot of workflows — "bring this one bug fix from
main onto the release branch," "lift this commit from a colleague's
fork." It's the smallest possible history rewrite: one new commit, one ref move.
git rebase: cherry-pick in a loop
Rebasing looks complicated because the UX talks in terms of "moving a branch onto
another base." Under the hood, git rebase main is mechanical:
- Find the set of commits on the current branch that aren't on
main(themain..HEADset). - Check out
main(detached HEAD). - Cherry-pick each of those commits, in order, onto the new base.
- Update the original branch ref to point at the new tip.
That's it. f1 and f2 are untouched in the object store;
f1' and f2' are new commits with different hashes (new
parents, possibly new timestamps). The branch ref moved. The reflog has a record of
every step.
Why rebasing feels risky but usually isn't
If a rebase goes wrong — wrong conflict resolution, wrong order, chose the wrong
base — the original commits are still in the reflog. git reset --hard ORIG_HEAD
(Git sets ORIG_HEAD before any rebase) puts the branch back exactly
where it was. The only way to really lose work during a rebase is to force-push
the rewrite and then wait 90 days for GC.
git rebase -i: the Swiss Army knife
Interactive rebase drops you into an editor with a todo list of commits and lets you rewrite them en masse. Each line starts with a verb:
The verbs
| Verb | What it does |
|---|---|
pick | Apply the commit as-is. |
reword | Apply it, then open the editor so you can rewrite the commit message. |
edit | Apply it, then pause so you can amend the commit (add/remove files, split it). |
squash | Combine with the previous commit; merges both messages. |
fixup | Like squash but silently discards this commit's message. |
drop | Skip this commit entirely. |
| (reorder lines) | Changes the application order — Git replays in the order you list. |
Every verb is expressible in terms of the underlying primitives: cherry-pick,
amend, skip. Interactive rebase is a scripted loop over those. When you see
Stopped at af5626b for editing…, Git literally paused the cherry-pick
loop so you can make changes and run git rebase --continue.
Squashing: why you might want fewer commits
A feature branch typically accumulates noisy history: "wip", "fix broken test", "try again", "ugh", "actually fix the test". When merging, many teams prefer a single clean commit that describes the end state. Three ways to get there:
- Interactive rebase with
squash/fixup— merge N commits into 1 before opening the PR. git merge --squash feature— doesn't create a merge commit; stages all the branch's changes so you can commit them as one.- "Squash and merge" in GitHub/GitLab — the forge does the squash on merge.
The tradeoff: you lose granular history. Bisecting becomes coarser, blame is less useful, individual commit messages disappear. Pick whichever fits your team's culture.
git reset: the three-mode ref mover
git reset is surprisingly simple under the hood — it moves the current
branch ref to the commit you specify. The three modes differ only in what else they
touch:
| Mode | Moves branch? | Touches index? | Touches working tree? |
|---|---|---|---|
--soft | Yes | No | No |
--mixed (default) | Yes | Yes (reset to target) | No |
--hard | Yes | Yes | Yes (overwrite) |
So git reset --soft HEAD~3 moves the branch back three commits but
leaves all those changes staged — useful for re-committing the same content as one
consolidated commit. git reset --hard HEAD~3 moves the branch back
three commits and throws away the working-tree changes. The commits
themselves still exist in the object store; you just don't see them through the
branch anymore.
git revert: the non-rewriting alternative
Revert is the polite way to undo. Instead of removing commits from history, it
creates a new commit that applies the inverse patch — if c5
added 12 lines and removed 3, git revert c5 creates a commit that
removes those 12 lines and adds the 3 back.
History grows rather than shrinks. The old commit is still reachable and unchanged. No hashes change, no force-push, no surprises for collaborators. This is the right tool for undoing something you've already shared.
reset
Rewinds the branch. Old commits become orphaned. Destructive if shared.
revert
Adds a new commit that cancels out the old one. Non-destructive, safe to push.
The one rule you can't break
Everything above is safe as long as the commits only exist locally. The moment a
commit has been pushed to a branch that other people are pulling, rewriting history
on that branch forces everyone else into a corner — their local copies of the
branch diverge from the rewritten version, and git pull will either
merge the old commits back in (creating ugly parallel history) or fail.
The golden rule of rewriting
Rewrite your own local history freely. Never rewrite shared branches —
main, develop, anything someone else is working from.
For your own feature branch that only you touch, rewrite all you want and
git push --force-with-lease (safer than --force because
it refuses if the remote has commits you haven't seen).
Try It Yourself — Rewrite Simulator
Start with a small commit history, then run rewriting commands and watch the DAG change. New commits appear, old ones fade to ghosts, and refs move. Every operation is reversible — the reflog tracks it all.
Commit graph
Reflog
What to take away
- You can't edit a commit; "rewriting" always means creating new commits and moving refs.
- Amend = new commit with the same parent. Cherry-pick = new commit with a different parent. Rebase = cherry-pick in a loop.
- Interactive rebase is a scripted todo list over those primitives — reorder, reword, squash, edit, drop.
- Reset moves a ref. Revert adds an inverse commit. Prefer revert on shared history.
- Everything is reversible via the reflog — until you force-push and wait 90 days for GC. So think before you force-push.