tags:

views:

214

answers:

3

Hi

I am trying to checkout a remote branch:

Somebody pushed a branch called test with git push origin test to a shared repository. I can see the branch with git branch -r. But how can I get this branch?

  • git checkout test does nothing
  • git checkout origin/test does something, but git branch says * (no branch). I am on no branch?

How do I share branches via a public repository?

Thank You.

+8  A: 

If you want to check out a local working branch based on origin/test you need to check it out with

git checkout -b test origin/test
m5h
To expand on this: git doesn't allow you to work on someone else's branches. You can only work on your own. So if you want to add to someone else's branch, you need to create your own "copy" of that branch, which is what the above command does (well, it creates your branch and checks it out, too).
Dan Moulding
+4  A: 

In this case, you probably want to create a local test branch which is tracking the remote test branch:

$ git branch test origin/test

In earlier versions, you needed an explicit --track option, but that is the default now when you are branching off a remote branch.

ndim
I did not know track was now default: nice. +1
jkp
Only when branching off a remote branch. It's in the docs. :)
ndim
A: 

Sidenote: with modern Git (which means probably not yet released version) you would be able to use just

git checkout test

(note that it is 'test' not 'origin/test') to perform magical DWIM-mery and create local branch 'test' for you, for which upstream would be remote-tracking branch 'origin/test'.


The * (no branch) in git branch output means that you are on unnamed branch, in so called "detached HEAD" state (HEAD points directly to commit, and is not symbolic reference to some local branch). If you made some commits on this unnamed branch, you can always create local branch off curent commit:

git checkout -b test HEAD
Jakub Narębski