Interview Prep/Git & GitHub/Undoing Mistakes

You made several commits in detached HEAD state and then switched branches.

BeginnerScenarioUndoing Mistakes
Answer

Situation read → Git warned you about leaving commits behind. They are unreachable from any branch.

What I'd do →

  1. git reflog and find the last commit made in detached state.
  2. git branch rescued-work <sha> (or git switch -c rescued-work <sha>).
  3. Merge or cherry-pick as needed.

Why → Reflog tracks HEAD movement even without branches.

Interview tip → If still in detached HEAD, just run git switch -c new-branch before leaving.

All Git & GitHub questions Roadmap: Git & GitHub

More on Undoing Mistakes