bashintermediate

Git Reflog Recovery Guide

Use git reflog to recover lost commits, undo hard resets, and restore deleted branches.

bash
# View reflog (shows all HEAD movements)
git reflog

# Detailed reflog with dates
git reflog --date=iso

# Reflog for a specific branch
git reflog show feature-branch

# Recover from accidental hard reset
# You did: git reset --hard HEAD~3 (lost 3 commits)
git reflog
# Find the commit before the reset, e.g., abc1234
git reset --hard abc1234

# Recover a deleted branch
git reflog
# Find the last commit of the branch, e.g., def5678
git checkout -b recovered-branch def5678

# Recover after bad rebase
git reflog
# Find the commit before rebase started
git reset --hard HEAD@{5}  # or specific hash

# Create a backup branch before risky operations
git branch backup-before-rebase

# View reflog entry details
git show HEAD@{3}

# Diff between reflog entries
git diff HEAD@{0} HEAD@{3}

# Reflog expires after 90 days by default
# Check expiry config
git config gc.reflogExpire

# Force expire reflog (caution!)
# git reflog expire --expire=now --all
# git gc --prune=now

Use Cases

  • Recovering from accidental git reset --hard
  • Restoring deleted branches
  • Undoing a bad rebase or merge

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.