Recover with Reflog
Reflog is a diary of every move the branch pointer made. It finds snapshots the log no longer shows.
Read the diary
The coffee snapshot sits at HEAD@{4}. The reset that lost it sits above.
$ git reflog -6Return to the lost snapshot
The hash comes from the diary. The branch moves back to it.
$ git reset --hard b483f8dThe snapshot is back
The coffee snapshot is newest again.
$ git log --onelineExample
$ git reflog -6
$ git reset --hard b483f8dKey points
- The log shows the snapshots on the branch now.
- Reflog shows every place the pointer has been.
- Each line shows a hash, a step number, and what happened.
- HEAD@{0} is the newest move. Higher numbers are older moves.
- Reset with --hard to a hash returns the branch to that snapshot.
- --hard throws away unstaged changes, so check first.
- Reflog keeps moves for weeks, even after a reset.
Here, the diary shows the coffee snapshot at HEAD@{4}. Reset returns the branch to it. The snapshot the log had lost is newest again.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does reflog list?