The Reflog

How to use `git reflog` as your ultimate safety net to recover lost commits and branches.

Version Control6 min readConcept 21 of 29

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.

  1. 1What does `git reflog` primarily track?
  2. 2You just ran `git reset --hard HEAD~1` and accidentally removed a commit you desperately needed. How can you get it back?
  3. 3Which of the following statements about `git reflog` is true?

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.