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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186

1 Answers1

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 lates in the commit chain.

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

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • 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. – Houssem Fathallah Feb 09 '16 at 21:16
  • Try this: `git checkout dev; git branch -D master; git checkout master` – CodeWizard Feb 09 '16 at 21:18
  • 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 ` – Houssem Fathallah Feb 09 '16 at 21:22
  • can this happen because master name ( branch master for heroku and branch master for origin) ? – Houssem Fathallah Feb 09 '16 at 21:28
  • 1
    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. – jub0bs Feb 09 '16 at 21:30
  • 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, `master` – De Novo May 18 '20 at 17:31
  • 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, `master` – De Novo May 18 '20 at 17:32