Lesson 26 +20 XP

Git Reflog and Recovery

Git Reflog and Recovery

Reflog is Git's safety net. It records every move of your HEAD pointer, even the mistakes.

What is the reflog?

git reflog logs where your HEAD has been:

git reflog

It shows recent operations like commits, resets, and checkouts, each with a hash. If you lose work with a bad reset --hard, the reflog often still knows where that work was.

Example recovery

Say you accidentally did git reset --hard HEAD~3 and lost 3 commits. Look at the reflog:

git reflog

Find the hash of the commit you want back, then reset to it:

git reset --hard <hash>

Your work is back.

Recover a deleted branch

git branch -D deletes a branch, but the reflog remembers its commits. Find the last commit of that branch in the reflog, then recreate the branch:

git branch recovered-branch <hash>

Important caveat

The reflog only survives for a while (commits are garbage collected eventually) and it is local, so recovery is not guaranteed forever. Back up your important work with pushes.

TL;DR

  • git reflog records every move of HEAD.
  • It helps recover after reset --hard or branch deletion.
  • Reset or recreate branches to the hash found in the reflog.
  • Reflog is local and temporary, so push important work.