373

I have cloned a repository, after which somebody else has created a new branch, which I'd like to start working on. I read the manual, and it seems dead straight easy. Strangely it's not working, and all the posts I've found suggest I'm doing the right thing. So I'll subject myself to the lambasting, because there must be something obviously wrong with this:

The correct action seems to be

git fetch
git branch -a
* master
  remotes/origin/HEAD --> origin/master
  remotes/origin/master
git checkout -b dev-gml origin/dev-gml

At this point there is a problem, for some reason after git fetch I can't see the dev-gml remote branch. Why not? If I clone the repository freshly, it's there, so certainly the remote branch exists:

$ mkdir ../gitest
$ cd ../gitest
$ git clone https://github.com/example/proj.git
Cloning into proj...
remote: Counting objects: 1155, done.
remote: Compressing objects: 100% (383/383), done.
remote: Total 1155 (delta 741), reused 1155 (delta 741)
Receiving objects: 100% (1155/1155), 477.22 KiB | 877 KiB/s, done.
Resolving deltas: 100% (741/741), done.
$ cd projdir
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev-gml
  remotes/origin/master

I've tried git update, git pull, git fetch --all, git pretty-please in all possible permutations...

Jonas
  • 121,568
  • 97
  • 310
  • 388
Edward Newell
  • 17,203
  • 7
  • 34
  • 36
  • 83
    What does `git config --get remote.origin.fetch` produce? If it's not `+refs/heads/*:refs/remotes/origin/*`, it probably should be. – torek Jul 24 '12 at 05:49
  • 1
    yup that's exactly what it produces – Edward Newell Jul 25 '12 at 05:21
  • 4
    Exactly the same problem, but the comment above solved it! I had `+refs/heads/master:refs/remotes/origin/master` with `master` instead of `*` – Mirko Oct 06 '12 at 08:43
  • 2
    Same problem for me, but none of the suggestions on this page solves it. Weird. – Magnus Jan 28 '13 at 22:31
  • I also faced the same problem and seeing Mirkos comment I modified the .git/config section for [remote "origin"], which fixed the problem. Could this have been caused by a shallow clone? – thoni56 Mar 07 '14 at 17:12
  • 2
    @thoni56: Yes, this is likely due to a shallow clone. – Trần Việt Hoàng Aug 29 '17 at 15:14
  • I had the same problem and I could not see my colleagues's new created branch when I ran the command `git fetch`; then I decided to just run the command `git checkout my-colleague-new-created-branch` to see if it is really exists or not; then I successfully switched to the branch. In fact, is was there, but it was not shown by `git branch`. – Mohsen Jun 15 '21 at 08:24
  • Could nested submodules result in similar behavior to the shallow clone that's been mentioned? – Addison Klinke Sep 15 '21 at 15:17
  • I want to add I ran into this issue when I accidently pointed at my fork instead of the actual repo when I created a branch in GitHub – spacebread Apr 04 '22 at 19:17

12 Answers12

695

The problem can be seen when checking the remote.origin.fetch setting
(The lines starting with $ are bash prompts with the commands I typed. The other lines are the resulting output)

$ git config --get remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master

As you can see, in my case, the remote was set to fetch the master branch specifically and only. I fixed it as per below, including the second command to check the results.

$ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

The wildcard * of course means everything under that path.

Unfortunately I saw this comment after I had already dug through and found the answer by trial and error.

Community
  • 1
  • 1
AndASM
  • 9,458
  • 1
  • 21
  • 33
  • 4
    This should probably be the accepted answer, as it actually resolved the issue in the original post. – LocalPCGuy May 26 '15 at 17:50
  • 1
    just a side note, I had to add the `--replace-all` parameter to replace all values on the configuration for my `remote.origin.fetch` – Garis M Suero Jun 02 '15 at 20:52
  • 5
    Note that this can happen if you have cloned your repository with a single branch only, e.g. `git clone --branch --single-branch []` – Narretz May 27 '16 at 10:34
  • 3
    Check stux's answer – Newbee Apr 11 '18 at 10:17
  • This fixed my issue. I have a quick question in case someone would know: Was this (having a master only setting) something from an old version of Git or could that be something that someone setup there deliberately to prevent fetching side-branches? (this master-only fetch setting is on an old machine using an old version of Git) – Rastikan Oct 26 '18 at 20:40
  • 42
    This could happen when you clone with `git clone ... --depth 1` – Anatol Bivol Mar 12 '19 at 10:26
  • 8
    A shallow clone with `git clone --depth ` implies `--single-branch`, as noted in the man page [git-clone(1)](https://git-scm.com/docs/git-clone#Documentation/git-clone.txt---depthltdepthgt), so we'd better do it with `git clone --depth --no-single-branch `. – whatacold Nov 30 '19 at 15:12
  • this is how my config looks, but when i do git fetch all it still doesn't pull the remote branch – wesley franks Dec 20 '19 at 19:23
  • 1
    `git clone --depth --no-single-branch ` worked way better for me than fetching all. I was purposely not fetching the entire history and this solution makes it so you'll get the entire history on the next fetch. – RubberDuck Mar 25 '20 at 12:18
  • @RubberDuck I'm pretty sure --no-single-branch didn't exist when this was answered in 2014.. Anyway, losing all the history and creating an incomplete clone would not have solved my issue back then. It really depends what you're doing. – AndASM Oct 22 '20 at 08:50
  • i also had this problem after a shallow clone - as doing `git fetch --unshallow` yes fetches the history, but doestn fetch all the branches :( – murison Feb 15 '21 at 16:22
  • This is the actual solution to the problem : check your remote config and don't shallow clone – shri_wahal Feb 03 '22 at 08:24
  • I confirm that this is a fix after you do >git clone --depth 1 >git fetch --unshallow Something I did to fix this issue: https://stackoverflow.com/questions/38618885/error-rpc-failed-curl-transfer-closed-with-outstanding-read-data-remaining – Weltgeist Apr 20 '22 at 23:23
  • Note: Most hosting providers give you the GUI and from that GUI they mostly fetch the master branch so sometimes in that case we need to update remote origin. – Vivek Pawar Sep 06 '22 at 04:47
  • Happened for me because of `--depth 1` took a while to understand why remote branches are not shown. – Afshar Mohebi Jul 20 '23 at 10:38
235

I had this issue today on a repo.

It wasn't the +refs/heads/*:refs/remotes/origin/* issue as per top solution.

Symptom was simply that git fetch origin or git fetch just didn't appear to do anything, although there were remote branches to fetch.

After trying lots of things, I removed the origin remote, and recreated it. That seems to have fixed it. Don't know why.

remove with: git remote rm origin

and recreate with: git remote add origin <git uri>

stux
  • 2,526
  • 1
  • 13
  • 6
  • 30
    I had correct git config for `remote.origin.fetch` i.e. `+refs/heads/*:refs/remotes/origin/*`. The above solution helped me. – Newbee Apr 11 '18 at 10:17
  • 18
    This solution was the correct one for me also. This is unfortunate since it indicates there's potentially a bug in Git. – Robert Oschler Jun 19 '18 at 00:56
  • 3
    This also solved my issue. I also appear to have this issue on a machine with git version 2.19.1v but didnt experience it on another machine with git version 2.17.1 – jerpint Mar 12 '19 at 21:23
  • 9
    `git remote update origin` worked for me. I guess something needed refreshing? – Felipe Gerard Mar 26 '19 at 21:15
  • 7
    `git remote update origin` didn't work for me but removing and adding the remote did. – Anatoliy Kmetyuk Dec 09 '19 at 13:25
  • I had the same issue. I think it happens when I `git checkout -b myremotebranch` before I `git fetch` the remote branch. It create a new local branch and even after `git branch -d myremotebranch` I cannot fetch it. – Gabriel Glenn Mar 13 '20 at 09:07
  • This solved my case too; I just had this issue with git 2.17.1. – linhares Mar 27 '20 at 00:43
  • 1
    Happened with me (Apple Git-122) git version 2.21.0. Only thing that worked was to remove and then re-add the origin with `git remote add origin`. – Jesse Lawson May 23 '20 at 02:27
  • 1
    Weird--so it happened again, but as it turns out, though `git branch` doesn't list all the fetched branches, I can still check them out. Seems like some kind of bug. – Jesse Lawson May 23 '20 at 02:34
  • 2
    This happened to me today with multiple repos, on version 2.25.1. Noting that we'd recently changed a number of our repos to use 'main' as the primary branch and then deleted 'master'. On most machines we had no problem with get fetch --all but on my (up-to-date) WSL ubunto instance, consistent with multiple repos. – Will Jun 11 '21 at 16:20
  • 1
    Same issue happened to me on `git version 2.32.1 (Apple Git-133)` with an ssh remote. Removing and adding `origin` helped. – Kalyan Jul 27 '22 at 19:27
  • 1
    This appeared to work for me (`git fetch` finally printed all the [new branch]es), but then `git branch` still showed that none of those branches were local. I also had to do `git branch FETCH_HEAD` to get `` to appear in `git branch` list locally, and be accessible locally. – A__ Feb 09 '23 at 17:06
  • “Try turning it off and on again” – Guildenstern May 11 '23 at 22:42
  • 1
    The solution by @A__ resolved the final issue for me and now I can see the branch locally – Daniel Flippance Jun 08 '23 at 20:02
111

Remote update

You need to run

git remote update

or

git remote update <remote> 

Then you can run git branch -r to list the remote branches.

Checkout a new branch

To track a (new) remote branch as a local branch:

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

or (sometimes it doesn't work without the extra remotes/):

git checkout -b <local branch> remotes/<remote>/<remote branch>

Helpful git cheatsheets

philipvr
  • 5,738
  • 4
  • 32
  • 44
  • 10
    But my problem is that I can't checkout an *existing* remote branch, because my git client doesn't think it exists. See my question. Note that when I run `git fetch` followed by `git branch -a` it does not show all the branches. I had to delete my working directory and re-clone to see the branch `dev-gml` that a collaborator made. It worked this time, but we will be branching often! – Edward Newell Jul 25 '12 at 05:28
  • Hey @EdwardNewell, thnks for the answer, just to let you know, your link http://cheat.errtheblog.com/s/git/ is dead for me... – Kjellski Aug 22 '13 at 11:51
  • It's been a long time since I first asked this question, and I just got pinged because someone posted afresh. I'm accepting this answer, even though originally nothing actually worked for me. The reason I have finally marked this correct is because I suspect that what (s)he wrote beside `Edit:` very well might have worked. It is what I would try if I was still facing the problem. HTH – Edward Newell Sep 20 '14 at 21:33
  • 2
    For the record, the bit that helped me here is `git remote update origin`. That made the missing branch visible via `git branch -l -r`. (I did look at `git config --get remote.origin.fetch` and the output was `+refs/heads/*:refs/remotes/origin/*` as expected.) – Robert Dodier Oct 02 '19 at 19:50
  • 1
    This answer also worked for me after I saw my git config for `remote.origin.fetch` was correct. – Britney Smith Jan 12 '22 at 14:27
  • This worked for me when `get fetch` did nothing .. then I realized I was using an ancient version of git (1.8.x.x), so I'm going to assume the functionality of `fetch` and `remote update` has evolved some in more recent versions. Before this, I had to blow away the repo and re-clone it every time. – yano Jun 28 '22 at 19:06
27

Had the same problem today setting up my repo from scratch. I tried everything, nothing worked except removing the origin and re-adding it back again.

git remote rm origin
git remote add origin git@github.com:web3coach/the-blockchain-bar-newsletter-edition.git

git fetch --all
// Ta daaa all branches fetched
Lukas Lukac
  • 7,766
  • 10
  • 65
  • 75
10

write it from the terminal

git fetch --prune.

it works fine.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
6

I had a similar problem, however in my case I could pull/push to the remote branch but git status didn't show the local branch state w.r.t the remote ones.

Also, in my case git config --get remote.origin.fetch didn't return anything

The problem is that there was a typo in the .git/config file in the fetch line of the respective remote block. Probably something I added by mistake previously (sometimes I directly look at this file, or even edit it)

So, check if your remote entry in the .git/config file is correct, e.g.:

[remote "origin"]
    url = https://[server]/[user or organization]/[repo].git
    fetch = +refs/heads/*:refs/remotes/origin/*
Juh_
  • 14,628
  • 8
  • 59
  • 92
  • I had quite the same case today and think I found a clue. I cloned my repo, as it contains a ´master´ and a ´devel´ branch. When I run `git config --get remote.origin.fetch` it returns `+refs/heads/master:refs/remotes/origin/master`. But at the same time if i look into the config file it shows **two** refspecs `fetch = +refs/heads/devel:refs/remotes/origin/devel` **as well as** `fetch = +refs/heads/master:refs/remotes/origin/master`. It seems `git config --get remote.origin.fetch` **only returns the last entry**. So, if on doubt look at the file and add the refspec manually. – procra Aug 09 '21 at 13:20
  • At least that is what i get in my enviroment. I'm tied on a windows system, so i use the powershell with the posh-git module installed – procra Aug 09 '21 at 13:24
5

git checkout --track origin/formats seemed to do the trick:

% git branch      ### show local branches
* main

% git branch - a  ### show local and remote branches
* main
  remotes/origin/HEAD -> origin/main
  remote/origin/formats
  remote/origin/main

% git checkout --track origin/formats
Switched to a new branch 'formats'
Branch 'formats' set up to track remote branch 'formats' from 'origin'

% git branch
* formats
  main

The following should do the same but with different local branch name:

git checkout -b my-formats origin/formats 

A new syntax git switch is available in git c2.23

git switch -c <branch> --track <remote>/<branch>
Craig Hicks
  • 2,199
  • 20
  • 35
3

To make it more specific Create a tracking branch, which means you are now tracking a remote branch.

git branch --track branch remote-branch
git branch --track exp remotes/origin/experimental

After which you can

git branch   # to see the remote tracking branch "exp" created .

Then to work on that branch do

git checkout branchname
git checkout exp

After you have made changes to the branch. You can git fetch and git merge with your remote tracking branch to merge your changes and push to the remote branch as below.

git fetch origin
git merge origin/experimental  
git push origin/experimental

Hope it helps and gives you an idea, how this works.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Swapna
  • 401
  • 2
  • 6
3

I had cloned the repo with --depth 1, so these answers weren't working. What did work for me was

git fetch origin BRANCHNAME:BRANCHNAME

It succesfully created a local branch with the same name.

ninpnin
  • 155
  • 4
1

This could be due to a face palm moment: if you switch between several clones it is easy to find yourself in the wrong source tree trying to pull a non-existent branch. It is easier when the clones have similar names, or the repos are distinct clones for the same project from each of multiple contributors. A new git clone would obviously seem to solve that "problem" when the real problem is losing focus or working context or both.

jerseyboy
  • 1,298
  • 13
  • 13
0

I had to go into my GitExtensions Remote Repositories as nothing here seemed to be working. There I saw that 2 branches had no remote repository configured. after adjusting it looks as followsenter image description here

Notice branch noExternal3 still shows as not having a remote repository. Not sure what combo of bash commands would have found or adjusted that.

Maslow
  • 18,464
  • 20
  • 106
  • 193
-1

All you need to do is, apply the following 2 commands:

git fetch --all

And once you see the branch (which was not visible before e.g. osc_at_works), select that and checkout as below:

git checkout origin/team/Enterprise/osc_at_works
Deb
  • 587
  • 4
  • 12