reflog - Your Undo Safety Net

git reflog - nothing is ever truly lost

Scenario: You ran git reset --hard to what you thought was the right commit, and your latest work just vanished from git log. Panic? No. Git has been secretly recording every single move your HEAD pointer has ever made on this machine - including commits that no branch points to anymore. reflog is the time machine that gets them back.

git log only shows commits reachable by following branch and tag pointers backwards. git reflog, by contrast, shows the private, local-only journal of everywhere HEAD has physically pointed over time - every commit, every checkout, every reset, every rebase step - regardless of whether any branch currently "remembers" that commit.

$ 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

Each line's HEAD@{N} is just an index into this journal - {0} is the most recent entry, {1} the one before that, and so on. Once you find the hash of the commit you lost, you can get back to it with git reset --hard <hash>, or more safely, git checkout -b saved <hash> to rescue it onto a fresh branch without touching your current one at all.

CommandRecovers
git reflogHistory of where HEAD has been
git reset --hard <hash>Jump back to a lost commit (careful - discards current uncommitted changes)
git checkout -b saved <hash>Rescue a lost commit onto a brand-new branch, non-destructively
Goal: Internalize this: as long as you had committed the work at some point, git reflog can almost always bring it back - even after a bad reset, a botched rebase, or an accidentally deleted branch. Git keeps these local reflog entries for roughly 90 days by default before old, truly unreachable ones are eventually cleaned up.
Tip: The one thing reflog can't save is changes you never committed at all - reflog only tracks where HEAD pointed, and HEAD only ever points at commits. That's the whole justification, from the very first module, for committing early and often: it's what makes you recoverable.