Safety & Recovery · 7 min read · Beginner
Reflog & Emergency Rescue
Recover 'lost' commits and accidentally deleted branches using Git's local safety net, git reflog.
What you'll learn
- Inspect every recent HEAD movement with git reflog
- Restore an accidentally reset commit or deleted branch from its reflog hash
- Understand why committed work in Git is almost never truly lost
Accidentally ran git reset --hard or deleted a branch before merging? As long as the work was committed, Git keeps a local journal of every position HEAD pointed to for ~30 days in the reflog.
The 2-Command Rescue
- Run
git reflogto find the hash right before the mistake (e.g.,HEAD@{2}). - Create a rescue branch at that commit:
git checkout -b rescue-branch <hash>.
Fill-in-the-Blank Exercise
Rescue a Commit via Reflog
View recent HEAD history and create a recovery branch at commit f4e3d2c.
git
git checkout -b rescue Interactive Git Sandbox
GitVerify Branch Safety
main
(main) $ git status
On branch main Untracked files: index.html, styles.css
Check Your Understanding
Score: 0 / 1
1. What does git reflog record?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.