Chapter Four · Recovery

Undoing Things

The chapter this book is built around, organised by where the change is sitting. Discard, unstage, amend, revert — and the reflog, the private record of everywhere your branch has been, which is why a committed mistake in Git is almost never fatal.

5 topics

This is the chapter the book is built around. Not because undoing is the most important thing Git does, but because "I have broken something and I am afraid to touch it" is what actually stops beginners — and the fix is knowledge, not courage.

The chapter is organized by where the change is sitting, using the three places from Chapter 2. That is deliberate: the first move in any Git accident is not to type something, it is to work out which of the three places you are dealing with. Once you know that, the command follows.

Five topics, in order of how far the change has travelled. A change you have not committed. A change you staged by mistake. A commit you made too early. A commit that is already in the history and possibly on someone else's machine. And finally the reflog: Git's private record of everywhere your branch has been, which is the reason a committed mistake is almost never fatal, and which almost nobody tells beginners about.

Each page says plainly whether it destroys anything. Exactly one of them does.

Where is the change? The answer decides the command
Edited, never staged — and you want it gonegit restore · destroys the change
Staged by mistake, contents are finegit restore --staged · safe
Committed seconds ago, nothing pushed yetgit commit --amend · safe here
Committed and possibly shared with othersgit revert · adds the opposite
No idea what happened or where anything wentgit reflog · the safety net

Topics in This Chapter