History Surgery
Everything before this chapter accepted history as it stood. This one rewrites it: correcting a message, folding a fix-up commit into the work it fixed, dropping a commit that should never have landed. Git calls the machinery interactive rebase, and on the command line it is a text file of instructions you edit blind. In lazygit it is a set of keys on the Commits panel, each naming the operation it performs on the commit in front of you. The outcome: you can reword a commit — run for real by the lab — and you know what the squash, fixup, drop and edit keys set in motion, and the one question to ask before using any of them: has anyone else got this history yet?
The keys, from the hint bar
Focus the Commits panel (4) and the
hint bar lists the surgeon's instruments:
Squash: s | Fixup: f | Reword: r | Drop: d | Edit: e | Amend: A | Checkout: <space> | …
Each acts on the selected commit, and each is, under the skin, an interactive rebase over the commits from that point to the head of the branch — which is why chapter 7 insisted you know which end of the list is the foundation. Operating on an old commit rewrites every commit after it: their contents survive, their hashes — their identities — do not.
Reword: run for real
r is the gentlest cut: same commit,
new words. The lab selected the top
commit, "Add summer walk notes", pressed
r, and an editor — the same boxes as
chapter 5's commit editor — opened with
the existing message in place. It added
" (updated)", pressed Enter, and the
commit reappeared in the panel with the
longer subject.
The verification is the chapter's whole
argument in one command. Before and
after, from git's own log (transcript
lazygit-lab/transcripts/t05-history.txt):
before: 710e1948 Add summer walk notes
after: 4fecdb5 Add summer walk notes (updated)
Same parent, same contents, new hash. The message is part of the commit, so changing the message changes the commit — surgery in miniature, with the scar visible. Nothing else in the repository moved, because nothing after the top commit existed to be rebuilt.
The rest of the tray
s— Squash. Fold the selected commit into the one below it (its parent), combining the two into a single commit. The classic use: a string of "draft, more draft, typo" commits becomes the one clean commit they were always meant to be. lazygit asks how to treat the two messages — the merged commit needs words — and then runs the rebase.f— Fixup. Like squash, but the selected commit's message is discarded outright; it exists only to correct its parent ("fix the count, properly this time"), and its parent's message survives alone.d— Drop. The commit is removed from history entirely, its changes vanishing from the branch (they remain in the reflog, chapter 8, until it expires). Behind a confirmation, as befits the tray's sharpest tool.e— Edit. The rebase stops at the selected commit: the branch is checked out there, mid-history, so you can amend the commit with ordinary work — change files, stage,Ato amend — and then continue the rebase from the menu lazygit leaves open, replaying the later commits on top of your amended one.A— Amend. Not a rebase: the selected commit — in practice the latest — absorbs whatever is currently staged, keeping or editing its message. The "one more file" commit-fixer, also on the Files panel (chapter 5).
The lab ran reword end to end and read
the rest from the hint bars and the ?
lists, where every one of these keys and
its description appears; a multi-commit
rebase — several of these operations in
one pass, with its todo list and its
continue/abort menu — is documented
machinery, filed as such in
criteria.md. What transfers from the
one verified cut is the shape of all of
them: select, key, read the program's
question, confirm, and check the hashes.
Moving commits bodily
Two more instruments, found in the
sub-views of chapter 7 and named here
for completeness: cherry-pick — copy
a commit from where it sits (C, "Copy
(cherry-pick)", in a branch's commit
list, where the lab read it) and paste
it onto your current branch — and
revert (t on a commit), which adds
a new commit that undoes an old one,
history intact. Revert is the one
operation in this chapter that is safe
on shared history, precisely because it
rewrites nothing.
The question that governs the chapter
Every tool here except revert changes hashes, and a changed hash is a changed commit as far as every other copy of the repository is concerned. So the chapter's rule, which is chapter 12's from the other side:
Rewrite history that only you have. The moment a commit has been pushed and pulled by someone else, its hash is a promise; rewrite it and every copy of the project disagrees about the past.
Your own unpushed branch is a notebook: squash it, reword it, drop its false starts, and push the clean story. The shared branch is a published book: correct it with new commits — revert, amend by addition — or coordinate the rewrite with everyone holding a copy, knowingly, as the deliberate exception. lazygit will not stop you either way; it shows confirmations, not judgements. The judgement is this paragraph, and the reflog of chapter 8 is the insurance that makes your own side of the line a safe place to practise.