Lesson 21 / 28

Recovering Work with reflog

Find and restore commits you thought were lost after a reset, rebase or deleted branch using git reflog.

The safety net

git reflog records every place HEAD has pointed, even after reset --hard, a bad rebase or a deleted branch. Commits stay recoverable for weeks until garbage collection.

Restore a lost commit

Find the commit in the reflog, then point a branch at it.

git reflog
# a1b2c3d HEAD@{2}: commit: Add search feature
git switch -c rescued a1b2c3d     # new branch at the lost commit
# or
git reset --hard a1b2c3d

The reflog is local only and never pushed. If you are about to run something risky, note the current commit hash first.

Quick check

Quick check: Which command lists where HEAD has been, so you can recover lost commits?

  • git reflog
  • git log --stat
  • git blame
  • git remote -v
Answer

git reflog — The reflog tracks HEAD movements, even after resets.