reflog - Your Undo Safety Net
git reflog - nothing is ever truly lost
Scenario: You rangit reset --hardand your commits vanished. Panic? No. Git secretly records every move HEAD makes.reflogis the time machine that gets them back.
git reflog lists everywhere HEAD has been - even commits no branch points to anymore.
$ git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~2
9f8e7d6 HEAD@{1}: commit: Add the feature I thought I lost
$ git reset --hard 9f8e7d6 # recover it
| Command | Recovers |
|---|---|
git reflog | History of where HEAD has been |
git reset --hard <hash> | Jump back to a lost commit |
git checkout -b saved <hash> | Rescue a lost commit onto a new branch |
Goal: Internalize this: as long as you had committed the work, git reflog can almost always bring it back - even after a bad reset or a deleted branch. Git keeps these entries for ~90 days.
Tip: The one thing reflog can't save: changes you never committed at all. Commit early, commit often - then you're always recoverable.