glossaryGit 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.
explanation
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.
examples
git reflog # show the full reflog for HEADgit reflog show main # show reflog for a specific branchgit reset --hard HEAD@{2} # reset to the state from 2 moves agogit reflog expire --expire=now --all # manually expire old entriesgit checkout HEAD@{5} # check out the state from 5 moves agohow git-agent helps
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-agentFAQ
Can I recover a commit I made yesterday that I accidentally hard-reset?Is the reflog shared with remote repositories?How long do reflog entries last?