The Reflog
How to use `git reflog` as your ultimate safety net to recover lost commits and branches.
The git reflog command
git reflog (Reference logs) is a local mechanism that tracks updates to the tip of branches and the HEAD pointer.
It acts as a chronological, hidden diary of every action that changes where your local HEAD points (e.g., commits, checkouts, resets, merges, and rebases).
Why it matters
It acts as your ultimate safety net.
If you accidentally delete an unmerged branch, mangle a complex rebase, or perform a destructive git reset --hard that removes code you actually needed, those commits are not instantly gone. The reflog lets you find the hash of the 'lost' state and restore your work exactly as it was.
How it works
The reflog maintains a hidden diary of every time HEAD moved locally.
When you run a command that moves HEAD, Git logs the old commit hash, the new commit hash, and a brief description of the action.
You can view this timeline using git reflog, identify the specific step where things went wrong, and use git reset --hard <hash> to travel back to the exact commit hash before the mistake occurred.
Recover a previous branch position
Inspect local reflog entries and choose the previous commit to recover.
$ git reflog
a11bc00 HEAD@{0}: reset to initial setup
a12bc34 HEAD@{1}: commit Add navbar
a11bc00 HEAD@{2}: commit Initial setupWorking page
Initial page without a navbar
main is at a11bc00 after the reset.
The selector shows the saved reflog at the start of this exercise. Recovering adds new reference movements in a real repository.
Reflogs record local reference movements and can help locate commits no longer reached by a branch. This exercise creates a new recovery branch; it does not rewrite main. Reflog entries can expire. Local simulation.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
Remember this
- The reflog tracks every time your local HEAD moves.
- It is your ultimate safety net for recovering 'deleted' commits or branches.
- Reflog data is strictly local and never pushed.
- It only recovers committed code, not uncommitted changes.
Done with this concept?
Mark it complete to track your progress. No login needed.