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

I am trying to check out 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?

share|improve this question
5  
@inger But it does not include the possibility to rename the new local branch (if you want to --set-upstream later on and keep naming consistency) – fachexot Feb 1 '14 at 12:43
9  
I think this thread is unhelpful. Nothing seems to work, the original question seems to have been lost in many of the answers. I have read every word, tried everything below, and have no idea how to do what the OP wants to do. – Tony Ennis Aug 26 '14 at 0:16
2  
Git commands are not intuitive to begin with, add the changes introduced with recent versions to the mix and you have this page... – Christophe Roussy Jan 12 at 17:41

14 Answers 14

up vote 5051 down vote accepted

Update

Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, you can just do:

git fetch
git checkout test

(User masukomi points out below that git checkout test will NOT work in modern git if you have multiple remotes. In this case use git checkout -b test remote-name/test)

Old Answer

Before you can start working locally on a remote branch, you need to fetch it as called out in answers below.

To fetch a branch, you simply need to:

git fetch origin

This will fetch all of the remote branches for you. You can see the branches available for checkout with:

git branch -v -a

With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy:

git checkout -b test origin/test
share|improve this answer
280  
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 Nov 23 '09 at 15:24
115  
If it's a new remote branch you may need to git fetch before doing this so that git is aware of origin/test – Neil Sarkar Nov 4 '11 at 14:38
45  
...and you would do this with git fetch origin test – Andrew Jan 22 '12 at 23:24
17  
Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?" – Xeoncross Sep 11 '12 at 20:35
37  
git checkout test will NOT work in modern git if you have multiple remotes which have the same branch name. It can't know which one to use. – masukomi Sep 16 '14 at 15:34

Sidenote: With modern Git (>= 1.6.6), you are 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 current commit:

git checkout -b test HEAD
share|improve this answer
24  
Unsurprising, but this version has been released in the last few years - knowing this syntax can save a lot of time since there's still a lot of old documentation and comment threads floating around that suggest the older method for doing this. – Curtis Apr 16 '12 at 13:24
9  
"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros. – Craig McQueen Aug 28 '12 at 2:30
5  
"modern git" in this context is git 1.6.6 – Bobby Norton Mar 19 '13 at 20:29
8  
@aidan If you get a response like error: pathspec 'branch_name' did not match any file(s) known to git. then you should do a git fetch first. – Dennis Oct 18 '13 at 0:40
3  
Using git version 1.8.3.msysgit.0 and this doesn't work for me - did not match any file(s) known to git - I've done many git fetches – PandaWood Dec 3 '13 at 23:59

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 of git, you needed an explicit --track option, but that is the default now when you are branching off a remote branch.

share|improve this answer
1  
Only when branching off a remote branch. It's in the docs. :) – ndim Nov 23 '09 at 14:39
3  
This will create a local branch without switching to it. – Alex.Designworks Oct 16 '13 at 7:20
2  
Though I got fatal: Ambiguous object name: 'origin/dev' - where a branch 'dev' on origin most definitely exists - but I'd accidentally created a branch called "origin/dev" on my machine (in my previous stupid attempts to get this right, no doubt) ... ouch – PandaWood Dec 4 '13 at 0:04
1  
This has been giving me the error error: failed to push some refs to hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and merge the remote changes hint: (e.g. 'git pull') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. – pal4life Jun 18 '14 at 20:01

Accepted answer not working for you?

While the first and selected answer is technically correct, there's the possibility you have not yet retrieved all objects and refs from the remote repository. If that is the case, you'll receive the following error:

$ git checkout -b remote_branch origin/remote_branch

fatal: git checkout: updating paths is incompatible with switching branches.
Did you intend to checkout 'origin/remote_branch' which can not be resolved as commit?

Solution

If you receive this message, you must first do a git fetch origin where origin is the name of the remote repository prior to running git checkout remote_branch. Here's a full example with responses:

$ git fetch origin
remote: Counting objects: 140, done.
remote: Compressing objects: 100% (30/30), done.
remote: Total 69 (delta 36), reused 66 (delta 33)
Unpacking objects: 100% (69/69), done.
From https://github.com/githubuser/repo-name
   e6ef1e0..5029161  develop    -> origin/develop
 * [new branch]      demo       -> origin/demo
   d80f8d7..359eab0  master     -> origin/master

$ git checkout demo
Branch demo set up to track remote branch demo from origin.
Switched to a new branch 'demo'

As you can see, running git fetch origin retrieved any remote branches we were not yet setup to track on our local machine. From there, since we now have a ref to the remote branch, we can simply run git checkout remote_branch and we'll gain the benefits of remote tracking.

share|improve this answer
1  
I'll add a note if you have a separate branch locally: Make sure you have associated this with the remote repo using 'git remote add origin [the_path_to_your_repo/repo_name.git]' . Then use 'git fetch origin' where 'origin' means the origin repository you have made the association with. – elliotrock Feb 2 '15 at 5:51
    
git checkout -b newbranch also works great for 1-step create and checkout a new branch based on the current branch. – Linus May 19 at 13:41
    
I think this is the most up-to-date (it keeps $@#!ing changing!). Git 2.5.5 I found the only way to actually see the remote branches was git ls-remote and the only way to actually use one is git checkout -b [branch] --track [remote/branch]...and that's after git pull [remote] [branch] worked. I.e., it actually pulled the whole branch, but still wouldn't list it. – delicateLatticeworkFever May 26 at 12:51

I tried the above solution, but it didn't work. Try this, it works:

git fetch origin 'remote_branch':'local_branch_name'

This will fetch the remote branch and create a new local branch (if not exists already) with name local_branch_name and track the remote one in it.

share|improve this answer
7  
This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why. – Godsmith Sep 11 '14 at 8:45
    
it depends on git version but that seems to be made possible with its latest release. – Aftab Naveed Oct 15 '15 at 23:02
1  
This was the most direct way to accomplish what I needed which was to use a remote branch (not master) to create a new branch. – Roralee Nov 13 '15 at 23:15
    
Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches. – Alex C Oct 17 at 10:19

This will DWIM for a remote not named origin (documentation):

$ git checkout -t remote_name/remote_branch

To add a new remote, you will need to do the following first:

$ git remote add remote_name location_of_remote
$ git fetch remote_name

The first tells Git the remote exists, the second gets the commits.

share|improve this answer
    
I had to do a git fetch first. (this comment's mostly a reminder for me the next time it doesn't work) – keithepley Dec 14 '12 at 17:28
    
@keithepley I edited the answer to make it more permanent. – tacaswell Dec 14 '12 at 22:19
    
I don't see git checkout -t in the documentation. – Matt Nov 11 '15 at 1:59
    
It is under 'options' in the link to the docs on the answer. – tacaswell Nov 11 '15 at 2:02

To clone a Git repository, do:

git clone <either ssh url /http url>

The above command checks out all of the branches, but only the master branch will be initialized. If you want to checkout the other branches, do:

git checkout -t origin/future_branch (for example)

This command checks out the remote branch, and your local branch name will be same as the remote branch.

If you want to override your local branch name on checkout:

git checkout -t -b enhancement origin/future_branch

Now your local branch name is enhancement, but your remote branch name is future_branch.

Documentation

share|improve this answer
    
git clone <either ssh url /http url> - works perfectly for me – Kmeixner Dec 16 '14 at 16:56
    
Yes you are correct. Thanks for your information, I will update it very soon @warvariuc – Madhan Ayyasamy Jan 8 '15 at 10:45
    
If the remote has no master, this is not going to work. – polkovnikov.ph Sep 27 at 16:47

Use:

git checkout -b <BRANCH-NAME> <REMOTE-NAME>/<BRANCH-NAME>

Other answers do not work with modern Git in my benign case. You might need to pull first if the remote branch is new, but I haven't checked that case.

share|improve this answer
1  
Do you realize that this is an extract of this answer – Thomas Ayoub Feb 21 at 11:03
4  
Looking at it now, they do overlap. Only mine is succinct and tells you what to do rather than tell a story. I assume it can be more useful therefore, especially for nowadays git versions. You can downvote it if you think it is a bad answer. – matanster Feb 21 at 11:34

You can try

git fetch remote
git checkout --track -b local_branch_name origin/branch_name

or

git fetch
git checkout -b local_branch_name origin/branch_name
share|improve this answer
1  
FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer. – user456814 Jun 21 '14 at 18:03

If the branch is on something other than the origin remote I like to do the following:

$ git fetch
$ git checkout -b second/next upstream/next

This will checkout the next branch on the upstream remote in to a local branch called second/next. Which means if you already have a local branch named next it will not conflict.

$ git branch -a
* second/next
  remotes/origin/next
  remotes/upstream/next
share|improve this answer

git fetch && git checkout your-branch-name

share|improve this answer

git branch -r says the object name is invalid, because that branch name isn't in Git's local branch list. Update your local branch list from origin with:

git remote update

And then try checking out your remote branch again.

This worked for me.

I believe git fetch pulls in all remote branches, which is not what the original poster wanted.

share|improve this answer
2  
FYI, git remote update will also fetch all remote branches. – user456814 Jun 21 '14 at 17:59

Commands

git fetch --all
git checkout -b <ur_new_local_branch_name> origin/<Remote_Branch_Name>

are equal to

 git fetch --all

and then

 git checkout -b fixes_for_dev origin/development

Both will create a latest fixes_for_dev from development

share|improve this answer

Please follow the command to create an empty folder. Enter that and use this command:

saifurs-Mini:YO-iOS saifurrahman$ git clone your_project_url
Cloning into 'iPhoneV1'...
remote: Counting objects: 34230, done.
remote: Compressing objects: 100% (24028/24028), done.
remote: Total 34230 (delta 22212), reused 15340 (delta 9324)
Receiving objects: 100% (34230/34230), 202.53 MiB | 294.00 KiB/s, done.
Resolving deltas: 100% (22212/22212), done.
Checking connectivity... done.
saifurs-Mini:YO-iOS saifurrahman$ cd iPhoneV1/
saifurs-Mini:iPhoneV1 saifurrahman$ git checkout 1_4_0_content_discovery
Branch 1_4_0_content_discovery set up to track remote branch 1_4_0_content_discovery from origin.
Switched to a new branch '1_4_0_content_discovery'
share|improve this answer

protected by Praveen May 9 '13 at 10:07

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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