🔀 reflog = Your Undo Button
You ran git reset --hard to the wrong commit, or a script did it for you, and every uncommitted change plus commits after that point appear gone. Before you recreate hours of work manually, check this first.
🐞 The Problem
git reset --hard HEAD~5 # Meant to undo 1 commit, accidentally undid 5. # git log now shows the older commits -- the 4 you needed look gone.
💡 Why This Is (Usually) Recoverable
git reset --hard moves the branch pointer AND wipes the working directory to match, but it does NOT delete the commit objects themselves. They become "unreachable" (no branch points at them) but stay in Git's object database until garbage collection -- same safety net as reflog.
🔢 Step 1 — Find the Lost Commits
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~5
# f9e8d7c HEAD@{1}: commit: Fix payment validation bug
# ...
# The entry right before your "reset" line is the commit you want back
🔢 Step 2 — Restore It
# Option A: move the branch pointer forward again git reset --hard f9e8d7c # Option B (safer -- doesn't touch working directory): git reset --soft f9e8d7c
📌 If Uncommitted (Staged/Unstaged) Changes Were Lost Too
# git reset --hard wipes uncommitted changes with NO reflog entry -- # these are only recoverable if they were staged at some point: git fsck --no-reflogs --unreachable --dangling | grep commit # Then inspect each dangling blob/commit: git show <hash> # This works ONLY if `git add` was run before the reset -- # truly uncommitted, unstaged changes have no recovery path in Git
📌 The Safer Habit Going Forward
# Before any destructive command, create a safety tag -- costs nothing: git tag safety-checkpoint git reset --hard HEAD~1 # If something goes wrong: git reset --hard safety-checkpoint git tag -d safety-checkpoint # clean up once confirmed fine
reflog turns “I just deleted five commits by accident” from a disaster into a two-command fix — but it can’t save uncommitted work that was never staged, so
git addliberally before running anything destructive.
