
Module 10 Lesson 3: The git reflog for disaster recovery
Nothing is ever truly lost. Master the 'git reflog'—the secret diary that tracks every move you make in Git, allowing you to recover 'deleted' branches and abandoned commits.
Module 10 Lesson 3: The git reflog for disaster recovery
We’ve all been there: You ran a git reset --hard by mistake. You deleted a branch that you thought was merged, but it wasn't. You was in the middle of a rebase and everything went wrong.
You think your work is gone forever. It isn't.
In this lesson, we learn about the Reference Log (reflog)—Git's ultimate "Safety Net."
1. What is the Reflog?
While the git log shows you the history of the commits in a branch, the git reflog shows you the history of your actions.
Every time you:
- Switch branches
- Make a commit
- Amend a commit
- Pull from a server
- Reset a branch
Git records that action in a local, private list. This log is not shared with your team; it only exists on your machine.
2. Navigating the Reflog
To see your recent history:
git reflog
Reading the output:
8f3d12a HEAD@{0}: reset: moving to HEAD~1
7a9b4c2 HEAD@{1}: commit: Add database logic
a1c2b3d HEAD@{2}: checkout: moving from main to feature-x
The numbers in the brackets (HEAD@{0}, HEAD@{1}) tell you how many steps ago that action happened.
3. The Resurrection: How to Recover
Imagine you accidentally "deleted" the commit 7a9b4c2 (Add database logic) by resetting. To bring it back:
# 1. Find the ID in the reflog
# 2. Reset back to it
git reset --hard 7a9b4c2
Or, if you accidentally deleted a branch, you can recreate it at the exact commit it was at before the deletion:
git branch feature-restored 7a9b4c2
graph TD
Mistake["Accident (Reset / Delete)"]
Reflog["git reflog"]
ID["Find Commitment SHA-1"]
Restore["git reset --hard [SHA-1]"]
Mistake --> Reflog
Reflog --> ID
ID --> Restore
Restore --> Happy["Project Recovered!"]
4. The Time Limit
The reflog is not infinite. Git periodically cleans it up (garbage collection). By default, reflog entries are kept for:
- 90 days for reachable commits.
- 30 days for unreachable (abandoned) commits.
So, if you make a mistake, you have about a month to realize it and fix it using the reflog!
Lesson Exercise
Goal: The Controlled Disaster.
- Go to your
git-practicerepo. - Make a change to a file and commit it.
- Now, run
git reset --hard HEAD~1to "delete" that commit. - Run
git log. Observe that the commit is gone. - Run
git reflog. Find the line where you made the commit (it should look likecommit: Message...). - Run
git reset --hard <SHA-1>from that line. - Run
git logagain. Is the commit back? (It should be!).
Observation: You'll realize that as long as you have committed your code at least once, Git effectively makes it impossible to lose your work on accident.
Summary
In this lesson, we established:
git reflogtracks every pointer movement on your local machine.- It is your primary tool for recovering after a
reset --hardor a branch deletion. - It is local and temporary (30-90 days).
- Knowing the reflog exists significantly reduces "Git Anxiety."
Next Lesson: Want Git to automatically test your code before you commit? Welcome to Lesson 4: Git hooks: Automating your local environment.