What is a “detached HEAD” and how can you recover from it?
A detached HEAD happens when Git’s HEAD (your current position) points to a specific
commit instead of a branch. If you make new commits in this state, they won’t belong to any
branch — you could lose them if you switch branches.
How to fix it:
If you accidentally commit in a detached HEAD state:
git switch -c temp-branch
This creates a new branch from your current state so your commits aren’t lost.
Example:
You checked out an old commit to test something:
git checkout a1b2c3d
If you make changes, create a branch to save them before switching back.