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.

Before amend: c1 c2 c3 main After amend: c1 c2 c3 c3' main c3 orphaned, still in reflog

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).

$ git cherry-pick 3e8f4c7 [main 9b2c1f4] add user validation Author: alice <[email protected]> Date: Mon Mar 3 14:32:11 2026 -0500 1 file changed, 12 insertions(+)

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:

  1. Find the set of commits on the current branch that aren't on main (the main..HEAD set).
  2. Check out main (detached HEAD).
  3. Cherry-pick each of those commits, in order, onto the new base.
  4. Update the original branch ref to point at the new tip.
Before rebase feature onto main: m0 m1 m2 main f1 f2 feature After: m0 m1 m2 f1' f2' main feature (f1, f2 orphaned)

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:

pick 3e8f4c7 add user validation reword 1b2c3d4 refactor auth module squash 7a9f31e fix typo edit af5626b implement reset-password flow drop c3b8f4e remove stray console.log

The verbs

VerbWhat it does
pickApply the commit as-is.
rewordApply it, then open the editor so you can rewrite the commit message.
editApply it, then pause so you can amend the commit (add/remove files, split it).
squashCombine with the previous commit; merges both messages.
fixupLike squash but silently discards this commit's message.
dropSkip 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:

  1. Interactive rebase with squash/fixup — merge N commits into 1 before opening the PR.
  2. git merge --squash feature — doesn't create a merge commit; stages all the branch's changes so you can commit them as one.
  3. "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:

ModeMoves branch?Touches index?Touches working tree?
--softYesNoNo
--mixed (default)YesYes (reset to target)No
--hardYesYesYes (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.