What is git reflog, and how does it save you?

Git & SDLC Undoing Changes Intermediate 2–3 Yrs3–5 Yrs Debugging 1 min read

Quick Answer

git reflog records every time HEAD moved — every commit, checkout, reset, rebase and merge — locally, for ~90 days. So a “lost” commit after reset --hard, a botched rebase, or a deleted branch is almost always recoverable: find its SHA in the reflog and git checkout / git branch / git reset back to it.

The reflog is Git's local record of everywhere HEAD has been — every commit, checkout, reset, rebase. So when someone panics because they ran git reset --hard and their commits are gone, they're not actually gone: git reflog shows the SHA the branch was at before, and you just git branch or git reset back to it. Same for a bad rebase or a deleted local branch. The caveats are that it's only on that one machine and unreferenced commits do get cleaned up eventually, so you fix it soon.

  • git reflog → lines like abc123 HEAD@{2}: reset: moving to HEAD~3.
  • Recover: git branch recovered abc123 or git reset --hard HEAD@{2}.
  • It's per-repository and per-clone — not pushed. It only helps on the machine where the operation happened.
  • Unreferenced commits are only garbage-collected after the reflog expiry, so act within days.