7300

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Juri Glass
  • 76,369
  • 8
  • 31
  • 46
  • 9
    @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 01 '14 at 12:43
  • 25
    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 00:16
  • 9
    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 '16 at 17:41
  • 24
    I 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 Mar 09 '17 at 16:35
  • 6
    Maybe you have a file named 'test' in your work tree, see https://stackoverflow.com/a/45006389/792416 for detail. – oldman Jul 10 '17 at 07:50
  • 2
    Here's what worked for me in 2017. Two steps: 1) git fetch origin 2) git checkout -b origin/ – user3089840 Aug 03 '17 at 18:42
  • `git checkout --track origin/test` – eigenfield Oct 25 '18 at 01:37
  • 1
    tldr; The syntax is like `git checkout -b `. – Honey Apr 15 '19 at 21:17
  • We now have a new command to checkout branches, its the switch. git swtich – Humble Bee Apr 21 '21 at 07:41

35 Answers35

9926

With One Remote

Jakub's answer actually improves on this. With Git versions ≥ 1.6.6, with only one remote, you can 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

or the shorthand

git checkout -t <name of remote>/test

With >1 Remotes

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
Harry B
  • 2,511
  • 1
  • 17
  • 40
hallski
  • 108,267
  • 4
  • 30
  • 21
  • 426
    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
  • 145
    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 04 '11 at 14:38
  • 58
    ...and you would do this with `git fetch origin test` – Andrew Jan 22 '12 at 23:24
  • 22
    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
  • 2
    @Xeoncross Read this answer [how to fix it](http://stackoverflow.com/a/12320930/18788) – Gramic Oct 02 '12 at 09:32
  • 7
    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](http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work) – mltsy Nov 20 '12 at 20:51
  • In case this helps anyone- for this to work for me, I had to run `git pull origin` first – mikermcneil Apr 05 '13 at 01:04
  • 1
    fetch first: `git fetch --all` – orluke May 31 '13 at 01:12
  • 3
    As 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 Jul 24 '13 at 09:50
  • 2
    This 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 Sep 19 '13 at 17:05
  • Worked for me, but after executing `git remote update` before `git checkout -b test origin/test` – tabdulradi Nov 20 '13 at 10:53
  • @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 Jan 26 '14 at 23:24
  • 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 Feb 21 '14 at 09:39
  • Where does git get the URL info from? Will I end up checking out a branch inside the current workspace? – Nick Jul 07 '14 at 23:51
  • 89
    `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
  • The previous comment seems like it needs to be a lot more prominent - can someone add this to the answer? – kcrisman Oct 30 '14 at 15:34
  • 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 Feb 05 '15 at 15:11
  • 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 Mar 04 '15 at 16:27
  • 6
    "`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 Nov 17 '15 at 09:33
  • yeah, but after making changes, then what? – botbot Dec 26 '15 at 06:03
  • And do not forget to pull the latest changes ```git pull``` – Todor Todorov Jan 25 '16 at 12:44
  • Do not include origin/ at the front of my branch spec when checking it out – Alok Adhao May 17 '16 at 11:17
  • 6
    @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 Jun 27 '16 at 14:35
  • Hi @hallski, I want to know what's the purpose of `git fetch origin`? Thanks :) – KarenAnne Aug 30 '16 at 02:57
  • @KarenAnne It fetches the latest changes from the repository 'origin' (often your remote remote repository, for example on Github). – hallski Sep 21 '16 at 12:44
  • 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 Oct 14 '16 at 23:52
  • 1
    "This will fetch all of the remote branches for you." is not a true statement. – AKWF Dec 20 '18 at 13:19
  • The syntax used in this answer is: `git checkout [-q] [-f] [-m] [[-b|-B|--orphan] ] []` which is basically: `git checkout -b test origin/test`. @hallski maybe you can include the syntax in the answer too – Honey Apr 13 '20 at 20:02
  • answer also knowed as: _how to get rich with upvotes_ – Lucas Vazquez May 04 '20 at 19:05
  • I've also seen git ```fetch && git checkout ``` work – Alex Bishka Jun 23 '20 at 00:27
  • 1
    git has a newer command for this - `git switch ` – Nitsan Avni Aug 05 '20 at 12:03
  • One more thing would be nice to elaborate, i.e. checking out the branch **without** adding a remote (let's say *alien* branch). `git fetch $REMOTE_URL $BRANCH && git checkout -b $LOCAL_BRANCH FETCH_HEAD` – 0andriy Jan 25 '21 at 18:02
1331

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

** EDIT (by editor not author) **

I found a comment buried below which seems to modernize this answer:

@Dennis: git checkout <non-branch>, 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 Jan 9 '14 at 8:17

emphasis on git checkout origin/test

Paul
  • 4,066
  • 2
  • 25
  • 53
Jakub Narębski
  • 268,805
  • 58
  • 209
  • 228
  • 36
    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
  • 11
    "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 02:30
  • 5
    "modern git" in this context is [git 1.6.6](https://raw.github.com/git/git/master/Documentation/RelNotes/1.6.6.txt) – Bobby Norton Mar 19 '13 at 20:29
  • 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 Jun 26 '13 at 07:06
  • 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 Jul 19 '13 at 22:17
  • 1
    I'm on git version 1.7.10.2 (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 Sep 18 '13 at 00:55
  • See tcaswell's answer below if the remote of interest is not named `origin` – Jean Jordaan Oct 16 '13 at 09:03
  • 13
    @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 00:40
  • 6
    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 03 '13 at 23:59
  • @PandaWood Didn't work for me either, using 1.8.3.4 (Apple Git-47) – LasagnaAndroid Jan 08 '14 at 15:46
  • @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 Jan 09 '14 at 02:03
  • @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 Jan 09 '14 at 02:14
  • 6
    @Dennis: `git checkout `, 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 Jan 09 '14 at 08:17
  • 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 Aug 17 '17 at 09:34
  • `git checkout origin/feature/my_branch` give me `You are in 'detached HEAD' state.` on `git version 2.20.1 (Apple Git-117)` – mrgloom Jul 02 '19 at 10:37
  • 1
    @mrgloom: what about using `git checkout feature/my_branch`, without the `origin/` prefix? – Jakub Narębski Jul 05 '19 at 15:28
594

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.

ndim
  • 30,855
  • 12
  • 43
  • 55
  • 17
    This will create a local branch without switching to it. – Alex Skrypnyk Oct 16 '13 at 07:20
  • 3
    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 04 '13 at 00: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
499

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.

Community
  • 1
  • 1
Corey Ballou
  • 39,300
  • 8
  • 60
  • 75
  • 2
    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 02 '15 at 05: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 '16 at 13:41
  • 2
    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. – CodeClown42 May 26 '16 at 12:51
  • and when this answer doesn't work either, see [this one](https://stackoverflow.com/a/19442557/365237). – eis Jan 16 '18 at 12:48
  • just to add, if you `git fetch other_remote only_branch`, you still get the `fatal` error. You have to `git fetch other_remote` without the branch name. Weird design. – est Dec 29 '18 at 02:56
  • This worked for me! – Antonio Pavicevac-Ortiz Apr 22 '21 at 21:15
296

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
  • 6,598
  • 3
  • 21
  • 29
  • 38
    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 08:45
  • 1
    it depends on git version but that seems to be made possible with its latest release. – Aftab Naveed Oct 15 '15 at 23:02
  • 6
    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
  • 7
    Worked seamlessly, especially when having cloned a single branch from a remote with multiple branches. – Alex C Oct 17 '16 at 10:19
  • 8
    this worked for me too, where accepted answers and other high voted didn't. My git version is 2.5.0 – pdepmcp Feb 17 '17 at 12:46
  • 3
    I had a branch of that form: "xx/xx" and This was the solution for me. – User Mar 14 '17 at 08:50
  • 6
    Does anyone have any idea why this works when everything else doesn't? (I'm on git 2.13.0) – Nathan Arthur Jun 19 '17 at 18:43
  • Works as of 2017 where the git version is 2.7.4 – kisanme Aug 15 '17 at 06:37
  • 1
    This is the only answer that worked for me on mac OS Sierra, git version 2.4.0 – Nj Subedi Oct 01 '17 at 14:07
  • 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 Nov 30 '17 at 19:38
  • 3
    If this is the only thing that works for you, might want to check if you have [this issue](https://stackoverflow.com/questions/11623862/git-fetch-doesnt-fetch-all-branches). For me, it prevented anything else from working. – eis Jan 16 '18 at 14:26
  • This is the only answer that worked for me `git version 2.15.1` – Ruan Carlos Feb 25 '18 at 07:25
  • 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 Jun 05 '18 at 17:56
  • 1
    This is the only way that worked for me on cloned repo with ```git clone --single-branch``` flag – Denis Pereverzev Sep 19 '18 at 11:51
  • 1
    Same experience as others here; this worked, higher voted answers did not. – Andrew Price Jul 12 '19 at 20:04
  • 4
    The users ending here did a checkout of a single branch. This leads to an entry in `.git/config` like `fetch = +refs/heads/xybranch:refs/remotes/master/xybranch` and thus fetch will only fetch that one branch. You could also hack your `.git/config` and replace that branch name with a `*` which reads `fetch = +refs/heads/*:refs/remotes/master/*`... so this solution is the clean way to track further branches and thus fetch and check these out. – bebbo Feb 12 '20 at 20:29
  • 1
    This worked for me while nothing else worked. I had cloned the remote repo as a single branch by `git clone --single-branch --branch ` – DeepSpace101 May 20 '20 at 18:28
  • this actually worked, while all the other hints didn't. I am on git version 2.17.1 – sal Oct 08 '20 at 08:49
  • Don't forget `git checkout local_branch_name` to switch to a new branch after you fetch a remote branch. – Serhii Popov Jan 04 '21 at 15:35
111

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
tacaswell
  • 73,661
  • 15
  • 189
  • 183
110

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
matanster
  • 13,785
  • 14
  • 75
  • 135
  • 2
    Do you realize that this is an extract of this [answer](http://stackoverflow.com/a/13770793/2307070) – Thomas Ayoub Feb 21 '16 at 11:03
  • 15
    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 '16 at 11:34
  • 1
    git fetch is the safe option. Try fetch before using pull. Both download remote content, but while fetch does not change local state, pull will immediately change the local state. – Peter Szalay Apr 03 '19 at 08:07
  • do not foget to do git pull afetr that ... – mishaikon Feb 16 '21 at 10:34
93

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

Noel Yap
  • 15,499
  • 17
  • 77
  • 123
Alireza
  • 83,698
  • 19
  • 241
  • 152
  • @DmitriZaitsev yes, it will work, if the remote branch is there, and you do fetch, you will get the branch locally... git fetch && git checkout test..So this works, unless there is no remote branch, but the question saying there is already a remote branch there... – Alireza Apr 25 '18 at 05:13
  • The way I see the question, `test` looks like a new branch, so it is not likely to be present locally. Otherwise you could pull it easier with single `git pull` command. – Dmitri Zaitsev Apr 25 '18 at 12:51
  • @DmitriZaitsev, yes, that's why I said git fetch, that check for remote branches which just created, pull can bring other unwanted stuffs in, but fetch makes all branches available if you already have the repo locally... – Alireza Apr 25 '18 at 13:36
  • Wouldn't `fetch` without `pull` leave changes in the fetched copy but not in the local branch, leading to your local branch not being up-to-date? – Dmitri Zaitsev Apr 25 '18 at 14:38
68

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.

Ondrej
  • 442
  • 1
  • 3
  • 9
Madhan Ayyasamy
  • 13,442
  • 3
  • 16
  • 18
44

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
uma
  • 2,698
  • 22
  • 20
  • 3
    FYI, `--track` is no longer needed in newer versions of git, because it's set by default, as explained in [this earlier answer](http://stackoverflow.com/a/1783437/456814). –  Jun 21 '14 at 18:03
36

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mohideen bin Mohammed
  • 14,471
  • 7
  • 86
  • 95
32

I use the following command:

git checkout --track origin/other_remote_branch
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
priyankvex
  • 5,092
  • 5
  • 23
  • 42
  • 14
    This answer would be a lot more useful if you explain why you are using it this way. i.e. why someone should use '--track' and so on... – Matt Friedman Nov 20 '17 at 03:19
28

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

madhead
  • 25,830
  • 14
  • 131
  • 174
sreekumar
  • 2,059
  • 1
  • 18
  • 25
25

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
prusswan
  • 6,473
  • 3
  • 37
  • 57
Kris
  • 16,882
  • 6
  • 79
  • 99
25

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

or

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
  • 119,667
  • 33
  • 277
  • 336
23

none of these answers worked for me. this worked:

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

brianyang
  • 790
  • 7
  • 12
  • 1
    thanks. I was wondering if I had to use the full path (_remotes/origin/feature/branch_) that I saw in `git` when calling `git branch -a` command, but I wasn't for sure, so I just used `git checkout -b apps/FEATURE/branch origin/apps/FEATURE/branch` and it appeared to work. Message: ```Branch 'apps/FEATURE/branch' set up to track remote branch 'apps/FEATURE/epicBranch' from 'origin'. Switched to a new branch 'apps/FEATURE/branch'``` – Chris22 Jul 27 '18 at 15:41
22

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

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
From github.com:MYTEAM/my-repo
    * branch            desired-branch -> FETCH_HEAD
alisa
  • 948
  • 2
  • 9
  • 20
21

git fetch && git checkout your-branch-name

Inder Kumar Rathore
  • 37,431
  • 14
  • 121
  • 176
17

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
webdevguy
  • 907
  • 9
  • 16
  • 2
    FYI, `git remote update` **will also fetch all remote branches**. –  Jun 21 '14 at 17:59
17

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.

Example:

$ 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)

Example:

$ git fetch origin test:test
$ git checkout test
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Thushan
  • 992
  • 11
  • 14
  • @hallski answered not working 2.15.1 versions but i have reduce .git file weigth clone only 1 branch and filter history with --depth flag. for example `$ git clone -b release --single-branch --depth 5 https://github.com/user/repo.git` Wron't information `$ git remote show origin`this does not listed all remote branch with single branch cloned repositories. – Qh0stM4N Jan 30 '18 at 13:52
14

Fetch from the remote and checkout the branch.

git fetch <remote_name> && git checkout <branch_name> 

E.g.:

git fetch origin && git checkout feature/XYZ-1234-Add-alerts

Pranav
  • 831
  • 8
  • 19
12

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.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
oldman
  • 4,136
  • 2
  • 17
  • 15
  • thanks, kept trying to figure out why git wasn't doing anything. – Mike R Nov 26 '18 at 19:05
  • Thanks, for the `Does nothing` != `Doesn't work` hint and the fallback description. Quick Solution: Use `git checkout xxx --` to use step 3 right away. Git uses the `--` to differ between branch and files, if no filename is given in the command it will skip step 2. – pixelbrackets Oct 16 '20 at 08:37
12

To get newly created branches

git fetch

To switch into another branch

git checkout BranchName
Community
  • 1
  • 1
Hasib Kamal
  • 1,971
  • 20
  • 26
11

git checkout -b "Branch_name" [ B means Create local branch]

git branch --all

git checkout -b "Your Branch name"

git branch

git pull origin "Your Branch name"

successfully checkout from the master branch to dev branch

enter image description here

Keshav Gera
  • 8,200
  • 1
  • 56
  • 43
8

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

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

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 ;
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
OzzyCzech
  • 7,505
  • 2
  • 38
  • 27
8

to get all remote branches use this :

git fetch --all

then checkout to the branch :

git checkout test
Zahra Badri
  • 738
  • 8
  • 17
6

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 fortunately it worked fine.

$ git checkout -b '#9773' origin/'#9773'
Ulysses Alves
  • 1,960
  • 1
  • 18
  • 32
  • In many unix shells the `#` character is used for comments so anything after the `#` will be ignored. This is a shell thing and not something specific to git. Using quotes of a backslash before the `#` should be enough. – Paulo Scardine Nov 07 '18 at 12:12
6

For us, it seems the remote.origin.fetch configuration gave a problem. Therefore, we could not see any other remote branches than master, so git fetch [--all] did not help. Neither git checkout mybranch nor git checkout -b mybranch --track origin/mybranch did work, although it certainly was at remote.

The previous configuration only allowed master to be fetched:

$ git config --list | grep fetch
remote.origin.fetch=+refs/heads/master:refs/remotes/origin/master

Fix it by using * and fetch the new information from origin:

$ git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'

$ git fetch
...
 * [new branch] ...
...

Now we could git checkout the remote branch locally.

No idea how this config ended up in our local repo.

hzpc-joostk
  • 181
  • 2
  • 4
6

Just run these two commands and you should be good to go.

git checkout <branch-name>
git pull <remote> <branch-name>
Craigo
  • 2,630
  • 22
  • 18
Sateesh
  • 524
  • 6
  • 5
  • This was the answer that worked for me, as my branch name had a '/' in it. So, Git was getting confused with with the other answers saying to do /. – Craigo May 28 '21 at 00:20
5

I used that one:

git fetch origin
git reset --hard origin/{branchname}
Andrii Sukhoi
  • 370
  • 2
  • 10
4

I always do: git fetch origin && git checkout --track origin/branch_name

M. Wojcik
  • 1,058
  • 2
  • 13
  • 23
3

Use fetch to pull all your remote

   git fetch --all

To list remote branches:

   git branch -r

For list all your branches

   git branch -l
   >>outpots like-
     * develop
       test
       master

To checkout/change a branch

   git checkout master
Nasir Khan
  • 547
  • 1
  • 5
  • 18
2

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'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mehedi Hasan
  • 269
  • 3
  • 8
1

There are many alternatives, for example:

  • Alternative 1:

    git fetch && git checkout test
    

    It's the most simple way.

  • Alternative 2:

    git fetch
    git checkout test
    

    It's the same but in two steeps.

Javier C.
  • 6,011
  • 4
  • 33
  • 46
1

You can add a new branch test on local and then use:

git branch --set-upstream-to=origin/test test
Kaaveh Mohamedi
  • 402
  • 5
  • 22