Switch to a remote branch getting detached head [duplicate]
Asked Answered
T

1

6

Here is a list of all my branches:

$ git branch -a
* temp
  remotes/heroku/master
  remotes/origin/dev
  remotes/origin/master

When I type git checkout remotes/origin/master to switch to my origin master branch, Git switches to a detached HEAD state. Why?

Trillby answered 9/2, 2016 at 20:51 Comment(0)
S
13

This is the right behavior since you have checked out the remote branch.

If you wish to check out master and work on it you should do this now:

# checkout remote branch as local branch
# this will look up the branch name locally and if it does not find it it
#will checkout your remote branch with this name.
git checkout master

When you checkout remote branch you are simply pointing your HEAD to the latest commit from this branch. If you wish to work on it you have to check it out as local branch without the remote/<branch>. This will automatically checkout and create local branch with the given name.

If you wish to learn more about the HEAD read all about it here.


What is a detached HEAD?

A detached HEAD mean that your HEAD point to a commit which is not the latest in the commit chain.

In this sample commit #4 is the latest while the HEAD is pointing to commit #2.

enter image description here

Subsequence answered 9/2, 2016 at 20:53 Comment(7)
hi @codeWizard, thank you for your reply, in fact , i have three branchs , one for heroku and 2 (dev/master) for my repos server, i worked with a temp branch (cause can't switch to any of the three branchs) after trying to figure out to exit from the detached head, then i push the temp branch code to all branchs and it works fine using push HEAD:master and HEAD:dev, now i need to work on origin/dev branch, when running branch -a (i have the remotes/origin/dev, remotes/origin/master, remotes/heroku/master) , but can't chckout the origin master branch, even after doing git fetch origin.Trillby
Try this: git checkout dev; git branch -D master; git checkout masterSubsequence
thank you , here is the result , Switched to branch 'dev' Your branch is up-to-date with 'origin/dev'. error: branch 'master' not found. error: pathspec 'master' did not match any file(s) known to git. and git branch -a give * dev temp remotes/heroku/master remotes/origin/dev remotes/origin/master Trillby
can this happen because master name ( branch master for heroku and branch master for origin) ?Trillby
This question has been asked and answered countless times on the site. I know easy rep' is hard to resist, but please try to look for duplicates before posting an answer.Inflorescence
This is not a correct explanation of a detached head. A detached head is ANY state where HEAD points directly to a commit, regardless of where that commit is in the tree. In your diagram, if HEAD was pointing to commit 4, it would still be detached, because it is pointing directly to a commit, rather than a branch ref, here, masterDygall
This is not a correct explanation of a detached head. A detached head is ANY state where HEAD points directly to a commit, regardless of where that commit is in the tree. In your diagram, if HEAD was pointing to commit 4, it would still be detached, because it is pointing directly to a commit, rather than a branch ref, here, masterDygall

© 2022 - 2024 — McMap. All rights reserved.