I Broke Everything
This is the page to remember. Not the commands — the fact that this page exists.
Something has gone wrong, the terminal is showing a message you have never seen, and work you are sure existed does not appear in git log. The instinct is to type things until it stops. The better move is to know that Git keeps a private record of everywhere your branch has been, including the states you thought you destroyed.
It is called the reflog, and almost nobody tells beginners about it.
What the reflog is
Every time your branch moves — a commit, an amend, a merge, switching branches — Git writes a line in a local log recording where it went. That log is the reflog.
Two things about it matter. It is local: it lives on your machine only, never travels to GitHub, and a fresh copy of a project has an empty one. And it expires: ninety days by default, and thirty for commits that are no longer reachable from any branch — which is exactly the case you would be using it for. It is a safety net for recent accidents, not an archive.
Reading it
git reflog
7f4b2a9 HEAD@{0}: commit (amend): Add the sightings list
c8d1e07 HEAD@{1}: commit: Add the sightings list
5b2a9f3 HEAD@{2}: commit: Add basic styling for the home page
Each line has a commit, a position marker, and what moved you there. Read down the list until you find the moment before things went wrong. Here, HEAD@{1} is the commit that the amend replaced — the one that is no longer in git log at all.
Getting back to a state
Once you have a hash, you can go and look at it safely:
git switch --detach c8d1e07
Git prints a paragraph about being in detached HEAD state. In plain terms: you are visiting a commit rather than working on a branch. Look around, open the files, confirm this is the state you wanted.
Older tutorials write this as git checkout c8d1e07, which does the same thing. The way home is the same in both cases:
git switch main
If the state you visited is one you want to keep, the safe move is to make a branch from it — which is Chapter 6, two chapters from now. Until then, the important part is that the commit still exists and you can find it.
What it cannot do
The reflog records where your branch has been, which means it records commits. Work that was never committed — the file you edited and then discarded with git restore — never entered it and cannot be recovered.
That is the honest shape of safety in Git: committed work is very hard to lose, and uncommitted work is easy to lose. Everything this book says about committing often comes from that one sentence.
The calm procedure
When something goes wrong, in this order:
Stop typing. Nothing gets worse while you read, and most work that is genuinely lost is lost in the panicked commands after the mistake rather than in the mistake itself.
Run git status. Which branch, and which of the three places holds what.
Run git log --oneline -5 and git reflog. What the history thinks happened, and where your branch has actually been.
Then decide. By this point the situation is usually one of the five on the chapter's opening diagram, and the command follows from the diagnosis.
Chapter 10 turns this into a one-page decision tree you can come back to. For now, the sentence worth memorizing is the first one: stop typing, and look.
- "If it is not in
git log, it is gone." The log shows the current chain. The reflog shows everywhere your branch has been, including commits no longer on any branch. - "The reflog is on GitHub too, so a colleague can find my commit." It is local and private to each copy. A fresh clone has an empty reflog, which is why recovery happens on the machine where the mistake was made.
- "The reflog keeps everything forever." Entries expire — ninety days by default, and only thirty for a commit no longer reachable from any branch, which is the kind you are usually hunting for. It rescues recent accidents, not old ones, and it is not a backup.
- "Detached HEAD means something is broken." It means you are visiting a commit instead of standing on a branch. Nothing is wrong, and
git switch mainis the way back.
- Knowing the reflog exists changes how you work: experiments stop being frightening once committed work is genuinely hard to lose, and fear is what keeps beginners from using branches at all.
- It closes the chapter honestly. Every undo here is recoverable except the one that was never committed, and now you know exactly which one that is.
- "Stop typing and look" is the single most valuable habit in this chapter, and it is the one that costs nothing to acquire.
Knowledge Check
What does the reflog record?
- Every position your branch has occupied, including states no longer in the log
- Every file you have edited since the repository was first created
- Every command you have typed, so that any of them can be run again
- Every change pushed to GitHub, so that pushes can be traced afterwards
Which kind of lost work can the reflog NOT recover?
- Changes that were edited and discarded without ever being committed
- Commits made on a branch that has since been deleted from the project
- Commits that were replaced by an amend a few minutes earlier
- Commits made before the most recent push to a remote copy
Git says you are in "detached HEAD" state. What does that mean?
- You are visiting a commit directly rather than working on a branch
- The repository is damaged and the current branch cannot be located
- Your commits have been separated from their parents and the chain is broken
- The connection to the remote copy has been lost and must be re-established
What are the first two things to do when something goes badly wrong?
- Stop typing, then run
git statusto find out where things stand - Run
git reset --hardto return the project to a known good state - Delete the repository and clone a fresh copy from the remote to start again
- Make a copy of the whole folder, then start trying commands on the original
You got correct