bashintermediate
Git Reflog Recovery Guide
Use git reflog to recover lost commits, undo hard resets, and restore deleted branches.
bashPress ⌘/Ctrl + Shift + C to copy
# 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=nowUse 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.
bashbeginner
Undo Last Git Commit (Soft and Hard)
Git commands to undo the last commit while keeping changes staged, unstaged, or fully discarded.
Best for: Fixing a commit with wrong files or message
#git#undo
bashbeginner
Git Revert Commit Safely
Create a new commit that undoes changes from a previous commit without rewriting history.
Best for: Undoing a deployed commit on shared branches
#git#revert
bashbeginner
Git Reset — Soft, Mixed, and Hard Explained
Understand the three git reset modes and when to use each for undoing commits safely.
Best for: Undoing accidental commits before pushing
#git#reset
bashadvanced
Git Revert — Undo Merge Commits Safely
Revert merge commits safely without rewriting history, with options for parent selection.
Best for: Rolling back a broken deployment without force push
#git#revert