1

I have cloned a repo with 2 branches from github:

git clone https://github.com/user/repo
cd repo

when I do a git branch it only shows the master branch. Then I did a git branch -a it shows all branches.

master
remotes/origin/HEAD
remotes/origin/master
remotes/origin/develop

then I did a git checkout develop:

Branch develop set up to track remote branch develop from origin.
Switched to a new branch 'develop'

then git checkout master to go back to master branch.

Why is git checkout develop different from git checkout origin/develop (it says i'm in detached head state)?

Jürgen Paul
  • 12,371
  • 24
  • 87
  • 129

3 Answers3

2

You should have done:

git checkout --track -b develop origin/develop
# or
git checkout -t origin/develop

From git checkout: if you don't specify a starting point, it starts from the current HEAD (which was master in your case, meaning your 'develop' branch doesn't start from where you think it should).

On the --track option, see "Git: What is a tracking branch?".

If you want to see/track all our remote branches in your local repo, see "Track all remote git branches as local branches".

Your starting point was master, because your git branch returned the current branch: master.
If you do a git checkout develop just there, it starts from the current branch.

Community
  • 1
  • 1
VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
1

When you said git checkout develop (without specifying the remote "origin") it is attempting to checkout a local branch named develop. Your gitconfig however is setup so that if it can't find a local branch with that name, it will automatically look for origin/develop. It found that and checked out a new branch named develop locally and automatically set it up to track the remote branch.

When you git checkout origin/develop, you are checking out your cached copy of the upstream develop branch. You can't edit these branches. Which is why git is telling you you are in a detached head state.

If you want to make changes you need to commit to the local develop branch and then do a git push.

Tadeck
  • 117,059
  • 25
  • 140
  • 191
D-Rock
  • 2,457
  • 20
  • 24
1

You have not yet created the branch 'develop'. To do this, you need to create a branch that follows the 'origin/develop' branch:

git checkout -b develop origin/develop

The new branch 'develop' is decoupled from the remote branch 'origin/develop'. git does this intentionally: the decoupling allows the branches to vary independently. This is desirable for many reasons, but the two I find most useful are:

  1. You can make a series of changes that are independent of the changes made in the github branch.

  2. Changes made in the github repository are not automatically incorporated into your branch, unless you want them to be. If, for example, 'origin/develop' has a history of being particularly unstable, you may want to control when you incorporate new changes into your local branch 'develop'.

sfstewman
  • 5,303
  • 1
  • 16
  • 25