Loading lessons...
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:
bash
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:
bash
git reflog
Find the hash of the commit you want back, then reset to it:
bash
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:
bash
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 reflogrecords every move of HEAD.- It helps recover after
reset - hardor branch deletion. - Reset or recreate branches to the hash found in the reflog.
- Reflog is local and temporary, so push important work.