Home DevOps Git Reflog — The 90-Day Safety Net That Saved a Deleted Branch
Intermediate 3 min · July 07, 2026
Git Reflog: Recover Lost Commits and Branches

Git Reflog — The 90-Day Safety Net That Saved a Deleted Branch

git reflog records every HEAD movement for 90 days.

N
Naren Founder & Principal Engineer

20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.

Follow
Production
production tested
July 07, 2026
last updated
214
articles · all by Naren
Before you start⏱ 15 min
  • Solid grasp of devops fundamentals
  • Comfortable reading and writing code examples independently
  • Basic understanding of production deployment concepts
 ● Production Incident
Quick Answer

git reflog shows every HEAD movement. Find the SHA before the disaster, then git reset --hard to restore. Reflog entries expire after 90 days (configurable).

✦ Definition~90s read
What is Git Reflog?

git reflog (reference log) records every change to HEAD — commits, resets, checkouts, merges, rebases, and amends. It's Git's local activity log, stored in .git/logs/HEAD.

Think of git reflog as the black box flight recorder on an airplane.
Plain-English First

Think of git reflog as the black box flight recorder on an airplane. It records every single move the plane (HEAD) makes — every turn, every altitude change, every bump. When something goes wrong, you can rewind the tape and see exactly where things went off course. The regular git log is the passenger manifest (who's on board). Reflog is the flight recorder (how the plane moved). Both are essential, but only reflog can tell you how you GOT to the current state.

⚙ Browser compatibility
Latest versions — ✓ supported
ChromeFirefoxSafariEdge

Every Git user eventually has a 'oh no' moment: git reset --hard on the wrong commit, deleted the wrong branch, rebase gone wrong. git reflog is the safety net for all of these. It records every HEAD movement for 90 days, and recovering from almost any disaster is a single reflog lookup away. The catch: reflog is LOCAL. It only records YOUR movements on YOUR machine. If you never had the commit locally, reflog can't help you. This guide covers the core recovery patterns and the incident where reflog saved three weeks of work after a botched rebase.

Understanding the Reflog

The reflog lives in .git/logs/HEAD. Each line records: old SHA, new SHA, committer, timestamp, and action description. git reflog displays a simplified view with an index (HEAD@{0}, HEAD@{1}, etc.), SHA, and action.

git reflog show <branch> shows the reflog for a specific branch. git reflog --all shows reflogs for all refs including stashes. git log -g shows the log with reflog references.

Reflog expiration is 90 days for reachable commits and 30 days for unreachable (dangling) commits. Running git gc (garbage collection) manually can expire entries early. git reflog expire --expire=now --all clears the reflog intentionally.

01_reflog_basics.shBASH
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# View the reflog
git reflog | head -10

# Restore to a specific reflog entry
git reset --hard HEAD@{2}

# Find a deleted branch commit
git reflog | grep 'feature/payments'

# Recreate a branch from a reflog SHA
git branch recovered-branch a1b2c3d

# View reflog for a specific branch
git reflog show main

# Search reflog for a specific string
git reflog | grep 'commit\|rebase'
Output
a1b2c3d HEAD@{0}: checkout: moving from feature/payments to main
d4e5f6g HEAD@{1}: commit: Add payment retry logic
e5f6g7h HEAD@{2}: commit: Add payment validation
...
Reflog Is LOCAL — It Does Not Push or Clone
Reflog entries exist only on your machine. If you clone a repo, the reflog is empty. If your hard drive dies, the reflog is gone. Never rely on reflog as your only backup — push important branches to remote. Reflog is a recovery tool, not a backup strategy.
Production Insight
Set up a CI job that runs git reflog expire --expire-unreachable=now --all && git gc --aggressive on build agents to prevent reflog bloat. Developer machines should keep the default 90-day expiry. For critical repos, consider backing up .git/logs/ as part of your backup strategy — it's a small file that can save weeks of work.
Key Takeaway
Reflog records every HEAD movement for 90 days. Recover deleted branches, undone commits, and botched rebases. Reflog is LOCAL — it doesn't sync to remote. Run git reflog before panicking after a Git disaster.
● Production incidentPOST-MORTEMseverity: high

Deleted Branch With 3 Weeks of Work — Reflog to the Rescue

Symptom
A junior developer accidentally deleted their feature branch with git branch -D instead of -d. The branch had 3 weeks of unmerged work. The developer panicked. A senior engineer ran git reflog | grep feature/payments, recovered the SHA, and recreated the branch in 30 seconds.
Assumption
The developer assumed the work was permanently lost. They started researching file recovery tools.
Root cause
1. Developer ran git branch -D feature/payments to delete an old local branch. 2. The branch contained commits that had never been pushed to remote. 3. Git deleted the branch pointer but NOT the commits. The commits became dangling objects. 4. The developer didn't know about reflog. They assumed the commits were gone forever. 5. Three weeks of work was recoverable — still in Git's object database, referenced by reflog.
Fix
1. Ran git reflog | head -30 to see recent HEAD movements. 2. Found the entry: a1b2c3d HEAD@{14}: checkout: moving from feature/payments to main. 3. The SHA a1b2c3d was the last commit on the deleted branch. 4. Ran git branch feature/payments a1b2c3d to restore the branch pointer. 5. Pushed the recovered branch to remote: git push origin feature/payments.
Key lesson
  • Reflog records every HEAD movement for 90 days.
  • git branch -D deletes the pointer, not the commits.
  • Recovery is always possible if you haven't run git gc.
  • Check reflog before panicking.
  • Push important branches to remote frequently — reflog is local, remote is permanent.
Production debug guideUse this when production is on fire3 entries
Symptom · 01
Accidentally hard-reset and lost commits
Fix
Run git reflog | head -20, find the SHA before the reset, then git reset --hard <sha>
Symptom · 02
Deleted a branch with unmerged work
Fix
Run git reflog | grep <branch-name> to find the tip commit SHA, then git branch recovered-branch <sha>
Symptom · 03
Rebase went wrong and want to undo
Fix
Run git reflog to find ORIG_HEAD or the SHA before rebase, then git reset --hard <sha>

Key takeaways

1
Reflog records every HEAD movement
commits, checkouts, resets, rebases
2
Default expiry
90 days for reachable, 30 days for dangling commits
3
Recover deleted branches
git reflog | grep <branch> then git branch <name> <sha>
4
Reflog is LOCAL
never rely on it as your only backup
N
Naren Founder & Principal Engineer

20+ years shipping production infrastructure and CI/CD at scale. Lessons pulled from things that broke in production.

Follow
Verified
production tested
July 07, 2026
last updated
214
articles · all by Naren
🔥

That's Git. Mark it forged?

3 min read · try the examples if you haven't

Previous
Git Worktree: Work on Multiple Branches Simultaneously
30 / 47 · Git
Next
Git Merge Strategies: Recursive, Ours, Theirs and Octopus