Git Reflog & Recovering History
Master `git reflog` to recover lost commits or branches, providing a safety net for your work.
Git Reflog & Recovering History is a free DevOps Bootcamp lesson on CoddyKit — lesson 1 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 DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Git Reflog?
Ever accidentally delete a branch or reset to the wrong commit? Don't panic! Git has a powerful safety net called the Reflog.
The reflog (reference log) is a record of where your HEAD and branch pointers have been in your local repository.
How Reflog Tracks History
Unlike the commit history (git log) which tracks changes in your project files, the reflog tracks changes to your repository's references.
Every time your HEAD (your current commit) or a branch pointer moves, Git records this event in the reflog. This includes:
- Committing changes
- Switching branches
- Merging or rebasing
- Performing a reset
- Cloning a repository
Viewing Your Reflog
To see your reflog, you simply use the git reflog command. It shows a list of actions and the state of your HEAD at that time.
Let's simulate some actions and then view the reflog:
git init my_repo
cd my_repo
echo "First line" > file.txt
git add file.txt
git commit -m "Initial commit"
echo "Second line" >> file.txt
git add file.txt
git commit -m "Add second line"
git reflogDecoding Reflog Entries
Each reflog entry has a specific format. Let's break down a typical entry you might see:
a1b2c3d HEAD@{0}: commit: Add new feature
a1b2c3d: The SHA-1 hash of the commit whereHEADwas at that time.HEAD@{0}: This is the "reflog entry pointer".@{0}is the most recent state,@{1}is the one before that, and so on.commit: Add new feature: A description of the action that causedHEADto move.
These pointers are crucial for recovery!
Scenario: Recovering a Lost Commit
Imagine you made a commit, then accidentally undid it with a git reset --soft HEAD~1. Your commit is no longer in your current branch history, but it's not truly gone!
The reflog remembers it. Let's simulate this common mistake:
git init my_project_recovery
cd my_project_recovery
echo "Initial content" > start.txt
git add start.txt
git commit -m "Initial commit"
echo "Important feature" > feature.txt
git add feature.txt
git commit -m "Added an important feature"
# Oops! Accidentally reset
git reset --soft HEAD~1
git reflogBringing Back the Commit
After the reset, if you run git reflog, you'll see an entry for your "Added an important feature" commit, perhaps as HEAD@{1} or similar.
To recover it, you can use git reset with the reflog entry. For example, if the lost commit's hash was abcdefg or its reflog entry was HEAD@{1}:
# Assuming the lost commit was HEAD@{1} in the reflog
git reset --hard HEAD@{1}
# Or using the specific commit hash from reflog
# git reset --hard abcdefg
# Now, check your log. The commit is back!
git log --onelineScenario: Retrieving a Deleted Branch
What if you accidentally deleted an entire branch before merging it, or thought you didn't need it anymore?
Branches are just pointers to commits. If you delete a branch, the commits it pointed to aren't immediately removed from your repository. The reflog still remembers where that branch's HEAD was!
Restoring a Branch from Reflog
Let's say you had a branch named feature-x, worked on it, and then deleted it with git branch -D feature-x.
When you run git reflog, you might see an entry like:
1234567 HEAD@{5}: branch: Created branch feature-x orabcdefg HEAD@{2}: checkout: moving from feature-x to master
You can use the commit hash (e.g., 1234567 or abcdefg) from the reflog entry where the branch was active to recreate it:
# Assume 'feature-x' was deleted and its last commit was '1234567'
# Create a new branch pointing to that commit
git branch feature-x-recovered 1234567
# Now, check out your recovered branch
git checkout feature-x-recovered
# Verify its history
git log --onelineReflog Entries Expire
While the reflog is a powerful safety net, its entries don't last forever. Git prunes old reflog entries after a certain period.
- By default, reachable entries (from your current
HEAD) expire after 90 days. - Unreachable entries (e.g., from deleted branches or commits no longer referenced) expire after 30 days.
This means it's best to recover "lost" work sooner rather than later!
Reflog Recovery Challenge
You created a new feature branch, made several commits, and then accidentally deleted it without merging. You run git reflog and see this entry:
...
c0ffee1 HEAD@{3}: commit: Implement new user profile page
bada55e HEAD@{4}: checkout: moving from feature/profile to main
...Which command sequence would correctly recreate your feature/profile branch, pointing to the latest commit it had before deletion?
Reflog: Your Git Safety Net
You've mastered git reflog, a crucial tool for recovering "lost" work in your local Git repository. Remember:
- It tracks where
HEADand branch pointers have been. - Use
git reflogto view this history. - You can recover commits or branches using
git resetorgit branchwith reflog entries (HEAD@{n}or commit hashes). - Reflog entries expire, so act quickly if you need to recover something!
Keep your Git history clean, but know that reflog is there for emergencies!
Frequently asked questions
Is the “Git Reflog & Recovering History” lesson free?
Yes — the full text of “Git Reflog & Recovering History” is free to read here on the web, and the DevOps Bootcamp 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 DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Git Reflog & Recovering History”?
Master `git reflog` to recover lost commits or branches, providing a safety net for your work. You practise DevOps Bootcamp 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 DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Git Reflog & Recovering History” 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 DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp 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.
All lessons in this course
- Git Reflog & Recovering History
- Git Bisect for Debugging
- Repository Maintenance & Housekeeping
- Rewriting History with git filter-repo