Skip to content

Latest commit

Β 

History

History
89 lines (61 loc) Β· 2.1 KB

File metadata and controls

89 lines (61 loc) Β· 2.1 KB

Episode 3: Git Reflog βͺ

The hidden gem: Git's secret undo history. Recover "deleted" branches, lost commits, and botched rebases.

The Problem

You just ran git reset --hard and wiped out an hour of commits. Or you deleted a branch. Or a rebase went sideways. git log doesn't show the lost commits anymore.

They're not gone. Git reflog remembers everything.

What is the Reflog?

The reflog (reference log) is a local journal of every time HEAD moved. Every commit, checkout, rebase, reset, merge β€” it's all recorded, even if the commits are no longer reachable from any branch.

git reflog
a1b2c3d HEAD@{0}: reset: moving to HEAD~3     ← you did this
f4e5d6c HEAD@{1}: commit: add search feature   ← THIS is what you lost
b7a8c9d HEAD@{2}: commit: add filter by tag
e0f1a2b HEAD@{3}: commit: refactor task loading

Your "lost" commits are right there at HEAD@{1}, HEAD@{2}, etc.

Recovery Recipes

Recover from git reset --hard

# See what happened
git reflog

# Find the commit before the reset, then:
git reset --hard HEAD@{1}

Recover a deleted branch

# Oops, deleted feature branch
git branch -D feature/search

# Find where it was
git reflog | grep "feature/search"

# Recreate it
git branch feature/search HEAD@{5}

Undo a bad rebase

# The pre-rebase state is in the reflog
git reflog

# Find the entry right before the rebase started
git reset --hard HEAD@{N}

Key Details

  • Reflog is local only β€” it's not pushed or shared.
  • Entries expire (default: 90 days for reachable, 30 for unreachable).
  • Each ref has its own reflog: git reflog show main, git reflog show stash.
  • HEAD@{N} means "where HEAD was N moves ago".
  • main@{yesterday} means "where main pointed yesterday" β€” time-based access!

Time-Based Reflog Access

# Where was main yesterday?
git show main@{yesterday}

# What did the repo look like 2 hours ago?
git diff main@{2.hours.ago} main

# Where was HEAD last Monday?
git log HEAD@{2024-01-15}..HEAD --oneline

Run the Demo

bash episodes/demos/03-reflog-demo.sh