Undo and the Reflog
Every git user knows the moment: a commit made too early, a reset aimed one commit too far, and the cold thought that work is gone. Usually it is not — git keeps a diary. This chapter is about lazygit's undo key, which walks that diary for you, and about what the diary can and cannot hold. The outcome: you can undo a commit and watch your changes come back staged; you know what a redo costs; and you know the boundary — the work the diary never saw — on the far side of which chapter 1's rule is the only protection.
The diary underneath: the reflog
Git quietly records every position each branch has ever pointed at: commits, checkouts, merges, resets, in order, with times. This is the reflog, and it is why "lost" commits are usually just unlabelled ones — the commit object still exists, and the reflog remembers its hash after every branch has moved on. Entries are kept for weeks by default (90 days for reachable ones, 30 for unreachable, in git's standard settings), so the diary covers every accident you are likely to notice having.
lazygit's undo does not add magic to this. It reads the same diary and offers to reset the branch back along it — and, to its credit, it shows you the exact reset before performing it.
Undo: z
The lab's demonstration is the strongest
evidence in the guide, because it was
checked with git at both ends
(transcripts lazygit-lab/transcripts/t13-undo.txt
and t14-undo-redo.txt). The lab stashed
and recovered its changes, committed
everything as "A commit to undo", and
pressed z.
lazygit answered with a confirmation that names its whole intention:
Are you sure you want to soft reset to 'ecd9d15f'?
Nothing had happened yet. Soft reset is the gentlest of chapter 10's resets: the branch pointer moves back to the named commit, and everything the undone commit contained stays in your index, staged, exactly as it was the moment before you committed it. Confirm with Enter, and the command log shows the deed in plain git:
git reset --soft 91547d3d4cb7e9447cbd10dedf487ddaa47b575a
Afterwards, git agreed with the screen:
git log no longer listed the commit,
and git status showed its three files
staged again — A shopping.txt,
M summer-walk.txt, A winter-walk.txt.
Undoing a commit, it turns out, is not
time travel. It is a reset along the
diary to one step back, with your work
handed back to you in the index.
Repeated z presses offer to walk
further back along the diary — each
with its own confirmation, each naming
its target — through checkouts and
merges as well as commits, because the
diary records them all. The lab walked
one step and verified it at both ends.
Redo: Z, and its price
Undo is not a one-way door. Press Z
(shift) and lazygit offers the way
forward again — but read its confirmation
closely, because the terms change:
Are you sure you want to hard reset to 'b4663588'? An auto-stash will be performed if necessary.
A hard reset — the branch moves
forward to the diary entry, and the
working tree is made to match it, which
can overwrite uncommitted changes. That
is why the sentence about the auto-stash
is there: if your tree has work that the
hard reset would clobber, lazygit stashes
it first, automatically, so the redo does
not eat it. The lab captured this offer
and, its point about the terms being
different now rather than in a fortnight's
accident, deliberately did not press
Enter; the redo's execution is the
documentation's account, and
criteria.md says so.
The asymmetry is the lesson. Undo is soft and asks politely; redo is hard and asks carefully. The further you intend to walk the diary in either direction, the more chapter 1's rule applies: a commit — even a throwaway one — is the cheapest insurance ever sold.
The boundary of the diary
Everything in this chapter works because the things being undone were commits: objects git stored, in a diary git keeps. Two kinds of loss live outside the diary's reach:
- Uncommitted work. An edit never staged or committed exists in no object and no diary. Chapter 10's discard key destroys it outright; no key in this program, and no command in git, brings it back. This is the rule from chapter 1, and it has no exceptions.
- The diary's own expiry. Reflog
entries age out, and
git gccollects unreferenced objects. A commit lost last month is an archaeology problem; a commit lost this morning is azpress. Undo early, while the diary is fresh.
Between those boundaries lies most of a working life with git, and in that whole territory the message of this chapter is the one the lab verified twice over: press the key, read the confirmation — it names a real command and a real hash — and the way back is real.