Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

The remote repo has a branch which doesn't exist on the local repo. I need to fetch it and start working with it without merging it with any other branches that I have. How can I do it?

share|improve this question

marked as duplicate by Andrew C, Community Jan 29 at 9:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

You can just type:

git fetch <remote>

to fetch all refs from your remote,

git checkout <remote>/<branch>

lets you checkout your remotes branch to an "anonymous" branch,

git checkout -b <new_local_branch>

creates a local branch based on the remote one you checked out.

You can also set the remote branch as upstream:

git branch --set-upstream-to=<remote/branch> <new_local_branch>
share|improve this answer
2  
It's not really an anonymous branch. After all you called it something. It is a remote tracking branch actually. – Tim Biegeleisen Jan 29 at 7:10
    
After checking out the remote branch with git checkout <remote>/<branch> I am on an unnamed, "anonymous", branch, I guess. – nilo Jan 29 at 7:16
    
After checking our remote/branch you are in detached head. This is not generally speaking useful – Andrew C Jan 29 at 7:36

TL;DR version

git fetch remote; git checkout branch

History

The git designers seem to have made this deliberately confusing, but actually the confusion is due to the evolutionary path git took.

In the very old days (before git version 1.5) you just grabbed the branch directly off the other repository, typing in the full URL for the repository each time. As you can probably imagine, typing in long repository paths all the time grew old fast. There were a bunch of intermediate solutions, then someone came up with the idea of a remote: a short name like origin you could use to store everything you might like to know about the other git repository.

Along with the remote came remote-tracking branches. The idea here was that if your git calls up another git over the Internet-phone and spends some time chatting with it and retrieves a whole bunch of new stuff, it might be nice to tuck away, in your own repository, a memory of every branch they offered you and which commits those were. These are your remote-tracking branches: origin/master, origin/develop, and so on.

Whenever you run git fetch origin your git phones up their git, picks up any new commits they offer, and squirrels away all their branches as origin/branch. If you did not have them before, you do now.

Checking out a remote-tracking branch

Now, you can check out a remote-tracking branch, but you can't get on a remote-tracking branch:

$ git checkout origin/develop

is exactly like finding the commit ID of origin/develop:

$ git rev-parse origin/develop
39d22b9678b8c571aab6902620c95907d5bef2db

and then handing that to git checkout:

$ git checkout 39d22b9678b8c571aab6902620c95907d5bef2db
Note: checking out '39d22b9678b8c571aab6902620c95907d5bef2db'.

You are in 'detached HEAD' state. ...

(The only difference is that git can use the name origin/develop if you hand that to git checkout—you still get that 'detached HEAD' message.)

Creating a local branch

git checkout -b branch will create and put you on a new local branch, starting from the current commit. Which is fine, but this means you have to check out the remote-tracking branch first. Or, you can tell git checkout -b where to start the new branch. But then you should also set it up to track (as git puts it) the other branch, which brings us to:

Creating a local branch from a remote-tracking branch

Now, the tricky bit is that once you have origin/develop (or some other origin/-qualified branch name), you can git checkout a regular, ordinary, local branch using that same name, even if you don't have one yet. Git will notice that develop is not (yet) a branch name, so it will scan through all your remote-tracking branches and discover that there's exactly one matching name—origin/develop in this case—and assume you meant "please create a new develop and make it track origin/develop so that it initially starts with the same commit that origin/develop points to right now".

(This is such a common thing to need that the git folks added it way back in git version 1.6.6.)

Wait, a local branch tracks a remote-tracking branch?

Yes.

But then why isn't it a local-tracking branch?

There are way too many occurrences of the words "local", "remote", and "tracking" already, not to mention the multiple meanings of the word "branch". You want a local-tracking branch to track a remote-tracking branch? What happens if your local branch tracks another local branch? :-) (Yes, git can do that.)

Seriously, the terminology is a little weird. It just grew like that.

A few other things

The git branch command has --set-upstream-to and --unset-upstream. These operate on the current branch by default, or a local branch if you specify one, and set the name of the other branch that the local branch is to track (for --set-upstream-to), or stop the local branch from tracking whatever it is set to track (for --unset-upstream). These use the word "upstream" instead of the phrase remote-tracking branch, which is reasonable since you can set the upstream to another local branch. That's how you track the other branch (whether or not it's a remote-tracking branch).

What good is having a local branch track some other branch? Why bother at all?

It lets git rebase and git merge know what to rebase-on or merge-with so that you don't have to type that in again; and it lets git status count commits that you have on the local branch that the other branch doesn't (ahead 3, for instance) and/or that they have that you don't (e.g., behind 12). In other words, it's just for convenience. It is pretty convenient though. It tastes good and it's good for you!

share|improve this answer

I found a way. This one command does it:

git fetch remote branch_name:branch_name
share|improve this answer
    
That does most of the job, but the resulting local branch is not tracking the remote-tracking branch. In general, the way to do this is git fetch <remote> followed by git checkout <branch>. I'll add an answer. – torek Jan 29 at 7:27

I'm not a git expert, but I guess you can switch to the desired branch, pull it, then create the new branch from that.
In other terms:

git checkout remote_branch
git pull
git checkout -b new_branch

To fetch remote branches, you can use:

git fetch remote_name

Where remote_name is the name of the remote in use, as an example origin.

share|improve this answer
1  
how can I checkout to a branch that doesn't exist localy yet? – German Jan 29 at 7:08
    
Answer updated. – skypjack Jan 29 at 7:13

This Git checkout -b new_branch creates a new branch of name 'new_branch'

git pull origin remote_branch pulls code from remote_branch to new_branch

git checkout -b new_branch
git pull origin remote_branch

Alternatively you can try

git checkout -b <branch> --track <remote>/<branch>

referance

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.