Reflog for Recovery
Discover `git reflog` as your safety net for recovering lost commits, branches, or accidental resets.
Reflog for Recovery is a free Git Advanced: Monorepo, Submodules & Workflows lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Git Advanced: Monorepo, Submodules & Workflows learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Your Git Safety Net
git reflog is a journal of every move your HEAD makes — commit, checkout, merge, rebase, reset. It's a powerful safety net for recovering lost work.
Git's Head & Reference Logs
HEAD points to your current position, and the reflog records everywhere it's been. It lives locally on your machine — not in the shared repo history.
Viewing Your Reflog
Run git reflog to see each action indexed as HEAD@{0}, HEAD@{1}, and so on — with the commit hash, the move, and a timestamp.
git reflogDeciphering Reflog Output
Read reflog output as a stack: HEAD@{0} is your current state, HEAD@{1} the one before it. Each entry marks where HEAD changed.
Recovering from a Hard Reset
Accidentally ran git reset --hard HEAD~2? Don't panic — those commits aren't gone, just unreachable. The reflog still remembers where HEAD was before the reset.
Using Reflog to Undo Resets
Find the commit before your bad reset in git reflog, then recover it with git reset --hard <hash> — or use the index directly, like HEAD@{2}.
Rescuing a Deleted Branch
Deleted a branch with git branch -D? Its commits still live in your reflog, since HEAD's movements onto that branch were all recorded.
Recreating a Lost Branch
Find the deleted branch's last commit in the reflog, then recreate it: git branch my-feature <hash>. Check it out and you're back in business.
Reflog Expiration
Reflog entries don't last forever: reachable ones expire after 90 days, unreachable ones after 30. It's a safety net, not a backup — always push important work.
Reflog Recovery Quiz
You accidentally ran git reset --hard HEAD~3 and lost your last three commits. You check git reflog and see:
a1b2c3d HEAD@{0}: reset: moving to HEAD~3
e4f5g6h HEAD@{1}: commit: Final bug fix
f7e8d9c HEAD@{2}: commit: Implement new feature
i7j8k9l HEAD@{3}: commit: Initial setupWhich command would restore your repository to the state before the reset, bringing back "Final bug fix" and "Implement new feature"?
Reflog: Your Git Lifesaver
Recap: git reflog logs everywhere HEAD has been, your first defense against bad resets and lost branches. Recover with git reset --hard HEAD@{n} — but it does expire.
Frequently asked questions
Is the “Reflog for Recovery” lesson free?
Yes — the full text of “Reflog for Recovery” is free to read here on the web, and the Git Advanced: Monorepo, Submodules & Workflows course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Git Advanced: Monorepo, Submodules & Workflows course, upgrade to CoddyKit PRO.
What will I learn in “Reflog for Recovery”?
Discover `git reflog` as your safety net for recovering lost commits, branches, or accidental resets. You practise Git Advanced: Monorepo, Submodules & Workflows with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Git Advanced: Monorepo, Submodules & Workflows?
No prior experience is required. Git Advanced: Monorepo, Submodules & Workflows on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reflog for Recovery” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Git Advanced: Monorepo, Submodules & Workflows lesson?
Yes. Every Git Advanced: Monorepo, Submodules & Workflows lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.