CurriculumAdvanced Gitgit reflog — Recovering Lost Work
Lesson 5 of 683%
Advanced9 min+90 XP

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.

Commit Timeline
a1b2c3d
init
b2c3d4e
feat
c3d4e5f
fix
d4e5f6a
HEAD

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.

Git's Three Areas
📁
Working Dir
Your files
→ git add
📋
Staging Area
Prepared
→ git commit
🗄️
Repository
.git/ history

Commands

commands — click to reveal output
$
git reflog
Show all recent HEAD movements
▼ show output
$
git reflog --all
Show reflog for all branches (not just HEAD)
$
git checkout d4e5f6a
Jump to the "lost" commit to inspect it
$
git checkout -b recovered-work d4e5f6a
Create a branch to permanently save the recovery
▼ show output
$
git reset --hard HEAD@{2}
Reset back to 2 positions ago using reflog reference directly
KNOWLEDGE CHECK

What makes the reflog so powerful for recovery?

Rate this lesson:
PreviousNext Lesson