Git Reflog: Recover Lost Commits & Undo Mistakes
git reflog
(Reference Log) is a lifesaver when you need to recover lost commits, undo mistakes, or track branch movements.
Unlike git log
, which shows commit history, git reflog
tracks all changes to HEAD, even if commits are lost or branches are deleted.

1. Viewing the Reflog
Run:
Example output:
🔹 HEAD@{0} is the most recent state.
🔹 Each entry shows what happened, such as commits, resets, or rebases.
2. Recovering Lost Commits
If you accidentally reset or deleted commits, find the commit hash using:
Then, reset your branch back to that commit:
3. Undoing a Reset or Checkout
3.1 Undo a Hard Reset
If you ran:
and lost commits, restore them with:
3.2 Undo a Checkout
If you mistakenly switched branches:
4. Using git reflog
with Branches
4.1 Recover a Deleted Branch
If you deleted a branch but need to restore it:
5. Clean Up Reflog
Old entries are automatically deleted, but you can clean them manually:
Conclusion
Action | Command |
---|
View reflog | git reflog |
Recover a lost commit | git reset --hard <commit-hash> |
Undo a reset | git reset --hard HEAD@{1} |
Recover a deleted branch | git checkout -b branch-name <commit-hash> |
Clean old reflog entries | git reflog expire --expire=30.days.ago --all |
🔹 git reflog
is your safety net in Git! Need more details? 🚀