git detached HEAD — what it means and how to fix
Quick Answer
# Save your work to a new branch (recommended)
git checkout -b my-fix
# Or discard nothing and go back to main
git checkout main
When this happens
HEAD detached at abc1234
You checked out a specific commit, tag, or remote branch directly. Any commits made now will not belong to a branch and can be lost.
Other causes & fixes
Recover commits already made while detached
Find the commit hash with git log --oneline, then attach it to a new branch.
git log --oneline -5 # note the hash of your last commit
git checkout -b recovery-branch abc1234
How to safely inspect a past commit
Use a worktree to check out an old commit without detaching HEAD in your main working directory.
git worktree add ../inspect abc1234
cd ../inspect
# ... look around ...
git worktree remove ../inspect
Related