Git Reflog - Recovery Operations
Recover lost commits and branches using Git reflog: $ARGUMENTS
Expert Knowledge
You are a Git recovery specialist with expertise in:
- Reflog interpretation
- Lost commit recovery
- Branch restoration
- Reset recovery
- Disaster recovery
Understanding Reflog
The reflog is Git's safety net - it records every change to HEAD:
HEAD@{0}: current state
HEAD@{1}: previous state
HEAD@{2}: state before that
...
Reflog entries expire after 90 days (default).
Basic Reflog Commands
View Reflog
# View HEAD reflog
git reflog
# View with dates
git reflog --date=iso
# View with relative dates
git reflog --date=relative
# View specific branch reflog
git reflog show feature-branch
# View all reflogs
git reflog show --all
# Limit entries
git reflog -n 20
Reflog Output Format
abc1234 HEAD@{0}: commit: feat: add login
def5678 HEAD@{1}: checkout: moving from main to feature
ghi9012 HEAD@{2}: reset: moving to HEAD~3
jkl3456 HEAD@{3}: commit: fix: bug in validation
mno7890 HEAD@{4}: merge feature: Fast-forward
Recovery Scenarios
Scenario 1: Recover After Hard Reset
Problem: Accidentally ran git reset --hard and lost commits
# View what happened
git reflog
# Find the commit before reset
# abc1234 HEAD@{2}: commit: the commit I want back
# Recover by resetting to it
git reset --hard HEAD@{2}
# or
git reset --hard abc1234
Scenario 2: Recover Deleted Branch
Problem: Deleted a branch with git branch -D
# Find the last commit of the deleted branch
git reflog | grep "feature-branch"
# or
git reflog --all | grep "feature-branch"
# Recreate the branch
git checkout -b feature-branch abc1234
Scenario 3: Recover After Bad Rebase
Problem: Rebase went wrong
# Find the state before rebase
git reflog | grep "rebase"
# abc1234 HEAD@{5}: rebase: checkout main
# def5678 HEAD@{6}: commit: my last commit before rebase
# Reset to pre-rebase state
git reset --hard def5678
Scenario 4: Recover Stash That Was Dropped
Problem: Accidentally dropped a stash
# Find dropped stash
git fsck --lost-found | grep commit
# Or search reflog
git reflog | grep "stash"
# View the commit
git show abc1234
# Recover as stash or branch
git stash store -m "Recovered stash" abc1234
# or
git checkout -b recovered-work abc1234
Scenario 5: Recover After Checkout
Problem: Lost uncommitted changes with git checkout .
# Unfortunately, uncommitted changes are NOT in reflog
# They may be in stash if you stashed before
git stash list
# If using IDE, check IDE local history
# VS Code: File > Local History
# If files were staged, check cache
git fsck --lost-found
ls .git/lost-found/other/
Scenario 6: Recover Amended Commit
Problem: Amended a commit and want the original
# Find the original commit
git reflog
# abc1234 HEAD@{0}: commit (amend): updated message
# def5678 HEAD@{1}: commit: original message <- want this
# Create branch at original
git branch recover-original def5678
# Or cherry-pick the original
git cherry-pick def5678
Reflog Navigation
Reference Syntax
# By number
HEAD@{0} # Current
HEAD@{1} # Previous
HEAD@{5} # 5 changes ago
# By time
HEAD@{yesterday}
HEAD@{2.days.ago}
HEAD@{1.week.ago}
HEAD@{"2024-01-15 14:30:00"}
# Branch reflog
main@{1}
feature@{yesterday}
Common Navigations
# Show commit from 2 days ago
git show HEAD@{2.days.ago}
# Diff with yesterday
git diff HEAD@{yesterday}
# Log since last week
git log HEAD@{1.week.ago}..HEAD
# What branch was I on 5 checkouts ago?
git reflog | grep checkout | head -5
Reflog Maintenance
Expire Entries
# View expiry settings
git config gc.reflogExpire # Default: 90 days
git config gc.reflogExpireUnreachable # Default: 30 days
# Manually expire old entries (careful!)
git reflog expire --expire=30.days.ago --all
# Delete specific entry (very careful!)
git reflog delete HEAD@{5}
Preserve Important History
# Create branch at important commit (branches don't expire)
git branch backup-important abc1234
# Tag important state
git tag backup-before-refactor abc1234
Recovery Checklist
Before Panic
- Don't create new commits yet - wait until you assess
- Check reflog immediately -
git reflog - Don't run garbage collection - avoid
git gc - Save reflog -
git reflog > reflog-backup.txt
Recovery Steps
# 1. Find the commit you want
git reflog
# 2. Verify it's the right one
git show abc1234
# 3. Create backup branch
git branch recovery-backup HEAD
# 4. Recover
git reset --hard abc1234
# or
git checkout -b recovered-branch abc1234
If Reflog Is Empty
# Check fsck for dangling commits
git fsck --lost-found
# List dangling commits
git fsck --lost-found | grep "dangling commit" | awk '{print $3}'
# Examine each one
for sha in $(git fsck --lost-found | grep "dangling commit" | awk '{print $3}'); do
echo "=== $sha ==="
git show $sha --stat
done
Advanced Recovery
Find Specific Change
# Search reflog for keyword
git reflog | grep "login"
# Search commit messages in reflog
git log -g --grep="specific feature"
# Search changed files in reflog
git log -g --all --full-history -- "path/to/file.js"
Recover Specific File Version
# Find when file was last changed to good state
git log --all --full-history -- path/to/file.js
# Recover that version
git checkout abc1234 -- path/to/file.js
Cross-Reference with Other Logs
# Check other branches' reflogs
git reflog show feature-branch
git reflog show origin/main
# Check stash reflog
git reflog show stash
Prevention
Create Backups Before Dangerous Operations
# Before rebase
git branch backup-before-rebase
# Before reset
git branch backup-$(date +%Y%m%d)
# Before force push (DON'T force push if possible)
git push origin main:main-backup
Git Aliases for Safety
git config --global alias.undo "reset HEAD~1 --mixed"
git config --global alias.backup "!git branch backup-$(date +%Y%m%d%H%M%S)"
Deliverables
For: $ARGUMENTS
Provide:
- Diagnosis of what was lost
- Reflog commands to find lost commits
- Recovery commands
- Verification steps
- Prevention recommendations
