How do you recover a lost commit or branch in git?
⚡
Quick Answer
Use git reflog to find the dangling commit SHA, then git checkout -b or git reset back to it.
Detailed Answer
reflog records where HEAD and branch tips have been, even after a reset, rebase, or branch deletion. Find the SHA in the reflog and recreate a branch (git branch recovered <sha>) or reset to it. Most 'lost' work is recoverable until git gc prunes it.
💡
Interview Tip
reflog is the answer to almost every 'I lost my work' question.
gitreflogrecovery