Frequently Asked Question

What is git reflog and why is it a safety net?

The reflog is a local journal of every time a reference (HEAD, a branch, a tag) was updated in your repository. Every commit, reset, checkout, merge, and rebase appends an entry recording the old and new values. git reflog prints HEAD's reflog; git reflog show main prints the reflog for the main branch. Each entry has a stable shorthand, HEAD@{2} means "two HEAD moves ago", that you can pass to other commands.

The reflog is your safety net for any operation you regret. Hard-reset onto the wrong commit? git reset --hard HEAD@{1} puts you back. Lost a branch with git branch -D? Its old tip is still in the reflog; check it out and recreate the branch. Botched a rebase? git reset --hard ORIG_HEAD or the matching reflog entry restores the pre-rebase state. The reflog is purely local, it is never pushed and the entries expire after 90 days by default, but as long as the commit object still exists in the object database, it is recoverable.

Video

Further reading and video