Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to a remote branch getting detached head [duplicate]

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?

like image 497
Houssem Fathallah Avatar asked Feb 09 '16 20:02

Houssem Fathallah


1 Answers

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

like image 160
CodeWizard Avatar answered Sep 28 '22 18:09

CodeWizard