Git Reflog and Recovery Cheat Sheet

Recover commits, branches, and work after resets, rebases, deletes, and mistaken force updates.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all
## Reflog Basics
Show reflog
git reflog

# List recent local HEAD and branch movements.

Show reflog as a log
git log -g --oneline --decorate

# Browse reflog entries using log formatting.

Show reflog for a specific branch
git reflog show main

# Inspect reference updates for a named branch.

Preview reflog expiration impact
git reflog expire --expire=30.days --all --dry-run

# See what would be pruned from reflog without changing anything.

## Recovering Commits
Restore branch to a reflog entry
git reset --hard HEAD@{3}

# Move the current branch back to a previous recorded state.

Recover work into a new branch
git branch recovered-work HEAD@{3}

# Create a new branch from a reflog entry without moving current branch yet.

Check out a lost commit directly
git checkout <lost_commit_sha>

# Inspect a recovered commit before deciding what to do with it.

Restore a lost commit by cherry-picking it
git cherry-pick <lost_commit_sha>

# Apply a recovered commit onto the current branch.

Find dangling commits and objects
git fsck --lost-found

# List objects that are no longer reachable from refs.

## Recovering Branches and HEAD States
Create branch from ORIG_HEAD
git branch recovered-orig ORIG_HEAD

# Use ORIG_HEAD as a quick recovery anchor.

Search reflog for deleted branch tips
git reflog --all | grep feature-branch

# Find likely commit hashes for branch recovery.

Restore a branch ref manually
git update-ref refs/heads/feature <commit_sha>

# Move a branch ref to a specific commit hash.

Switch to a reflog entry in detached HEAD
git switch --detach HEAD@{3}

# Inspect a previous HEAD state without changing refs.

## Safety and Maintenance
Set default reflog retention
git config --global gc.reflogExpire 90.days

# Customize how long normal reflog entries are retained.

Set unreachable reflog retention
git config --global gc.reflogExpireUnreachable 30.days

# Control retention for entries that are no longer reachable.

Run aggressive pruning now
git gc --prune=now

# Force garbage collection and immediate pruning.

Inspect loose objects and packing state
git count-objects -vH

# Check repository object counts and size details.

Recommended next

No recommendations yet.