Recover commits, branches, and lost work using Git's hidden history.
git reflog shows:
A log of all HEAD movements (your Git activity history)
Even if you:
- delete a branch ❌
- reset commits ❌
- lose changes ❌
👉 Reflog can recover them.
flowchart LR
A[Commits] --> B[HEAD Moves]
B --> C[Reflog Records]
C --> D[Recover Lost Work]
- commits
- checkouts
- resets
- rebases
- merges
git refloga1b2c3 HEAD@{0}: commit: fix bug
d4e5f6 HEAD@{1}: checkout: moving to feature
g7h8i9 HEAD@{2}: commit: add feature
Reflog is:
Local only
Temporary (expires)
Hidden safety net
1. You commit work
2. You accidentally run git reset --hard
3. Work seems gone
4. Use reflog → recover commit
git reflogHEAD@{2}
git checkout HEAD@{2}OR:
git reset --hard HEAD@{2}Git stores:
HEAD movements → reflog entries
HEAD → current commit pointer
Every time HEAD moves:
👉 Reflog records it
Commit A → Commit B → Commit C
↑
HEAD moves
Reflog tracks all moves
git reflogFind commit → recreate branch:
git checkout -b recovered-branch <commit-hash>git reset --hard HEAD~2Oops 😱
Recover:
git reflog
git reset --hard HEAD@{1}Not shared with remote.
default ~90 days
Many think work is lost forever.
Reflog helps recover.
- use reflog for recovery
- act quickly before expiration
- combine with log for clarity
- understand HEAD movement
| Reflog | Log |
|---|---|
| shows history of HEAD | shows commit history |
| includes lost commits | only reachable commits |
Tracks HEAD movement history.
Yes (if not expired).
No, it is local.
Log shows commits, reflog shows actions.
# create commit
echo "test" >> file.txt
git commit -am "test commit"
# reset
git reset --hard HEAD~1
# recover
git reflog
git reset --hard HEAD@{1}Reflog is your safety net.
It allows you to:
- recover lost commits
- undo mistakes
- debug history
➡️ 04-git-bisect.md