git reflog — Recovering Lost Work
The reflog (reference log) records every position HEAD has pointed to — every commit, switch, reset, and rebase. It is your ultimate safety net. Even after git reset --hard or a bad rebase, you can almost always recover your work using reflog.
01.How the Reflog Works
Git keeps a local log of every place HEAD has been for approximately 90 days. Entries are numbered HEAD@{0} (now), HEAD@{1} (previous), HEAD@{2} (before that), and so on. After a git reset --hard that "destroyed" commits, those commit objects still exist in Git's object database — they are just unreferenced. Reflog finds them.
02.Recovery Workflow
Scenario: you ran "git reset --hard HEAD~3" and lost important commits. Step 1: git reflog — find the entry showing HEAD BEFORE the reset. Step 2: Copy that commit hash (e.g., d4e5f6a HEAD@{1}: commit: Add awesome feature). Step 3: git checkout -b recovery d4e5f6a — creates a new branch at that point. Step 4: Merge or cherry-pick back to where you need it.
Commands
What makes the reflog so powerful for recovery?