Back to roadmaps git Course

Recovering Lost Work with git reflog

Did you accidentally run git reset --hard and lose all your local commits? Or delete a feature branch containing days of work? In Git, almost nothing is truly lost if it was committed at least once. git reflog is your tool for recovering lost work.


1. What is the Reflog?

While git log displays the history of commits in your active branch, git reflog records every action you take in your local repository. This includes switching branches, rebasing, resetting, and making commits.

# Display the history of all local HEAD pointer updates
git reflog

2. Recovery Scenario: Restoring a Hard Reset

Suppose you accidentally ran git reset --hard HEAD~2 and lost your last two commits.

  1. Run git reflog to view your history:
abc1234 HEAD@{{0}}: reset: moving to HEAD~2
def5678 HEAD@{{1}}: commit: feat: build order api
xyz9012 HEAD@{{2}}: commit: feat: configure payments
  1. Locate the commit hash before the reset occurred (in this case, def5678).
  2. Restore the branch state to that commit hash:
# Reset HEAD back to the target commit
git reset --hard def5678

3. Recovery Scenario: Restoring a Deleted Branch

If you deleted a branch using git branch -D feature-auth before merging it:

  1. Run git reflog and look for the last commit made on that branch (e.g. xyz9012).
  2. Recreate the branch from that commit:
# Rebuild the branch pointing to the recovered commit hash
git switch -c feature-auth xyz9012

Your deleted branch is now fully restored. Note: Reflog entries are local and expire after 90 days. Be sure to perform recoveries promptly.

Published on Last updated: