Dims
Dims

Reputation: 50989

Detached head does not disappear by creating branch. Why?

I am always falling into detached head state I don't know why. And still not able to find a good way to get out of it. Some time I was thinking that it is sufficient to create branch when in detached head, but this doesn't help, HEAD remains detached:

>git branch
 (HEAD detached at af34e34)
 master

>git branch detachedhead20160610

>git branch
 (HEAD detached at af34e34)
 detachedhead20160610
 master

>git branch detachedhead20160610
Fatal: A branch named 'detachedhead20160610' already exists.

>git branch detachedhead20160610-2

>git branch
 (HEAD detached at af34e34)
 detachedhead20160610
 detachedhead20160610-2
 master

I.e. new branches just added without curing detached head state.

Upvotes: 0

Views: 196

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520898

Your confusion arises from not knowing what the git branch command does. From the documentation:

Note that this will create the new branch, but it will not switch the working tree to it; use "git checkout " to switch to the new branch.

In other words, when you did git branch detachedhead20160610, you created a new branch called detachedhead20160610 but you did not switch to that branch, hence Git still reported you as being in a detached head state. Instead, you have two options:

git branch detachedhead20160610      # create a new branch
git checkout detachedhead20160610    # switch to that branch

or you can do it in one single command via:

git checkout -b detachedhead20160610 # create and switch to new branch

Upvotes: 4

Related Questions