I tried something like this:
git branch temp
to create a new branch but don't move the HEAD
. But I get:
# Not currently on any branch.
I don't want to merge anything, I just want a new branch at the current HEAD
.
If you want to keep changes made with a detached HEAD, just create a new branch and switch to it. You can create it right after arriving at a detached HEAD or after creating one or more commits. The result is the same. The only restriction is that you should do it before returning to your normal branch.
To change from one branch to another, use git switch branchName to create a new branch, then switch to it using the git switch -c branchName command. Although finding your code in the detached HEAD state is not ideal, you can use these methods to move or remove your commits and quickly get your project back on track.
Now, the best way to reattach the HEAD is to create a new branch. We can do it as simple as git checkout -b <branch-name> . This will commit the changes from your temporary branch into the branch you need them.
Detached HEAD means that it is not attached to any branch, i.e. it points directly to some commit. In other words: If it points to a commit directly, the HEAD is detached. If it points to a commit indirectly, (i.e. it points to a branch, which in turn points to a commit), the HEAD is attached.
You're sitting on a detached HEAD
:
git checkout <sha>
You want to make a branch at that commit:
git branch my-new-branch
And now switch to that branch:
git checkout my-new-branch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With