Home/Learn/Git/Reflog — Recovering Lost Commits

Reflog — Recovering Lost Commits

Advanced
Advanced

The reflog is Git's safety net — it records every change to HEAD and branch pointers for 90 days. You can recover commits lost to reset --hard, dropped rebases, or accidental branch deletions.

Overview

The reflog is the feature that makes Git nearly impossible to permanently corrupt during normal development. Every time HEAD moves — commit, checkout, merge, rebase, reset — Git appends a line to .git/logs/HEAD. This log persists for 90 days (configurable). Combined with git reset --hard to a reflog entry, you can recover almost anything: a commit reset --hard'd away, a branch accidentally deleted, work lost during a bad rebase. It is also the explanation for why "git reset --hard HEAD" is survivable.

Reading the Reflog

git reflog shows all recent HEAD positions with a short reference syntax HEAD@{n} where n is how many steps ago.

Reading the reflog
git reflog
# a3f8c12 HEAD@{0}: commit: Add payment gateway
# 9d2e441 HEAD@{1}: rebase -i (finish): returning to refs/heads/main
# 7b2e441 HEAD@{2}: rebase -i (squash): Implement payment
# 3d9f2e0 HEAD@{3}: rebase -i (pick): Add auth
# 2c1a8b7 HEAD@{4}: reset: moving to HEAD~3
# f1a2b3c HEAD@{5}: commit: Messy WIP commit  ← "lost" commit
# e9d8c7b HEAD@{6}: checkout: moving from feature to main

# Reflog for a specific branch
git reflog show feature/payment

# Reflog shows both hash AND the operation that moved HEAD
# This is how you find a "lost" commit hash

Recovering Lost Commits

Once you have the hash or reflog reference of the lost commit, restore it with reset, checkout, or cherry-pick.

Recovering lost commits with reflog
//  Scenario: accidentally did git reset --hard HEAD~3
//  Three commits are now "unreachable" — apparently lost

# Find the lost commits in reflog
git reflog
# a3f8c12 HEAD@{0}: reset: moving to HEAD~3   ← the reset
# b4d7e9f HEAD@{1}: commit: Add login tests    ← lost commit 3
# c5e8f1a HEAD@{2}: commit: Fix auth flow      ← lost commit 2
# d6f9a2b HEAD@{3}: commit: Add user model     ← lost commit 1

# Recover by resetting HEAD to before the accident
git reset --hard HEAD@{1}
# Or by hash:
git reset --hard b4d7e9f

# Recover to a new branch (safest — preserves current state)
git switch -c recovered-work HEAD@{1}

# Also works: cherry-pick the lost commit onto current branch
git cherry-pick d6f9a2b

# If you don't know the hash, find dangling commits
git fsck --lost-found
# dangling commit d6f9a2b...

Key Points to Remember

  • 1git reflog records every HEAD movement for 90 days — it is Git's safety net
  • 2HEAD@{n} syntax references how many HEAD positions ago (HEAD@{0} = current, HEAD@{1} = previous)
  • 3Use git reflog to find the hash of a "lost" commit, then git reset --hard <hash> to recover
  • 4git fsck --lost-found finds dangling objects not reachable from any ref
  • 5Reflog is per-repository and local — it is NOT shared or pushed to remotes

Interview Questions

Sign in to ask Aria
1

How would you recover a commit that was removed with git reset --hard?

Hard
2

What is the git reflog? How does it differ from git log?

Medium
3

How long does Git keep reflog entries by default?

Easy

Ask Aria about Reflog — Recovering Lost Commits

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…