How to avoid detached headstate in git?

I'm trying to get a remote branch and track it, but when I do this:

git checkout remotes/mybranch

      

When I do git branch -a it displays remotes / mybranch in red? I am getting the error:

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 performing another checkout.

      

How can I check this mybranch locally and track it? How can I avoid the detached HEAD state?

+3


source to share


3 answers


what will i do next time to avoid this separate condition?

See " Why did my Git repository enter the offline HEAD state? "

You just need to check the local name with a forked string, not remote tracking (for example origin/mybranch

).
The remote tracking branch is origin/mybranch

not written to the write commit, but only to track commits retrieved from the upstream repository.



To avoid this:

git checkout -b abranch origin/abranch 

      

This will create a local " abranch

" branch and update origin/abranch

.
See " Difference between Git checkout --track origin / branch and Git checkout -b branch origin / branch "

+2


source


Create a temporary branch, then pick a pivot point to align with the detached head:

git checkout -b temporary_branch
git branch -f master temporary_branch
git checkout master

      



Then you can do what you are trying to do:

git checkout remotes/mybranch

      

0


source


To create a new tracking branch, you can simply do this:

git checkout mybranch

      

This will automatically create a local tracking branch and check it out. It will fail if there are multiple remotes having a named branch mybranch

.

You can save the work you did in a seperate head, but if you haven't done any work in a seperate HEAD, it's easier to just run the above command.

0


source







All Articles