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 →
git reflogand find the last commit made in detached state.git branch rescued-work <sha>(orgit switch -c rescued-work <sha>).- 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.
More on Undoing Mistakes
You committed on `main` when you meant to be on a feature branch. Nothing is pushed. Fix it.BeginnerYou made a typo in your last commit message and have not pushed.BeginnerA bad commit is already on shared `main`. Others have pulled it. How do you undo it?IntermediateYou ran `git reset --hard` and lost uncommitted-then-committed work. Can you get it back?IntermediateYou need to revert a merge commit on `main`. `git revert` says "commit is a merge but no -m option was given".IntermediateYou deleted a local branch with unmerged work by mistake.Beginner