Git reflog is a powerful Git command that allows you to view the history of the HEAD pointer and other references. It records actions like commits, resets, merges, and rebases, making it extremely useful for recovering lost commits or returning to previous states.
HEAD and branch referencesBasic syntax to view reflog:
// Show the history of HEAD movements
git reflog
// Sample reflog entry format
1234567 HEAD@{0}: commit: Commit message
You can restore lost commits using the hash found in reflog.
// Checkout a lost commit using its hash
git checkout 1234567
// Restore commit by resetting HEAD
git reset --hard 1234567
// Find the commit before reset
git reflog
// Move HEAD back to previous state
git reset --hard abcdef1
git reflog expire