Lesson Completion
Back to course

Reflog Deep Dive

Intermediate
9 minutes4.8Git

The Hook (The "Byte-Sized" Intro)

You already know git reflog shows where HEAD has been. But reflog goes deeper: it tracks every branch independently, has timestamps you can query, and has expiry settings you can configure. Knowing these advanced patterns turns you from someone who "might be able to recover" into someone who always recovers.

📖 What is Reflog Deep Dive?

Beyond the basics, reflog offers branch-specific logs, time-based queries, configurable expiration, and integration with other recovery tools. This lesson covers the advanced patterns.

Conceptual Clarity

Types of reflogs:

ReflogCommandWhat It Tracks
HEAD refloggit reflogEvery HEAD movement across all branches
Branch refloggit reflog show mainMovements of the main branch pointer
Stash refloggit reflog show stashStash operations (push, drop)

Expiry settings:

SettingDefaultMeaning
gc.reflogExpire90 daysHow long reachable entries are kept
gc.reflogExpireUnreachable30 daysHow long unreachable entries are kept

Real-Life Analogy

Basic reflog is your browser's "Back" button. Deep-dive reflog is your browser's full history panel — you can search by date, filter by site, and find pages you visited weeks ago.

Visual Architecture

flowchart TD REFLOG["📋 Reflog System"] REFLOG --> HEAD["HEAD Reflog<br/>All branch switches"] REFLOG --> BRANCH["Branch Reflogs<br/>Per-branch pointer moves"] REFLOG --> STASH["Stash Reflog<br/>Push/drop operations"] HEAD --> QUERY["Time queries<br/>HEAD@{yesterday}"] style REFLOG fill:#0f3460,stroke:#53d8fb,color:#53d8fb style QUERY fill:#1a1a2e,stroke:#ffd700,color:#ffd700

Why It Matters

  • Precision: Branch-specific reflogs help when HEAD reflog is too noisy.
  • Time-based queries: Find "where was main at 3pm yesterday?" without scanning.
  • Configuration: Extend expiry for critical repos or shorten for disk savings.
  • Stash recovery: Stash reflog helps recover dropped stashes.

Code

bash
# ─── HEAD reflog (all branches) ─── git reflog # ─── Branch-specific reflog ─── git reflog show main git reflog show feature/login # ─── Reflog with timestamps ─── git reflog --date=relative # HEAD@{2 hours ago}: commit: Add feature git reflog --date=iso # HEAD@{2026-02-13 22:00:00}: commit: Add feature # ─── Time-based queries ─── git diff HEAD@{yesterday} HEAD # What changed since yesterday? git log HEAD@{1.week.ago}..HEAD # Commits in the last week git show main@{2.days.ago} # What was main 2 days ago? # ─── Reset to a time-based reflog entry ─── git reset --hard main@{yesterday} # Restore main to yesterday's state # ─── Configure reflog expiry ─── git config gc.reflogExpire "120 days" git config gc.reflogExpireUnreachable "60 days" # ─── Stash reflog ─── git reflog show stash # stash@{0}: WIP on main: abc123 commit message # stash@{1}: WIP on feature: def456 another commit

Key Takeaways

  • Reflog tracks HEAD, individual branches, and stash operations separately.
  • Use time-based queries (HEAD@{yesterday}, main@{2.days.ago}) for precise recovery.
  • Default expiry is 90 days (reachable) / 30 days (unreachable) — configurable.
  • Branch-specific reflogs are cleaner than HEAD reflog for targeted searches.

Interview Prep

  • Q: How does branch-specific reflog differ from HEAD reflog? A: HEAD reflog tracks every movement of HEAD across all branches (including checkouts). Branch-specific reflog tracks only the movements of that branch's pointer (commits, resets, merges). Branch reflog is less noisy when you only care about one branch's history.

  • Q: How would you find what main looked like 2 days ago? A: Use git show main@{2.days.ago} to see the commit, or git diff main@{2.days.ago} main to see what changed. You can also git log main@{2.days.ago}..main to see all commits in between.

  • Q: What happens to reflog entries when they expire? A: They're removed during garbage collection (git gc). Reachable entries expire after 90 days, unreachable after 30 days by default. Once expired, the associated objects may be permanently deleted if no other references point to them.

Topics Covered

Git RecoveryGit Reflog

Tags

#git#reflog#recovery#advanced

Last Updated

2026-02-13