git reflog
View the history of where your branch pointers have been, recovering from mistakes and finding lost commits.
You just ran git reset --hard and immediately regretted it, or you
accidentally deleted a branch that had important commits. git reflog is your
safety net, showing a log of where your HEAD and branch pointers have been, even
for commits that are no longer reachable from any branch.
Running git reflog displays a chronological list of every position HEAD has
pointed to: commits, checkouts, resets, merges, rebases—everything. Each entry
shows the commit SHA, the action that moved HEAD there, and how many steps back
it is (HEAD@{0} is current, HEAD@{1} is one step back, etc.). This history is
local to your repository and isn't shared when you push.
If you reset to the wrong commit, reflog shows where you were before. Say you
were at commit abc123, ran git reset --hard def456 by mistake, and want to get
back. Running git reflog shows abc123 in the history, and you can return with
git reset --hard abc123. You've essentially undone your reset by resetting to
where you were before.
Deleted branches can be recovered the same way. If you deleted feature-branch
with git branch -D feature-branch, reflog shows where that branch pointed.
Create a new branch at that commit with git checkout -b feature-branch abc123,
and you've recovered the deleted branch.
The reflog expires after 90 days by default (30 days for unreachable commits), so it's not permanent. But for recent mistakes, it's an incredibly powerful undo mechanism. It's specific to your local repository—if you clone fresh, you start with an empty reflog.
Each branch has its own reflog, accessed with git reflog branch-name. This
shows the history of that specific branch pointer, which can be useful when
debugging complex branch manipulations.
Understanding reflog means understanding that Git rarely deletes commits immediately. When you reset, rebase, or delete branches, the commits still exist—they're just not reachable from any reference. Reflog gives you a way to find those commits and recover them before Git's garbage collection eventually removes them.
