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.
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.
Topics in This Chapter
git add . and something came along that should not have. One command puts it back, and nothing is at risk.