git reflogThe reference log (reflog) records every movement of HEAD: commits, checkouts, resets, rebases, and more. It is your safety net. Even if you lose a commit from the graph (e.g., via git reset --hard), the reflog keeps a reference to it for 90 days.
Reflog entriesEach reflog entry shows: HEAD@{index}: action (from -> to). The most recent entry is HEAD@{0}. You can use HEAD@{N} anywhere you would use a commit hash to reference a past state.
Recovering with reflogAfter losing a commit, run git reflog to find its hash. Then run git cherry-pick <hash> to re-apply it, or git reset --hard HEAD@{N} to restore your branch to that state.
View the commit history
Run git log --oneline. There are 3 commits. The latest commit "Add important fix" has a critical change we need to keep.
● View the commit history
○ Accidentally delete the latest commit
○ Confirm the commit is gone
○ Find the lost commit with git reflog
○ Recover the lost commit
○ Verify the recovery
Terminal
📘 Starting tutorial: Git Reflog
Recover lost commits using git reflog — Git's safety net