glossary

Git Reflog

A reference log that records every movement of HEAD in the local repository, providing a safety net to recover lost commits and undo history-altering operations.

The reflog (reference log) is git's safety net. Every time HEAD changes — commits, checkouts, rebases, merges, resets, cherry-picks, stashes — an entry is written to the reflog. This means that even if you "lose" a commit (e.g., by doing a hard reset or rebase that discarded commits), you can find it in the reflog and recover it. The reflog is local-only. It is never pushed to remotes and is not shared with other developers. Entries expire by default after 90 days for reachable commits and 30 days for unreachable ones. You can view the reflog with git reflog, which shows each entry's position (HEAD@{N}), the commit hash, and the action that created it. Common recovery scenarios made possible by reflog include: recovering after a hard reset (git reset --hard HEAD@{1}), undoing a rebase that went wrong (git reset --hard ORIG_HEAD or finding the pre-rebase commit in reflog), and restoring a dropped stash. The reflog is the first place to check when something went missing in git.

git reflog # show the full reflog for HEAD
git reflog show main # show reflog for a specific branch
git reset --hard HEAD@{2} # reset to the state from 2 moves ago
git reflog expire --expire=now --all # manually expire old entries
git checkout HEAD@{5} # check out the state from 5 moves ago

git-agent's conventional commit messages make reflog entries self-documenting. When you scan git reflog, each entry shows the commit message, so you can immediately identify which state you need to recover even without cross-referencing hashes against the main log.

brew install gitagenthq/brew/git-agent
Can I recover a commit I made yesterday that I accidentally hard-reset?
Almost certainly yes. Run git reflog and look for the commit message or the state before the reset. The commit will have an entry like HEAD@{N} that you can reset or cherry-pick from.
Is the reflog shared with remote repositories?
No. The reflog is strictly local to your repository and is never pushed or fetched. Each developer has their own reflog tracking only their local operations.
How long do reflog entries last?
Default expiry is 90 days for commits reachable from a branch or tag, and 30 days for unreachable commits. These values are configurable via gc.reflogExpire and gc.reflogExpireUnreachable.