Undo & History

How to Recover Deleted Commits or Branches with Git Reflog

Find and restore commits or branches that were accidentally lost after a hard reset, bad rebase, or branch deletion.

Instant 1-Line Solution
Safe CommandAdvanced
git reflog
When to use this scenario:You accidentally ran "git reset --hard" or deleted a branch before pushing, and thought your code was gone forever.

Step-by-Step Execution Guide

1View all recent HEAD movements
git reflog

Shows a chronological log of every HEAD change (commits, checkouts, resets) with identifiers like HEAD@{2}.

2Find the commit right before the accident
git reflog show --date=relative

Helps identify which point in time contained your lost work.

3Restore to a new recovery branch
git checkout -b recovery-branch HEAD@{2}

Creates a brand new branch pointing directly to the recovered commit state.

Critical Pitfalls & Precautions

  • β€’Reflog entries expire after 30 to 90 days depending on git gc settings.
Frequently Asked Questions

Recover Lost Commits (Reflog) - Frequently Asked Questions

Common questions about safe rollback, remote history implications, and troubleshooting.

No. Git reflog only records commits. Unstaged, uncommitted files discarded with "git restore" cannot be recovered via reflog.