How do I check out a remote Git branch?

5 623

1 476

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

Now I'm trying to check out the remote test branch.

I've tried:

  • git checkout test which does nothing

  • git checkout origin/test gives * (no branch). Which is confusing. How can I be on "no branch"?

How do I check out a remote Git branch?

Juri Glass

Posted 2009-11-23T14:23:46.150

Reputation: 30 404

7@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 – 2014-02-01T12:43:49.540

13I 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 – 2014-08-26T00:16:17.160

5Git commands are not intuitive to begin with, add the changes introduced with recent versions to the mix and you have this page... – Christophe Roussy – 2016-01-12T17:41:39.680

8I feel like I'm taking crazy pills. I'm trying to checkout a branch from an upstream, not just origin, and every recommended answer doesn't do anything remotely helpful (pun-intended). EDIT - excuse me, the multitude of suggestions contained in the top 2 answers were useless; 3rd one (git branch test origin/test) is what works. Glad the top 2 have 20x the number of votes... – dwanderson – 2017-03-09T16:35:18.830


Maybe you have a file named 'test' in your work tree, see for detail.

– oldman – 2017-07-10T07:50:10.510

1Here's what worked for me in 2017. Two steps:

1) git fetch origin <branchname> 2) git checkout -b <branchname> origin/<branchname> – user3089840 – 2017-08-03T18:42:38.460

git checkout --track origin/test – ifelsemonkey – 2018-10-25T01:37:10.497


7 796


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

git fetch
git checkout test

As user masukomi points out in a comment, git checkout test will NOT work in modern git if you have multiple remotes. In this case use

git checkout -b test <name of remote>/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


Posted 2009-11-23T14:23:46.150

Reputation: 86 499

53...and you would do this with git fetch origin test – Andrew – 2012-01-22T23:24:42.487

19Error: "git checkout: updating paths is incompatible with switching branches. Did you intend to checkout origin/test which can not be resolved as commit?" – Xeoncross – 2012-09-11T20:35:59.240

354To 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 – 2009-11-23T15:24:13.190


@Xeoncross Read this answer how to fix it

– Gramic – 2012-10-02T09:32:38.590


I was getting the same error, and I just had to do a git fetch first: Git checkout on a remote branch does not work

– mltsy – 2012-11-20T20:51:23.947

In case this helps anyone- for this to work for me, I had to run git pull origin first – mikermcneil – 2013-04-05T01:04:08.453

1fetch first: git fetch --all – orluke – 2013-05-31T01:12:00.913

3As pointed out by Jakub - you should be able to use a shorter and safer checkout command since 1.6.6. Please consider selecting his answer. – inger – 2013-07-24T09:50:30.503

2This doesn't define "fetch" other than to say that you can fetch with the fetch command. An obvious new user might appreciate a word or two about what is being gotten from where to where. – Lee Meador – 2013-09-19T17:05:52.980

Worked for me, but after executing git remote update before git checkout -b test origin/test – Radian – 2013-11-20T10:53:55.597

@AdriánSalgado the -b causes a new branch to be created as if you had written "git branch test origin/test; git checkout test" – hallski – 2014-01-26T23:24:37.977

1@jordan-dea-mattson I'm not confident enough to edit the answer but I just tried this shorter version and it works: git fetch; git checkout test. It automatically sets up the correct remote tracking branch. – Andy Baker – 2014-02-21T09:39:21.473

Where does git get the URL info from? Will I end up checking out a branch inside the current workspace? – Nick – 2014-07-07T23:51:07.277

75git 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 – 2014-09-16T15:34:55.167

The previous comment seems like it needs to be a lot more prominent - can someone add this to the answer? – kcrisman – 2014-10-30T15:34:45.523

Sorry to tell you, this doesnt work. it will create a local branch that has no idea that the remote branch is related to it – FRR – 2015-02-05T15:11:17.747

1If I could only upvote this answer every time I have to google it again... – Kato – 2015-02-19T19:43:55.827

I had multiple remotes where the branch only existing in one, yet it still didn't work. So I've updated the answer to be less specific. – samthebest – 2015-03-04T16:27:14.917

5"git checkout test will NOT work in modern git if you have multiple remotes" -- I have a single remote and I still need to do git checkout -b test origin/test... – Berislav Lopac – 2015-11-17T09:33:55.863

yeah, but after making changes, then what? – botbot – 2015-12-26T06:03:17.037

And do not forget to pull the latest changes git pull – TodStoychev – 2016-01-25T12:44:21.610

Do not include origin/ at the front of my branch spec when checking it out – Alok Adhao – 2016-05-17T11:17:22.637

4@BerislavLopac and @samethebest the git checkout test was not working for me either with multiple remotes but only one branch with the name test... but I finally realized my problem was that I had a folder named test. So git was just checking out that folder. Thus I still needed to do git checkout -b test origin/test – Ryan Walls – 2016-06-27T14:35:44.037

Hi @hallski, I want to know what's the purpose of git fetch origin? Thanks :) – KarenAnne – 2016-08-30T02:57:53.820

@KarenAnne It fetches the latest changes from the repository 'origin' (often your remote remote repository, for example on Github). – hallski – 2016-09-21T12:44:44.800

this is bad semantics on git's part "git checkout -b test origin/test" it should be this instead "git checkout -b origin/test test", oh well – Alexander Mills – 2016-10-14T23:52:06.567

130If 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 – 2011-11-04T14:38:53.333

1 128

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

Jakub Narębski

Posted 2009-11-23T14:23:46.150

Reputation: 208 170

29Unsurprising, 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 – 2012-04-16T13:24:49.217

10"modern git"--for the record, (approx) what version are you referring to? Sometimes we have to work on systems running older distros. – Craig McQueen – 2012-08-28T02:30:55.610


"modern git" in this context is git 1.6.6

– Bobby Norton – 2013-03-19T20:29:52.597

1This should now be the accepted answer IMO. If anyone knows what version of git this was introduced in, please edit the answer. – Dennis – 2013-05-24T07:49:33.663

1@knite: The git checkout test is more DWIM-my. Not only it adds --track option, but when checking out nonexistent branch it notices that there exist remote-tracking branch of the same name and automagically creates local branch for you that you can checkout (you cannot checkout remote-tracking branch, as it is controlled by outside remote repository). HTH – Jakub Narębski – 2013-06-26T07:06:58.653

1thanks, works fine in git – John Griffiths – 2013-06-27T14:57:16.310

I had pushed a branch to my repo and then modified the README via github. I then wanted to update my local copy. Using git checkout mybranch lead to git suggesting me to use git pull because mybranch already existed. Thanks for leading me towards the solution! – Meredith – 2013-07-19T22:17:30.667

1I'm on git version (Apple Git-33) and git checkout test didn't work for me. Had to fall back to the accepted answer. I probably have some strange config interfering somewhere. – aidan – 2013-09-18T00:55:34.347

See tcaswell's answer below if the remote of interest is not named origin – Jean Jordaan – 2013-10-16T09:03:54.270

10@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 – 2013-10-18T00:40:10.760

4Using 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 – 2013-12-03T23:59:44.180

@PandaWood Didn't work for me either, using (Apple Git-47) – LasagnaAndroid – 2014-01-08T15:46:34.190

@PandaWood @AdriánSalgado In your repo, open .git/config and check that the refspec for fetching from your remote (e.g. origin) doesn't only fetch from a specific branch. For example, this is bad: fetch = +refs/heads/master:refs/remotes/origin/master because it only fetches master. It should look like fetch = +refs/heads/*:refs/remotes/origin/*. Then use git fetch origin after that. If successful, the branch you're interested in should appear in the fetch output, and it should also appear in the output of git branch -a as a remote branch. – Dennis – 2014-01-09T02:03:16.867

@JakubNarębski What is the relevance of the lower section of your answer about the unnamed branch? With git v1.8.3.2 I'm not on an unnamed branch after checking out (and tracking) the remote branch. – Dennis – 2014-01-09T02:14:22.573

2@Dennis: git checkout &lt;non-branch&gt;, for example git checkout origin/test results in detached HEAD / unnamed branch, while git checkout test or git checkout -b test origin/test results in local branch test (with remote-tracking branch origin/test as upstream) – Jakub Narębski – 2014-01-09T08:17:56.213

Using git checkout -B origin/branch will create and checkout the desired remote branch in one line. where as -b option will only create the branch and put HEAD in detached state. – IskandarG – 2017-08-17T09:34:52.030


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.


Posted 2009-11-23T14:23:46.150

Reputation: 24 438

12This will create a local branch without switching to it. – Alex.Designworks – 2013-10-16T07:20:02.833

2Though 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 – 2013-12-04T00:04:50.527

1This 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 – 2014-06-18T20:01:53.730

git branch test works for me – Isaac Pak – 2017-02-17T15:27:20.817


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?


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.
   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.

Corey Ballou

Posted 2009-11-23T14:23:46.150

Reputation: 29 723

2I'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 – 2015-02-02T05:51:14.853

git checkout -b newbranch also works great for 1-step create and checkout a new branch based on the current branch. – Niko – 2016-05-19T13:41:18.070

2I think this is the most up-to-date (it keeps [email protected]#!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 – 2016-05-26T12:51:12.300

and when this answer doesn't work either, see this one.

– eis – 2018-01-16T12:48:42.860


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.

Sahil kalra

Posted 2009-11-23T14:23:46.150

Reputation: 4 391

27This worked for me when neither git fetch origin or git remote update created local branches. I'm not sure why. – Godsmith – 2014-09-11T08:45:56.970

1it depends on git version but that seems to be made possible with its latest release. – Aftab Naveed – 2015-10-15T23:02:40.503

4This 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 – 2015-11-13T23:15:47.563

7Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches. – Alex C – 2016-10-17T10:19:54.250

7this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0 – pdepmcp – 2017-02-17T12:46:30.340

1I had a branch of that form: "xx/xx" and This was the solution for me. – User – 2017-03-14T08:50:50.013

4Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0) – Nathan Arthur – 2017-06-19T18:43:40.200

Works as of 2017 where the git version is 2.7.4 – kisanme – 2017-08-15T06:37:25.303

1This is the only answer that worked for me on mac OS Sierra, git version 2.4.0 – Nj Subedi – 2017-10-01T14:07:47.483

At this point I don't know if this is what OP was asking or whatever but what this command allows me to do is automatically pull down fast forward commits. So for instance, after a pull request to master you don't want to wait for your IDE to reload the whole solution. Right after the PR you can run this command, then switch to master and create a new branch for the next feature. THANKS FOR THIS! – Ryanman – 2017-11-30T19:38:13.453

If this is the only thing that works for you, might want to check if you have this issue. For me, it prevented anything else from working.

– eis – 2018-01-16T14:26:13.307

This is the only answer that worked for me git version 2.15.1 – Ruan Carlos – 2018-02-25T07:25:11.720

I've gone through this like 50 times already... git fetch --all fetches the branches but does not create them. git checkout -b branchName checkouts a new branch based on your current branch. git checkout upstream/newBranch creates a branch literally called upstream/newBranch. This is the 100% correct answer. – damusix – 2018-06-05T17:56:28.567

This is the only way that worked for me on cloned repo with git clone --single-branch flag – Denis Pereverzev – 2018-09-19T11:51:59.080

Huuray, worked for me – qwebek – 2018-11-08T10:48:21.280


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.


Posted 2009-11-23T14:23:46.150

Reputation: 53 735




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.


Posted 2009-11-23T14:23:46.150

Reputation: 6 324


OK, the answer is easy... You basically see the branch, but you don't have a local copy yet!...

You need to fetch the branch...

You can simply fetch and then checkout to the branch, use the one line command below to do that:

git fetch && git checkout test

I also created the image below for you to share the differences, look at how fetch works and also how it's different to pull:

git fetch


Posted 2009-11-23T14:23:46.150

Reputation: 43 514


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.


Madhan Ayyasamy

Posted 2009-11-23T14:23:46.150

Reputation: 10 081

git clone <either ssh url /http url> - works perfectly for me – Kmeixner – 2014-12-16T16:56:14.440

Yes you are correct. Thanks for your information, I will update it very soon @warvariuc – Madhan Ayyasamy – 2015-01-08T10:45:22.187

If the remote has no master, this is not going to work. – – 2016-09-27T16:47:26.767


You can try

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


git fetch
git checkout -b local_branch_name origin/branch_name


Posted 2009-11-23T14:23:46.150

Reputation: 2 134


FYI, --track is no longer needed in newer versions of git, because it's set by default, as explained in this earlier answer.

– None – 2014-06-21T18:03:49.837


First, you need to do:

git fetch # If you don't know about branch name

git fetch origin branch_name

Second, you can check out remote branch into your local by:

git checkout -b branch_name origin/branch_name

-b will create new branch in specified name from your selected remote branch.

Mohideen ibn Mohammed

Posted 2009-11-23T14:23:46.150

Reputation: 6 869



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


Posted 2009-11-23T14:23:46.150

Reputation: 1 246


I use the following command:

git checkout --track origin/other_remote_branch


Posted 2009-11-23T14:23:46.150

Reputation: 2 901


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


Posted 2009-11-23T14:23:46.150

Reputation: 10 661


git fetch && git checkout your-branch-name

Inder Kumar Rathore

Posted 2009-11-23T14:23:46.150

Reputation: 31 564


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.


Posted 2009-11-23T14:23:46.150

Reputation: 800

2FYI, git remote update will also fetch all remote branches. – None – 2014-06-21T17:59:37.853


none of these answers worked for me. this worked:

git checkout -b feature/branch remotes/origin/feature/branch


Posted 2009-11-23T14:23:46.150

Reputation: 434


The git remote show <origin name> command will list all branches (including un-tracked branches). Then you can find the remote branch name that you need to fetch.


$ git remote show origin

Use these steps to fetch remote branches:

git fetch <origin name> <remote branch name>:<local branch name>
git checkout <local branch name > (local branch name should the name that you given fetching)


$ git fetch origin test:test
$ git checkout test


Posted 2009-11-23T14:23:46.150

Reputation: 625


Other guys and gals give the solutions, but maybe I can tell you why.

git checkout test which does nothing

Does nothing doesn't equal doesn't work, so I guess when you type 'git checkout test' in your terminal and press enter key, no message appears and no error occurs. Am I right?

If the answer is 'yes', I can tell you the cause.

The cause is that there is a file (or folder) named 'test' in your work tree.

When git checkout xxx parsed,

  1. Git looks on xxx as a branch name at first, but there isn't any branch named test.
  2. Then Git thinks xxx is a path, and fortunately (or unfortunately), there is a file named test. So git checkout xxx means discard any modification in xxx file.
  3. If there isn't file named xxx either, then Git will try to create the xxx according to some rules. One of the rules is create a branch named xxx if remotes/origin/xxx exists.


Posted 2009-11-23T14:23:46.150

Reputation: 3 668


You can start tracking all remote branches with the following Bash script:

git fetch --all
for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`
  do git branch -f --track "$branch" "origin/$branch"

Here is also a single-line version:

git fetch --all; for branch in `git branch -r --format="%(refname:short)" | sed 's/origin\///'`; do git branch --track "$branch" "origin/$branch" ; done ;


Posted 2009-11-23T14:23:46.150

Reputation: 4 966


To get newly created branches

git fetch

To switch into another branch

git checkout BranchName

Hasib Kamal

Posted 2009-11-23T14:23:46.150

Reputation: 670


Simply run git checkout with the name of the remote branch. Git will automatically create a local branch that tracks the remote one:

git fetch
git checkout test

However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use. In that case you can use either:

git checkout --track origin/test


git checkout -b test origin/test

In 2.19, Git learned the checkout.defaultRemote configuration, which specifies a remote to default to when resolving such an ambiguity.

Eugene Yarmash

Posted 2009-11-23T14:23:46.150

Reputation: 81 916


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'

Mehedi Hasan

Posted 2009-11-23T14:23:46.150

Reputation: 130


I was stuck in a situation seeing error: pathspec 'desired-branch' did not match any file(s) known to git. for all of the suggestions above. I'm on git version

So this worked for me:

git fetch origin desired-branch
git checkout -b desired-branch FETCH_HEAD

The explanation behind is that I've noticed that when fetching the remote branch, it was fetched to FETCH_HEAD:

$ git fetch origin desired-branch
    * branch            desired-branch -> FETCH_HEAD


Posted 2009-11-23T14:23:46.150

Reputation: 411


Fetch origin and checkout the branch.

git fetch origin && git checkout branch_name 


Posted 2009-11-23T14:23:46.150

Reputation: 493


If the remote branch name begins with special characteres you need to use single quotes around it in the checkout command, or else git won't know which branch you are talking about.

For example, I tried to checkout a remote branch named as #9773 but the command didn't work properly, as shown in the picture below:

enter image description here

For some reason I wondered if the sharp symbol (#) could have something to do with it, and then I tried surrounding the branch name with single quotes, like '#9773' rathen than just #9773, and it worked fine.

$ git checkout -b '#9773' origin/'#9773'

Ulysses Alves

Posted 2009-11-23T14:23:46.150

Reputation: 1 200