What is reflog and why is it needed?
git reflog is a log of the movements of the HEAD pointer and branches in the local repository.
It stores information about:
- where HEAD pointed
- which commands changed the repository's state
- which commits were current before
Why reflog is needed
reflog lets you:
- recover commits after a
reset - find lost commits after a
rebase - return to the state before a mistaken command
- recover work after a
checkoutorcommit --amend
Its main purpose:
reflog is an "insurance policy" against losing history in the local repository.
What is important to know
reflogis local - it is not transferred viapush- it is kept for a limited time (by default ~90 days)
- it contains even commits that are no longer reachable via
git log
Example usage
bash
git reflogThe output shows the list of HEAD states:
text
HEAD@{0} reset: moving to HEAD~1
HEAD@{1} commit: add featureYou can return to a state:
bash
git checkout HEAD@{1}or
bash
git reset --hard HEAD@{1}A key phrasing for an interview
git reflogis a local log of HEAD changes that lets you recover commits and the repository's state after reset, rebase, and other dangerous operations.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.