Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git create branch where detached HEAD is

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.

like image 653
octavian Avatar asked Mar 12 '14 23:03

octavian


People also ask

Can I create a branch from a detached 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.

How do you switch from detached head to 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.

How do you reattach a head to a branch?

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.

What is a detached branch in git?

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.


1 Answers

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 
like image 101
redhotvengeance Avatar answered Sep 24 '22 13:09

redhotvengeance