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?
source to share
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 "
source to share
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.
source to share