git reflog
`git reflog` records every change to the tip of your branches, even ones that aren't visible in `git log`, such as resets, rebases, and checked-out commits. It's a safety net for recovering 'lost' work.
Because reflog tracks where HEAD has pointed over time, you can use it to find and recover commits after an accidental hard reset or a botched rebase.
git reflog
git reset --hard HEAD@{2}Viewing the reflog
Run `git reflog` to see a list of recent HEAD movements, each with a reference like HEAD@{1} and a short description of the action that caused it.
Recovering lost commits
Once you find the commit hash you need in the reflog, use `git reset --hard commit-hash` or `git cherry-pick commit-hash` to recover it.
git reflog9c2e1aa HEAD@{0}: commit: Fix crash when cart is empty
a3f5c9e HEAD@{1}: reset: moving to a3f5c9eShows recent HEAD movements, including a reset, which can help find 'lost' commits.
git reset --hard HEAD@{1}HEAD is now at a3f5c9e Add login validationRecovers the repository state from before an earlier reset, using the reflog reference.
Key points
- git reflog tracks all movements of HEAD, even 'invisible' ones.
- It's a safety net for recovering commits lost after reset or rebase.
- Reflog entries are local only and eventually expire.
- You can reset or cherry-pick using reflog references like HEAD@{1}.
