You accidentally ran git reset --hard and erased the last three commits. Or you ran git branch -D feature/important and accidentally deleted an entire week of work. In almost every other tool, your work would be gone forever. In Git, you have git reflog—the ultimate developer safety net.
While git log shows the public chronological commit history of the current branch, git reflog (Reference Log) records every single movement of the HEAD pointer on your local machine. Every time you switch branches, commit, rebase, reset, merge, or cherry-pick, Git writes a permanent entry to the local reflog!
$ git reflog
8a2f1b4 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~1
4f8e2a1 HEAD@{1}: commit: feat: add Stripe payment webhook
7d1935a HEAD@{2}: checkout: moving from feature/auth to main
e69de29 HEAD@{3}: commit: fix: validate JWT expiration
b5b7b99 HEAD@{4}: rebase (finish): returning to refs/heads/main
HEAD@{0}: Where HEAD is right now.HEAD@{1}: Where HEAD was 1 step ago.HEAD@{2}: Where HEAD was 2 steps ago.HEAD@{yesterday} or HEAD@{2.hours.ago}: Time-based lookup!git reset --hardSuppose you accidentally ran git reset --hard HEAD~1 and wiped out commit 4f8e2a1:
# Step 1: Check reflog to find the commit hash before the reset:
$ git reflog
8a2f1b4 HEAD@{0}: reset: moving to HEAD~1
4f8e2a1 HEAD@{1}: commit: feat: add Stripe payment webhook
# Step 2: Reset HARD back to that commit!
$ git reset --hard HEAD@{1}
HEAD is now at 4f8e2a1 feat: add Stripe payment webhook
Every single line of your code and files is instantly restored!
Suppose you deleted a branch with unmerged commits: git branch -D feature/ai-chat:
git reflog and locate the last commit made on that branch (e.g. e69de29).$ git branch feature/ai-chat e69de29
git switch feature/ai-chat. Your entire deleted branch is back!By default, Git preserves orphaned, unreferenced commits in .git/objects/ and reflogs for 30 days, and referenced commits for 90 days before git gc (garbage collection) purges them. You have plenty of time to recover any mistake!
Strengthen your version control mastery by studying these connected classes:
Use the reflog to restore commits dropped by accidental git reset --hard calls.
Roll back messy interactive rebases to the exact pre-rebase HEAD position.
Inspect how Git records reference updates inside the .git/logs/ folder.
Recover deleted local branches and uncommitted changes like a senior engineer.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to git bisect Debugging → Previous: ← git cherry-pick | Full Syllabus