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

Inspect reference movements and recent branch history changes.

Show reflog

List recent local HEAD and branch movements.

bashANYgitreflogrecovery
bash
git reflog
Notes

Reflog is local-only and records where refs pointed over time, which makes it invaluable for recovery.

Show reflog as a log

Browse reflog entries using log formatting.

bashANYgitrefloglogrecovery
bash
git log -g --oneline --decorate
Notes

`git log -g` formats reflog traversal as commit log output, which many users find easier to scan.

Show reflog for a specific branch

Inspect reference updates for a named branch.

bashANYgitreflogbranch
bash
git reflog show main
Notes

Useful when the recovery target involves a branch ref rather than the current HEAD movement.

Preview reflog expiration impact

See what would be pruned from reflog without changing anything.

bashANYgitreflogmaintenance
bash
git reflog expire --expire=30.days --all --dry-run
Notes

Mostly administrative, but useful when troubleshooting object reachability and retention.

Recovering Commits

Find and restore commits after destructive operations.

Restore branch to a reflog entry

Move the current branch back to a previous recorded state.

bashANYgitreflogresetrecovery
bash
git reset --hard HEAD@{3}
Notes

Strong recovery option after bad rebase, reset, or merge. Verify the target first because `--hard` discards working tree changes.

Recover work into a new branch

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

bashANYgitreflogbranchrecovery
bash
git branch recovered-work HEAD@{3}
Notes

Safer than immediately hard-resetting because it preserves your current branch while giving you a recovery pointer.

Check out a lost commit directly

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

bashANYgitrecoverycheckout
bash
git checkout <lost_commit_sha>
Notes

Detached HEAD checkout is useful for verifying that a reflog-discovered commit is the one you want to recover.

Restore a lost commit by cherry-picking it

Apply a recovered commit onto the current branch.

bashANYgitrecoverycherry-pick
bash
git cherry-pick <lost_commit_sha>
Notes

Useful when you only need one or a few recovered commits rather than rewinding the whole branch.

Find dangling commits and objects

List objects that are no longer reachable from refs.

bashANYgitfsckdanglingrecovery
bash
git fsck --lost-found
Notes

Helpful when reflog is insufficient and you suspect commits still exist as dangling objects.

Recovering Branches and HEAD States

Restore deleted branches and previous branch tips.

Create branch from ORIG_HEAD

Use ORIG_HEAD as a quick recovery anchor.

bashANYgitorig-headbranchrecovery
bash
git branch recovered-orig ORIG_HEAD
Notes

ORIG_HEAD often points to the pre-operation tip after merge, pull, reset, or rebase.

Search reflog for deleted branch tips

Find likely commit hashes for branch recovery.

bashANYgitreflogbranchgrep
bash
git reflog --all | grep feature-branch
Notes

Search across all reflogs when recovering branch pointers that were deleted or force-updated.

Restore a branch ref manually

Move a branch ref to a specific commit hash.

bashANYgitupdate-refbranchrecovery
bash
git update-ref refs/heads/feature <commit_sha>
Notes

Low-level but useful when you know the exact target and want to repair a ref directly.

Switch to a reflog entry in detached HEAD

Inspect a previous HEAD state without changing refs.

bashANYgitreflogdetached-head
bash
git switch --detach HEAD@{3}
Notes

A safe way to inspect old states before deciding to branch or reset.

Safety and Maintenance

Retention, garbage collection, and recovery-friendly practices.

Set default reflog retention

Customize how long normal reflog entries are retained.

bashANYgitreflogconfiggc
bash
git config --global gc.reflogExpire 90.days
Notes

Longer reflog retention can improve recovery options in repositories with heavy history editing.

Set unreachable reflog retention

Control retention for entries that are no longer reachable.

bashANYgitreflogconfiggc
bash
git config --global gc.reflogExpireUnreachable 30.days
Notes

Important if you often rewrite history and want a wider window for recovery.

Run aggressive pruning now

Force garbage collection and immediate pruning.

bashANYgitgcprunedanger
bash
git gc --prune=now
Notes

Use carefully. Immediate pruning can remove recovery options for unreachable objects sooner than expected.

Inspect loose objects and packing state

Check repository object counts and size details.

bashANYgitmaintenanceobjects
bash
git count-objects -vH
Notes

Useful during repository maintenance and when reasoning about how much potentially recoverable loose object data exists.

Recommended next

No recommendations yet.