stackademic

The leading education platform for anyone with an interest in software development.

Git Reflog Recovery

Use the reflog to recover lost commits, branches, and undo destructive operations

Overview

The reflog records every movement of HEAD and branch tips in your local repository, even for operations that rewrite or "lose" history. Because Git rarely deletes commit objects immediately, the reflog is your safety net for recovering after a bad reset, rebase, or an accidentally deleted branch. Note that the reflog is local and entries expire (90 days by default), so recovery must happen before garbage collection prunes the commits.

Syntax / Usage

Inspect the movement history, then reset or branch back to a recovered SHA.

# Show the history of HEAD movements
git reflog

# Show the reflog for a specific branch
git reflog show main

# Restore HEAD to a previous state by index or SHA
git reset --hard HEAD@{2}

# Recover work onto a new branch without moving the current one
git branch recovered-work a1b2c3d

Examples

Undo a git reset --hard that discarded commits:

git reflog
# find the entry before the reset, e.g. HEAD@{1}
git reset --hard HEAD@{1}

Recover a branch you deleted by mistake:

git reflog
# locate the last commit that branch pointed to
git branch feature/lost 7d6e5f4

Rescue commits orphaned by a botched interactive rebase:

git reflog
# the pre-rebase tip appears as "rebase (start): checkout ..."
git checkout -b rebase-rescue HEAD@{5}

Common Mistakes

  • Assuming the reflog is shared—it is strictly local and not pushed
  • Running git gc --prune=now, which can permanently remove recoverable commits
  • Confusing HEAD@{n} (reflog position) with HEAD~n (commit ancestry)
  • Waiting past the expiry window, after which entries are pruned
  • Using git reset --hard to recover while unsaved changes exist, losing them too

See Also

commit rebase git-interactive-rebase