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.
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.