fetch in git doesn't get all branches

201,978

Solution 1

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.

Solution 2

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>

Solution 3

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

Solution 4

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 [email protected]:web3coach/the-blockchain-bar-newsletter-edition.git

git fetch --all
// Ta daaa all branches fetched

Solution 5

write it from the terminal

git fetch --prune.

it works fine.

Share:
201,978

Related videos on Youtube

Edward Newell
Author by

Edward Newell

Updated on July 13, 2022

Comments

  • Edward Newell
    Edward Newell almost 2 years

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

    • torek
      torek almost 12 years
      What does git config --get remote.origin.fetch produce? If it's not +refs/heads/*:refs/remotes/origin/*, it probably should be.
    • Edward Newell
      Edward Newell almost 12 years
      yup that's exactly what it produces
    • Mirko
      Mirko over 11 years
      Exactly the same problem, but the comment above solved it! I had +refs/heads/master:refs/remotes/origin/master with master instead of *
    • Magnus
      Magnus about 11 years
      Same problem for me, but none of the suggestions on this page solves it. Weird.
    • thoni56
      thoni56 about 10 years
      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?
    • Trần Việt Hoàng
      Trần Việt Hoàng over 6 years
      @thoni56: Yes, this is likely due to a shallow clone.
    • Mohsen
      Mohsen almost 3 years
      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.
    • Addison Klinke
      Addison Klinke over 2 years
      Could nested submodules result in similar behavior to the shallow clone that's been mentioned?
    • spacebread
      spacebread about 2 years
      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
  • Edward Newell
    Edward Newell almost 12 years
    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!
  • Kjellski
    Kjellski over 10 years
    Hey @EdwardNewell, thnks for the answer, just to let you know, your link cheat.errtheblog.com/s/git is dead for me...
  • Edward Newell
    Edward Newell over 9 years
    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
  • LocalPCGuy
    LocalPCGuy almost 9 years
    This should probably be the accepted answer, as it actually resolved the issue in the original post.
  • Garis M Suero
    Garis M Suero almost 9 years
    just a side note, I had to add the --replace-all parameter to replace all values on the configuration for my remote.origin.fetch
  • Narretz
    Narretz almost 8 years
    Note that this can happen if you have cloned your repository with a single branch only, e.g. git clone <url> --branch <branch> --single-branch [<folder>]
  • Newbee
    Newbee about 6 years
    I had correct git config for remote.origin.fetch i.e. +refs/heads/*:refs/remotes/origin/*. The above solution helped me.
  • Newbee
    Newbee about 6 years
    Check stux's answer
  • Robert Oschler
    Robert Oschler almost 6 years
    This solution was the correct one for me also. This is unfortunate since it indicates there's potentially a bug in Git.
  • MadTurki
    MadTurki over 5 years
    Thank you! I tried many things and thought I'd just give this a shot... Now to look up what I actually did...
  • Rastikan
    Rastikan over 5 years
    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)
  • Adam Orłowski
    Adam Orłowski over 5 years
    What does it do?
  • Samet ÖZTOPRAK
    Samet ÖZTOPRAK over 5 years
    It takes all available branches. Look at the head.
  • Anatol Bivol
    Anatol Bivol about 5 years
    This could happen when you clone with git clone ... --depth 1
  • jerpint
    jerpint about 5 years
    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
  • Felipe Gerard
    Felipe Gerard about 5 years
    git remote update origin worked for me. I guess something needed refreshing?
  • Vlad Dekhanov
    Vlad Dekhanov almost 5 years
    Thank you a lot, That's really helpful
  • Robert Dodier
    Robert Dodier over 4 years
    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.)
  • whatacold
    whatacold over 4 years
    A shallow clone with git clone --depth <depth> <repo> implies --single-branch, as noted in the man page git-clone(1), so we'd better do it with git clone --depth <depth> --no-single-branch <repo>.
  • Anatoliy Kmetyuk
    Anatoliy Kmetyuk over 4 years
    git remote update origin didn't work for me but removing and adding the remote did.
  • wesley franks
    wesley franks over 4 years
    this is how my config looks, but when i do git fetch all it still doesn't pull the remote branch
  • Gabriel Glenn
    Gabriel Glenn about 4 years
    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.
  • RubberDuck
    RubberDuck about 4 years
    git clone --depth <depth> --no-single-branch <repo> 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.
  • linhares
    linhares about 4 years
    This solved my case too; I just had this issue with git 2.17.1.
  • Jesse Lawson
    Jesse Lawson almost 4 years
    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
    Jesse Lawson almost 4 years
    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.
  • sachsom
    sachsom almost 4 years
    this solved the issue for can anyone tell what the underlying cause for this?
  • AndASM
    AndASM over 3 years
    @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.
  • Aleksandr
    Aleksandr over 3 years
    You are my superstar! @AndASM
  • murison
    murison about 3 years
    i also had this problem after a shallow clone - as doing git fetch --unshallow yes fetches the history, but doestn fetch all the branches :(
  • Juha Tuomala
    Juha Tuomala almost 3 years
    This solution helped here too instantly, should be selected solution. Thanks.
  • Will
    Will almost 3 years
    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.
  • procra
    procra over 2 years
    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
    procra over 2 years
    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
  • Britney Smith
    Britney Smith over 2 years
    This answer also worked for me after I saw my git config for remote.origin.fetch was correct.
  • shri_wahal
    shri_wahal about 2 years
    This is the actual solution to the problem : check your remote config and don't shallow clone
  • Weltgeist
    Weltgeist about 2 years
    I confirm that this is a fix after you do >git clone --depth 1 >git fetch --unshallow Something I did to fix this issue: stackoverflow.com/questions/38618885/…
  • jtlz2
    jtlz2 almost 2 years
    Didn't work for me!