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:
| Reflog | Command | What It Tracks |
|---|---|---|
| HEAD reflog | git reflog | Every HEAD movement across all branches |
| Branch reflog | git reflog show main | Movements of the main branch pointer |
| Stash reflog | git reflog show stash | Stash operations (push, drop) |
Expiry settings:
| Setting | Default | Meaning |
|---|---|---|
gc.reflogExpire | 90 days | How long reachable entries are kept |
gc.reflogExpireUnreachable | 30 days | How 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
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
# ─── 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 commitKey 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
mainlooked like 2 days ago? A: Usegit show main@{2.days.ago}to see the commit, orgit diff main@{2.days.ago} mainto see what changed. You can alsogit log main@{2.days.ago}..mainto 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.