Help, I am in “detached HEAD” state
HEAD points at a commit instead of a branch. Keep what you did on a new branch, or simply go back.
git checkout HEAD~1 Note: switching to 'HEAD~1'. You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch. If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example: git switch -c <new-branch-name> Or undo this operation with: git switch - Turn off this advice by setting config variable advice.detachedHead to false HEAD is now at 246fd15 Update header
The fix
git switch -c experimentCreates a branch right where you are. Everything you committed while detached is now safe on it.
experiment is an example. Replace it with your own.
git switch mainBack on main. Git warns about commits you leave behind and prints their hash, so you could still save them with
git branch <name> <hash>.
git switch -c experimentBefore
After
Why this works
Normally HEAD points at a branch, and every new commit moves that branch forward. After checking out a commit directly, HEAD points at the commit itself: you are on no branch. You can look around and even commit, but nothing holds on to those commits once you leave. Creating a branch gives them a name that does.