πŸ“š Advanced Git & GitHub Topics
Estimated reading: 4 minutes 15 views

πŸ•΅οΈ 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, or checkout 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:

  1. View reflog:
git reflog

Find the commit hash before the reset, e.g.:

f61e59b HEAD@{1}: commit: Fix login bug
  1. 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

  1. Run:
git reflog
  1. Look for the pre-rebase state, e.g.:
b4a1e1f HEAD@{5}: rebase: checkout main
  1. 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?

Featuregit loggit reflog
Tracks historyOnly current reachable commitsAll HEAD movements (including lost commits)
Shows deleted data❌ Noβœ… Yes
User-facingβœ… Yes (for reviewing project history)βš™οΈ More for recovery/debugging
Example usagegit log --onelinegit 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

IssueFix or Tip
Reflog expired or cleaned upAvoid running git gc --prune=now too often
Relying on reflog across machinesReflog is local only
Can’t find commitTry 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Git Reflog

Or Copy Link

CONTENTS
Scroll to Top