π΅οΈ Git Reflog β Recover Lost Commits and Track HEAD Movements
π§² Introduction β Why Use Git Reflog?
Accidentally deleted a branch? Ran a bad reset or force push and lost some work? π¨ Donβt panicβGit Reflog can save the day.
git reflog
is a powerful Git tool that lets you track every change made to the HEADβeven changes that are not visible in Git history or branches. It’s your time machine to restore lost commits, branches, and work-in-progress.
π― In this guide, youβll learn:
- What
git reflog
is and how it works - How to recover deleted commits or branches
- Reflog syntax and real examples
- Best practices and common mistakes to avoid
π What Is git reflog
?
git reflog
(short for reference log) records every movement of the HEAD (e.g., commits, resets, checkouts, rebases), even if those commits are no longer referenced by a branch.
β It helps you:
- Restore lost commits
- Recover deleted branches
- Undo bad
reset
,rebase
, orcheckout
operations
π§° Basic Syntax
git reflog
This shows a chronological list of all recent HEAD updates.
π Example Output:
9a4f2c1 HEAD@{0}: reset: moving to HEAD~1
f61e59b HEAD@{1}: commit: Fix login bug
3b7c0a8 HEAD@{2}: checkout: moving from main to dev
d2e2b55 HEAD@{3}: commit: Add navbar styling
Each entry includes:
- Commit hash
- Reference (
HEAD@{n}
) - Action performed (e.g., commit, reset, checkout)
- Message
π Recovering Lost Commits with Reflog
Letβs say you accidentally ran:
git reset --hard HEAD~2
Now your last two commits are gone.
β Step-by-step recovery:
- View reflog:
git reflog
Find the commit hash before the reset, e.g.:
f61e59b HEAD@{1}: commit: Fix login bug
- Reset back to it:
git reset --hard f61e59b
π Your lost commit is now restored.
π Undoing a Bad Rebase or Checkout
Scenario: You mistakenly rebased and lost changes
- Run:
git reflog
- Look for the pre-rebase state, e.g.:
b4a1e1f HEAD@{5}: rebase: checkout main
- Restore it:
git reset --hard b4a1e1f
π¦ Recovering a Deleted Branch
If you deleted a branch that had commits not merged anywhere:
git reflog
Find the last commit from that branch and recreate it:
git checkout -b restored-branch <commit-hash>
β This brings the branch back with its commit history.
π Git Reflog vs Git Log β Whatβs the Difference?
Feature | git log | git reflog |
---|---|---|
Tracks history | Only current reachable commits | All HEAD movements (including lost commits) |
Shows deleted data | β No | β Yes |
User-facing | β Yes (for reviewing project history) | βοΈ More for recovery/debugging |
Example usage | git log --oneline | git reflog |
π§ Useful Reflog Variants
Show reflog for a specific branch:
git reflog show dev
Limit entries:
git reflog -n 5
Show hash only:
git reflog --pretty=oneline
β οΈ Common Pitfalls
Issue | Fix or Tip |
---|---|
Reflog expired or cleaned up | Avoid running git gc --prune=now too often |
Relying on reflog across machines | Reflog is local only |
Canβt find commit | Try git fsck --lost-found for orphaned data |
β Best Practices
- Use
git reflog
after destructive operations like:git reset --hard
git rebase
- Branch deletions
- Reflog entries expire (by default after 90 days), so act quickly
- Use it before you panic or think you lost work
π Summary β Git Reflog
git reflog
is an invaluable safety net that tracks all HEAD movementsβperfect for recovering lost commits, branches, and work. It’s your personal undo button in the Git world.
π Key Takeaways:
git reflog
logs all recent actions, including commits, resets, and checkouts- Helps recover lost work even after branch deletion or hard resets
- Works locally and expires over time
βοΈ Real-World Relevance: Developers use reflog as a last resort tool to undo mistakes and rescue important code lost during resets, rebases, or force pushes.
β Git Reflog β Frequently Asked Questions (FAQs)
β How long does Git reflog retain history?
β
By default, 90 days. You can customize it via:
git config --global gc.reflogExpire 30.days
β Is git reflog
shared with remote repos?
β No. Reflog is local only. It does not sync with remotes.
β Can I recover a commit that isnβt in any branch?
β
Yes. Use git reflog
to find the commit and then:
git checkout -b restore <commit-hash>
β Whatβs the difference between git log
and git reflog
?
β
git log
shows visible commits, while reflog
shows all HEAD movements, even orphaned or deleted ones.
β Can reflog restore changes after a reset?
β
Yes. Use:
git reflog
git reset --hard <previous-hash>
Share Now :