2550

My colleague and I are working on the same repository. We've branched it into two branches, each technically for different projects, but they have similarities, so we'll sometimes want to commit back to the *master from the branch.

However, I have the branch. How can my colleague pull that branch specifically?

A git clone of the repository does not seem to create the branches locally for him, though I can see them live on unfuddle after a push on my end.

Also, when I originally made the branch, I did -b checkout. Does that make much difference?

$ git branch -r
origin/HEAD -> origin/master
origin/daves_branch
origin/discover
origin/master

$ git fetch origin discover
$ git checkout discover

These are the commands I ran. But it definitely is not working.

I want to be able to check out that branch and then push and commit back just the branches changes from various collaborators or workstations.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
David
  • 28,483
  • 10
  • 38
  • 68

31 Answers31

3437

Update: Using Git Switch

All of the information written below was accurate, but a new command, git switch has been added that simplifies the effort.

If daves_branch exists on the remote repository, but not on your local branch, you can simply type:

git switch daves_branch

Since you do not have the branch locally, this will automatically make switch look on the remote repo. It will then also automatically set up remote branch tracking.

Note that if daves_branch doesn't exist locally you'll need to git fetch first before using switch.


Original Post

You need to create a local branch that tracks a remote branch. The following command will create a local branch named daves_branch, tracking the remote branch origin/daves_branch. When you push your changes the remote branch will be updated.

For most recent versions of Git:

git checkout --track origin/daves_branch

--track is shorthand for git checkout -b [branch] [remotename]/[branch] where [remotename] is origin in this case and [branch] is twice the same, daves_branch in this case.

For Git 1.5.6.5 you needed this:

git checkout --track -b daves_branch origin/daves_branch

For Git 1.7.2.3 and higher, this is enough (it might have started earlier, but this is the earliest confirmation I could find quickly):

git checkout daves_branch

Note that with recent Git versions, this command will not create a local branch and will put you in a 'detached HEAD' state. If you want a local branch, use the --track option.

Full details are here: 3.5 Git Branching - Remote Branches, Tracking Branches

Zach Saucier
  • 21,903
  • 12
  • 75
  • 126
ralphtheninja
  • 107,622
  • 20
  • 101
  • 118
  • 7
    Does this need to be proceeded with a git fetch discover? – David Mar 02 '12 at 17:45
  • 1
    Git fetch is only needed if something has changed on the remote, e.g. if Dave has pushed a branch to the main repo and your repo doesn't yet have any references to it. – ralphtheninja Mar 02 '12 at 17:48
  • Which is actually true in this instance. – David Mar 02 '12 at 17:52
  • 146
    "git fetch" to make sure your repo is updated with remote references and "git checkout --track origin/discover" should be enough. Then you can commit to that branch and a "git push" to sync the remote with your changes. – ralphtheninja Mar 02 '12 at 17:54
  • 33
    I tried this and got "fatal: git checkout: updating paths is incompatible with switching branches. Did you intend to checkout 'upstream/develop' which can not be resolved as commit?". Am I doing something wrong? – Neil Barnwell Apr 08 '12 at 22:40
  • 1
    @NeilBarnwell As Magnus said, do a `git fetch` – Jamie Apr 30 '12 at 16:13
  • 11
    @NeilBarnwell I needed to do a `git fetch --all` (which fetches all remotes) to get this to work. – Lucas Wilson-Richter Sep 21 '12 at 05:26
  • 2
    @NeilBarnwell Read this answer [how to fix it](http://stackoverflow.com/a/12320930/18788) – Gramic Oct 02 '12 at 09:31
  • @NeilBarnwell this worked for me: `git remote show origin` `git remote update` (explanation at http://stackoverflow.com/questions/945654/git-checkout-on-a-remote-branch-does-not-work) – Peter Ehrlich Jun 09 '13 at 05:54
  • 3
    `fatal: cannot update paths and switch to branch '...' at the same time. Did you intend to checkout 'origin/...' which can not be resolved as commit?` ??? – codeling Jun 16 '13 at 12:37
  • 43
    Looks like git 1.5.6.5 needs this instead: git checkout --track -b origin/daves_branch – Charlie Jul 12 '13 at 13:31
  • 3
    I ran into `warning: refname 'origin/' is ambigious`. The fix for me was to instead run `git checkout --track remotes/origin/` – JoshP Feb 05 '14 at 17:11
  • Thanks @Charlie, on GIT 1.9 I had to do a `-b` too. – Honiix Mar 28 '14 at 10:02
  • 36
    This made a mess for me, it created a local branch named origin/ which is now ambiguous to the remote branch origin/ and I don't know how to get rid of the crazy local branch! – Alan Moore Jul 09 '15 at 17:00
  • 1
    Yeah I would not use this command. It may be because I have multiple remotes in my config, but I am now in the same boat as 'Alan Moore'. I just deleted the local repo and then cloned it from GitHub again... But before I did that, I was having a hard time undoing whatever the above command did. Even after `$ git branch -d origin/mybranch`... – derekmx271 Jul 10 '15 at 17:46
  • 24
    You need to add the local branch name explicitly, otherwise git creates a new local branch with the full branch path, as @AlanMoore and @derekmx271 stated above: `git checkout -b --track daves_branch origin/daves_branch` – Mike Scott Jul 22 '15 at 15:57
  • 8
    @MikeScott `git checkout --track -b daves_branch origin/daves_branch`, otherwise it tells me _--track is not a valid branch_. – Roberto Feb 14 '16 at 21:23
  • 3
    You do need to do `git fetch` first if the branch is not present locally. – Richard Whitehead Feb 10 '17 at 10:39
  • @Mike Scott Regarding "`You need to create a local branch that tracks a remote branch`". Does it matter what name is given to the local branch or should it match that of the remote repo's? – Minh Tran Jun 06 '18 at 21:34
  • `git fetch origin mybranch` neither showed any error, nor did it show mybranch in branch list with `git branch -v` .. And then doing `git checkout mybranch` had no effect. So I tried, `git checkout --track origin/mybranch`, and it worked! Thanks @ralphtheninja – Prashant Jul 11 '19 at 08:31
  • Can this be done to create local versions of all remote branches, without specifying each individually? – David Foster Oct 30 '19 at 08:31
1049

I have used fetch followed by checkout...

git fetch <remote> <rbranch>:<lbranch>
git checkout <lbranch>

...where <rbranch> is the remote branch or source ref and <lbranch> is the as yet non-existent local branch or destination ref you want to track and which you probably want to name the same as the remote branch or source ref. This is explained under options in the explanation of <refspec>.

Git is so smart it auto completes the first command if I tab after the first few letters of the remote branch. That is, I don't even have to name the local branch, Git automatically copies the name of the remote branch for me. Thanks Git!

Also as the answer in this similar Stack Overflow post shows, if you don't name the local branch in fetch, you can still create it when you check it out by using the -b flag. That is, git fetch <remote> <branch> followed by git checkout -b <branch> <remote>/<branch> does exactly the same as my initial answer. And evidently, if your repository has only one remote, then you can just do git checkout <branch> after fetch and it will create a local branch for you. For example, you just cloned a repository and want to check out additional branches from the remote.

I believe that some of the documentation for fetch may have been copied verbatim from pull. In particular the section on <refspec> in options is the same. However, I do not believe that fetch will ever merge, so that if you leave the destination side of the colon empty, fetch should do nothing.

NOTE: git fetch <remote> <refspec> is short for git fetch <remote> <refspec>: which would therefore do nothing, but git fetch <remote> <tag> is the same as git fetch <remote> <tag>:<tag> which should copy the remote <tag> locally.

I guess this is only helpful if you want to copy a remote branch locally, but not necessarily check it out right away. Otherwise, I now would use the accepted answer, which is explained in detail in the first section of the checkout description and later in the options section under the explanation of --track, since it's a one-liner. Well... sort of a one-liner, because you would still have to run git fetch <remote> first.

FYI: The order of the <refspecs> (source:destination) explains the bizarre pre Git 1.7 method for deleting remote branches. That is, push nothing into the destination refspec.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Mark Mikofski
  • 16,534
  • 2
  • 46
  • 82
  • 8
    You assume Git autocompletion is setup. http://code-worrier.com/blog/autocomplete-git/ – antonagestam Aug 29 '13 at 09:19
  • Yes for git commands to be auto-completed then [git-completion.bash](https://github.com/git/git/blob/master/contrib/completion/git-completion.bash) must be enabled. Directions "to use these routines" is included at the top of the source file. You may also wish to use [git-prompt](https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh) as well (directions included in source). Note on a Macintosh, `~/.bashrc` is `~/.bash-profile`. During Git installation on Macintosh, these two files are not included, and the `.bash-profile` file in `$HOME` is not altered to add theses commands. – Mark Mikofski Aug 29 '13 at 17:01
  • 2
    This worked for me to get the remote code into a local branch. It did not however get my local branch to track the remote branch. – aknosis Sep 17 '14 at 22:35
  • And approve that switched to branch that you want to be in `git branch`. – K-Gun Jan 15 '15 at 15:40
  • 1
    For some reason, `git fetch remote branch` didn't add a branch head at all for me, though all the refs got fetched, so when I tried to follow the steps in the accepted answer, I got the error that `pathspec did not match any file(s) known to git.`, but the `rbranch:lbranch` approach worked. Interestingly, it also fetched all the tags that started with the same prefix, like it is a wildcard (`rbranch*`). – haridsv Jun 30 '15 at 08:36
  • 5
    Nit: git doesn't do the autocompleting, it is the bash shell that is doing it. – legalize Dec 29 '15 at 20:44
  • This really helped me - I simply wanted to fetch a remote branch that my colleague had added to the origin. A lot of documentation seems to me to be missing the critical piece about adding the name of the new local branch that you want the fetch to be directed to - `:`. Thank you very much. – skwidbreth Aug 25 '16 at 16:14
  • 2
    FWIW, I think the difference between this answer and the accepted answer is that this one tells you to do the `fetch` command. It the accepted answer makes sense though because OP notes that he already did the fetch. That's at least the issue I ran into. – tenor528 Mar 16 '17 at 15:17
  • This is perfect for tracking a remote branch of a repo you've previously forked (e.g. tracking the dev branch of an OSS project you contribute to). Thanks! – ConorB Sep 25 '17 at 10:06
  • This specific approach has the added benefit of only retrieving the desired branch (rather than all remote branch references), this was important for me on a slow connection interfacing with a rather large repo. – lase Sep 29 '17 at 15:57
  • I just did `git fetch origin ` now how can I locally check that branch out? NeverMind. I figured it out. I can simply do `git checkout ` and it will just create a local branch with the name of ``. I'm just not so sure how I should process that. I wrote a question [here](https://stackoverflow.com/questions/56464499/how-does-git-checkout-work-after-git-fetch) if you can answer... – Honey Jun 05 '19 at 16:22
  • 1
    Hi @Honey, as the [answer above](https://stackoverflow.com/a/16095458/1020470) states: "And evidently if your repo has only one remote, then you can just do git checkout after fetch and it will create a local branch for you. EG: You just cloned a repo and want to check out additional branches from the remote." Also in the [git-checkout docs](https://git-scm.com/docs/git-checkout): "If is not found but there does exist a tracking branch in exactly one remote (call it ) with a matching name, treat as equivalent to: `$ git checkout -b --track /`" – Mark Mikofski Jun 05 '19 at 16:35
  • My bad for not reading it in full. Can you copy/past what you wrote as an answer? – Honey Jun 05 '19 at 16:37
  • 1
    This also works with partial clones: Example: `git fetch --depth=20 :` – koppor Jul 14 '19 at 19:19
  • Wow! Git is SO smart that it can default to name the new branch the same as the remote one... – Juan Jul 27 '20 at 11:14
  • This worked for me! Thank you! – Kidus Tekeste Feb 28 '21 at 02:15
  • i get `fatal: Refusing to fetch into current branch refs/heads/fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize of non-bare repository` – Fuseteam Apr 18 '21 at 15:21
  • @Fuseteam is it possible you are trying to update the branch that you're already on? See: https://stackoverflow.com/questions/2236743/git-refusing-to-fetch-into-current-branch. Try to create a remote first: `git remote add FOO url/of/repo` then fetch the branch from the remote: `git fetch FOO fuse_keybindings...`. This may create a new local branch or update the existing local branch depending. See [the git-fetch docs](https://git-scm.com/docs/git-fetch) & good luck! – Mark Mikofski Apr 20 '21 at 23:09
  • hi @MarkMikofski i am trying to fetch a remote branch to have it locally, I have already added the remote, and I tried `git fetch --all` to no avail. i can't remember if I tried `git fetch remote-name branch` thanks for the tip – Fuseteam Apr 21 '21 at 16:45
426

If you are trying to "checkout" a new remote branch (that exists only on the remote, but not locally), here's what you'll need:

git fetch origin
git checkout --track origin/<remote_branch_name>

This assumes you want to fetch from origin. If not, replace origin by your remote name.

paneer_tikka
  • 5,403
  • 1
  • 17
  • 16
  • 2
    Worked for me, I hadn't fetched the correct remote branch so the accepted answer kept failing for me with a confusing message. +1 – Nathan Hinchey May 16 '17 at 16:32
  • Usually, I use git fetch, but the thing is what is the difference between git fetch and git fetch origin ? – Ping Woo Jul 26 '19 at 21:12
  • @PingWoo Assuming that the branch you want to fetch resides in _origin_ remote, both `git fetch` and `git fetch remote` will do the same thing. If you need to fetch from a remote other than `origin`, you could do that using `git fetch `. This situation is highly uncommon, just mentioned here for completeness. – paneer_tikka Aug 06 '19 at 10:04
  • i get `fatal: 'fusedwm/fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' is not a commit and a branch 'fuse_keybindings-setborderpx-alphabar-transparency-monrules-nowarpresize' cannot be created from it` – Fuseteam Apr 18 '21 at 15:22
137

To checkout myBranch that exists remotely and not a locally - This worked for me:

git fetch --all
git checkout myBranch

I got this message:

Branch myBranch set up to track remote branch myBranch from origin
Switched to a new branch 'myBranch'
David
  • 1,523
  • 1
  • 12
  • 12
  • 1
    In other words you don't have to write `-t`? – Andrew Samuelsen Apr 07 '14 at 16:47
  • 4
    I think there's an error in this answer. I originally did the command without `-t` and got `You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.` because there wasn't a local branch of the same name. I had to re-run with `-t` to fix. – stanri Sep 23 '15 at 16:38
  • 1
    This worked well for me - my colleague had added a new remote branch that I wanted to add to my local repo. I kept fetching but didn't see the new branch appearing locally. Didn't realize that I could just run `checkout` for it to create it. Thanks! – skwidbreth Jul 22 '16 at 16:23
  • 2
    depends on git versions. Latest version, as said elsewhere here, only needs git checkout . BE CAREFUL WITH SAME-NAME LOCAL vs REMOTE BRANCHES - they will mess stuff up – leRobot Jul 14 '17 at 11:20
  • `--all` is never a good idea, because it will download every file on every branch. It will take more time and space. It's better to be specific with the branch name and do like [this](https://stackoverflow.com/a/16095458/5175709) – Honey Apr 13 '20 at 19:51
73

The easiest way to do it, at least for me:

git fetch origin <branchName> # Will fetch the branch locally
git checkout <branchName> # To move to that branch
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
James Rochabrun
  • 2,459
  • 17
  • 17
  • 1
    Does not bring other branches – Benjamin Harel Oct 17 '17 at 08:23
  • 17
    @BenjaminHarel the question says "fetch a remote branch" not all branches. for that follow this may be helpfull for you https://stackoverflow.com/questions/10312521/how-to-fetch-all-git-branches – James Rochabrun May 02 '18 at 23:16
  • 2
    After using this `fetch` command required branch will be available on local machine. `git checkout -b 'your_branch' origin/'remote branch'` is required to checkout this branch. – Abhijeet Jan 02 '19 at 03:58
  • 1
    absolutely charm. This is the easiest wat to get only particular remote Branch locally – Anil Gupta Oct 02 '20 at 05:52
68

Use git branch -a (both local and remote branches) or git branch -r (only remote branches) to see all the remotes and their branches. You can then do a git checkout -t remotes/repo/branch to the remote and create a local branch.

There is also a git-ls-remote command to see all the refs and tags for that remote.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Daniel Lee
  • 6,819
  • 1
  • 41
  • 55
  • `git checkout remotes/repo/branch` makes git checkout look for a pathspec, not a remote repo. – Erin Feb 15 '13 at 19:09
  • 1
    Yes, is it even possible to checkout a branch on the remote repo? Obviously (or maybe it wasn't so obvious), remotes are first fetched so that you have them locally. The git book has a good section on them: http://git-scm.com/book/en/Git-Branching-Remote-Branches – Daniel Lee Feb 16 '13 at 01:15
  • `git checkout -t remote_branch_name` is probably the most simple and laziest way out of all answers. – Anthony Lei Aug 14 '20 at 18:57
50

The title and the question are confused:

  • Git fetch remote branch
  • how can my colleague pull that branch specifically.

If the question is, how can I get a remote branch to work with, or how can I Git checkout a remote branch?, a simpler solution is:

With Git (>= 1.6.6) you are able to use:

git checkout <branch_name>

If local <branch_name> is not found, but there does exist a tracking branch in exactly one remote with a matching name, treat it as equivalent to:

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

See documentation for Git checkout

For your friend:

$ git checkout discover
Branch discover set up to track remote branch discover
Switched to a new branch 'discover'
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Guillaume Vincent
  • 9,586
  • 10
  • 66
  • 88
32

Use:

git checkout -b serverfix origin/serverfix

This is a common enough operation that Git provides the --track shorthand:

git checkout --track origin/serverfix

In fact, this is so common that there’s even a shortcut for that shortcut. If the branch name you’re trying to checkout (a) doesn’t exist and (b) exactly matches a name on only one remote, Git will create a tracking branch for you:

git checkout serverfix

To set up a local branch with a different name than the remote branch, you can easily use the first version with a different local branch name:

git checkout -b sf origin/serverfix

Now, your local branch sf will automatically pull from origin/serverfix.

Source: Pro Git, 2nd Edition, written by Scott Chacon and Ben Straub (cut for readability)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
ManuelSchneid3r
  • 14,156
  • 9
  • 53
  • 94
31

To fetch a branch that exists on remote, the simplest way is:

git fetch origin branchName
git checkout branchName

You can see if it already exists on remote with:

git branch -r

This will fetch the remote branch to your local and will automatically track the remote one.

Harshit Agarwal
  • 1,349
  • 2
  • 12
  • 19
  • 2
    How does this question not have more upvotes? I could be wrong but this sure seemed to do the trick, fetched a branch I didn't have on local from remote... – Nicholas Petersen May 14 '19 at 20:16
29

With this simple command:

git checkout -b 'your_branch' origin/'remote branch'
Javier C.
  • 6,011
  • 4
  • 33
  • 46
Karthik damodara
  • 1,681
  • 15
  • 17
25

[Quick Answer]

There are many alternatives, and my favourites are:

- Alternative 1:

git fetch --all
git checkout YourBranch

Using this alternative using a branch that exist remotely, but not in your local.

- Alternative 2:

git checkout -b 'YourBranch' origin/'YourRemote'

Probably, this is the simplest way.

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

What helped me was

1) To view all available remote branches (e.g. 'remote-branch-name')

git branch -r

2) Create a local branch using remote branch name

git fetch && git checkout 'remote-branch-name'
Arlan T
  • 2,421
  • 1
  • 13
  • 10
  • 1
    What happens when you run the command ```git push``` without any other arguments? Is the local branch named ```remote-branch-name``` automatically associated with (tracking to) the remote branch named ```origin/remote-branch-name```. Or do you need to run ```git push -u origin remote-branch-name``` – PatS Nov 07 '18 at 18:15
  • 1
    this will cause HEAD detached state – Akin Hwan Nov 29 '18 at 17:38
16
git fetch

git branch -r

git checkout <branch_name>
TLama
  • 71,521
  • 15
  • 192
  • 348
John Rodriguez
  • 171
  • 1
  • 3
15

You can fetch and checkout the remote branch in one shot too:

git fetch && git checkout the-branch-name
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Kumar Sambhav
  • 6,675
  • 13
  • 56
  • 82
14

I typed

git checkout <branch_name>

and got

Branch <branch_name> set up to track remote branch <branch_name> from origin.
Switched to a new branch '<branch_name>'
tshepang
  • 10,772
  • 21
  • 84
  • 127
  • from git checkout documentation: If is not found but there does exist a tracking branch in exactly one remote with a matching name, treat as equivalent to: `git checkout -b --track /` – Guillaume Vincent Mar 31 '14 at 10:49
13

At times you are asked not to fiddle with the master branch and work only the remote branch (as I was asked to). So all you need is the remote branch.

So to clone the remote branch alone (without the master), do this

git clone url --branch remote_branch_name

where, remote_branch_name is the name of the remote branch

For example,

git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git --branch v3.15

This will make sure that you clone the remote branch to your local branch with the name of the remote branch.

Now if you commit your code and push, the code will be submitted to that branch alone.

ganezdragon
  • 179
  • 1
  • 9
  • 1
    Related: *[How do I clone a single branch in Git?](https://stackoverflow.com/questions/1778088)* - *"git 1.7.10 (April 2012) actually allows you to clone only one branch:"* – Peter Mortensen Dec 16 '19 at 15:35
10

Let's say that your remote is git@xyz.git and you want its random_branch branch. The process should be as follows:

  1. First check the list of your remotes by

    git remote -v

  2. If you don't have the git@xyz.git remote in the above command's output, you would add it by

    git remote add xyz git@xyz.git

  3. Now you can fetch the contents of that remote by

    git fetch xyz

  4. Now checkout the branch of that remote by

    git checkout -b my_copy_random_branch xyz/random_branch

  5. Check the branch list by

    git branch -a

The local branch my_copy_random_branch would be tracking the random_branch branch of your remote.

zafar142003
  • 1,962
  • 2
  • 16
  • 29
9

The steps are as follows;

  1. git fetch origin or git fetch --all , this will fetch all the remote branches to your local and then this the second option you can proced with.

  2. git checkout --track origin/<The_remote_branch you want to switch over>

Then work on this branch and you can verify whether you are on that branch or not by typing

git branch

It displayes the branch you currently in.

Sam
  • 1,463
  • 1
  • 17
  • 28
9

If you would like to fetch all remote branches, please type just:

git fetch --all
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ishwor Khanal
  • 1,039
  • 14
  • 26
8

git fetch --all & git checkout <branch name>

Jerome Anthony
  • 7,203
  • 2
  • 36
  • 26
7

If you have a repository that was cloned with --depth 1 then many of the commands that were listed will not work. For example, see here

% git clone --depth 1 https://github.com/repo/code
Cloning into 'code'...
cd code
remote: Counting objects: 1778, done.
remote: Compressing objects: 100% (1105/1105), done.
remote: Total 1778 (delta 87), reused 1390 (delta 58), pack-reused 0
Receiving objects: 100% (1778/1778), 5.54 MiB | 4.33 MiB/s, done.
Resolving deltas: 100% (87/87), done.
Checking connectivity... done.
Checking out files: 100% (1215/1215), done.
% cd code
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
% git fetch origin other_branch
remote: Counting objects: 47289, done.
remote: Compressing objects: 100% (15906/15906), done.
remote: Total 47289 (delta 30151), reused 46699 (delta 29570), pack-reused 0
Receiving objects: 100% (47289/47289), 31.03 MiB | 5.70 MiB/s, done.
Resolving deltas: 100% (30151/30151), completed with 362 local objects.
From https://github.com/repo/code
 * branch            other_branch-> FETCH_HEAD
% git checkout other_branch
error: pathspec 'other_branch' did not match any file(s) known to git.
%

In this case I would reclone the repository, but perhaps there are other techniques e.g. git shallow clone (clone --depth) misses remote branches

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Colin D
  • 1,857
  • 1
  • 26
  • 29
7

git fetch && git checkout <your friend's branch name> should do the trick

tambakoo
  • 303
  • 2
  • 12
7

I want to give you one-liner command for fetching all the remote branches to your local and switch to your desired newly created local branch:

git fetch && git checkout discover

After running the above command you will get the below message:

Switched to a new branch 'discover'
Branch discover set up to track remote branch discover from origin.

The first line states that switched to a new branch - why new? It is already there in remote!

But actually you have to create it locally too. The branch is taken from the remote index and created locally for you.

Here discover is a new branch which were created from your repository's remote branch discover.

But the second line gives more information than the first one which tell us that:

Our branch is set up to track remote branch with the same name.

Although git fetch fetches all branches to local. But if you run git branch after it, you will see only master branch in local. Why?

Because for every branch you have in remote you have to create it locally too, for tracking it as git checkout <branchname> as we have done in the above example.

After running git checkout command you can run git branch, and now you can see both the branch:

  1. master and 2. discover in your local listing.
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Haritsinh Gohil
  • 3,299
  • 26
  • 33
6

Simply try:

git pull origin your_branch_name
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rizo
  • 2,391
  • 1
  • 15
  • 18
  • 2
    Rizo, git pull origin branch_name should be the best solution. You are the only person that posted this as a solution and it worked for me. This works because it will update your branch with the master branch. simple and uncomplicated. – Ian Poston Framer Aug 13 '18 at 16:31
  • 3
    the problem with this is that it will try to merge that remote branch with your CURRENT branch, which is not the remote one (since that is new for your local repo) – Bernardo Dal Corno Apr 26 '19 at 09:01
  • This will merge to your current branch. – Eem Jee Jun 13 '19 at 03:48
3

If you already know your remote branch like so...

git remote
=> One
=> Two

and you know the branch name you wish to checkout, for example, br1.2.3.4, then do

git fetch One
=> returns all meta data of remote, that is, the branch name in question.

All that is left is to checkout the branch

git checkout br.1.2.3.4

Then make any new branches off of it.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
SoEzPz
  • 11,914
  • 8
  • 56
  • 58
3

Check your .git/config file, particularly what tracking is present on fetch for that remote.

[remote "randomRemote"]
    url = git@github.com:someUser/someRepo.git
    fetch = +refs/heads/*:refs/remotes/randomRemote/*

If it has heads/* pointing to randomRemote/*, when you run git fetch randomRemote, it will fetch all branches.

Then you can just checkout that branch.

Otherwise,

  1. You need to add remote branches to the tracking using this. Check your .git/config after running this. You will understand.

    git remote set-branches --add randomRemote randomBranch
    
  2. Run git fetch randomRemote. This will fetch the remote branch.

  3. Now you can run git checkout randomBranch.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Vamshi Suram
  • 750
  • 1
  • 6
  • 18
2

git branch <name> --track origin/<name>

Chris F Carroll
  • 7,909
  • 2
  • 42
  • 51
1

You use 'git pull' to keep your branches separate. I will use the actual repository and branch names to help since 'lbranch' and 'rbranch' are tough to decipher.

Let's use:

  • myteam.unfuddle.com = the remote Git server
  • tlc = Unfuddle project account where the repository exists
  • daves_branch = remote branch name

    You, or any colleague, can run this to pull only your branch, no matter how many branches there are:

    git init
    git pull git@myteam.unfuddle.com:myteam/tlc daves_branch:refs/remotes/origin/daves_branch
    
  • Peter Mortensen
    • 28,342
    • 21
    • 95
    • 123
    Andrew
    • 678
    • 4
    • 8
    1

    A simple command, git checkout remote_branch_name will help you to create a local branch that has all the changes in the remote branch.

    Peter Mortensen
    • 28,342
    • 21
    • 95
    • 123
    Sijeesh
    • 127
    • 2
    • 8
    1

    If you download the repository with git clone <repo_url> -b <branch> (only cloning certaing branch), you should modify the <repo_name>/.git/config file. Replace or modify the line that references the fetch target of the [remote "origin"] section to let the command git fetch --all discover all branches:

    [remote "origin"]
            url = <repo_git_url>
            fetch = +refs/heads/master:refs/remotes/origin/master
    

    Be sure to set the fetch parameter point to /heads/master.

    Care with git fetch --all because this will fetch all, so may take a long time.

    Shide
    • 31
    • 4
    -1
    git checkout -b branch_name
    git pull remote_name branch_name
    
    Leon
    • 2,632
    • 20
    • 34
    • 3
      While this snippet might answer the question, it's better to include some explanation about what it does and how it differs from the very large number of answers already here. – DaveyDaveDave Jan 22 '19 at 11:11
    • 2
      I personally do not like this option. Because in case you are creating a new branch from Master and then pulling data from remote branch, that remote branch may or may not be on the same page as the master and might invite some unnecessary code updates – zeetit Jan 31 '19 at 12:29
    • Some explanation would be in order. – Peter Mortensen Dec 16 '19 at 18:26