12

Somehow one of my repositories refuses to fetch new branches:

C:\temp>git fetch --all
Fetching origin

C:\temp>git branch -a
* develop
remotes/origin/develop

C:\temp>git ls-remote --heads origin
d130c97c1ccbe9ab35cd6e268c760781e37e3628 refs/heads/2.1.0.x
...
92685125df152fe053a2818de0c4d2ce8655c6b8 refs/heads/2.2.5.x
1e2eec16c7d63c84d6b53d0a221d22109b4de2a8 refs/heads/develop
f6447d5315fe777803b283bee7dac1dbdba92536 refs/heads/feature/0001089__icons_are_gone_-_move_inclusion_to_RC_files
e2cf0112b7e7fc18eb3467c7c42657208147efb2 refs/heads/feature/0001102__Debug_time_fix_exception_EIdSocketError_10060
6b2c89c6a39b3ce26cf42c5e8e5e0dd12c88abac refs/heads/feature/0001103__Research_cause_of_Internal_error_Stack_not_balanced
...
9f724b76b7c3533996fa03189e18a2f61fa5cf4f refs/heads/master
c233696172eb05522d1bb6705a3ea8cd730a762d refs/heads/origin/master
1db38f4fab2c41ee1931c9c6165aa6087556210a refs/heads/release
c233696172eb05522d1bb6705a3ea8cd730a762d refs/heads/trunk

How can I force git to fetch all these remote branches?

I'm at git version 2.8.2.windows.1.

Thom
  • 14,013
  • 25
  • 105
  • 185
Jeroen Wiert Pluimers
  • 23,965
  • 9
  • 74
  • 154
  • 1
    Just FYI, that this specific case would happen when a git repo is initialized by `git submodule add` as a submodule of another repo (at least for me). In that case, the repo config `remote.origin.fetch` would be set to the default branch but not the wildcard one `+refs/heads/*:refs/remotes/origin/*`. this bothered me a lot... – ttimasdf Oct 26 '20 at 03:38

2 Answers2

29

Check you git config --get remote.origin.fetch refspec.

It would only fetch all branches if the refspec is

+refs/heads/*:refs/remotes/origin/*

If the refspec is:

+refs/heads/develop:refs/remotes/origin/develop

Then a fetch would only bring back the develop branch.
This is typical of a git clone --branch develop --single-branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks a million; on the freshly cloned repository it was `+refs/heads/*:refs/remotes/origin/*` and on the faulty one `+refs/heads/develop:refs/remotes/origin/develop` Any idea why that could be? – Jeroen Wiert Pluimers Oct 10 '16 at 12:06
  • 1
    @JeroenWiertPluimers That happens when you do a `git clone --branch develop --single-branch` – VonC Oct 10 '16 at 12:07
  • 1
    I usually perform a plain `git clone` (this is the first time I learn about `--single-branch`) or clone via SourceTree (for which the current Windows version does a `git -c filter.lfs.smudge= -c filter.lfs.required=false -c diff.mnemonicprefix=false -c core.quotepath=false clone --branch develop --recursive`). Something odd happened and I can't remember why. I'm getting old (: Anyway: thanks again. This solves the problem: `git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*` – Jeroen Wiert Pluimers Oct 10 '16 at 12:29
  • 1
    @JeroenWiertPluimers: just FYI, you don't need to `--add` this particular case, though it should be harmless. (The `*` mapping means "all branches", so `--add` results in "get me the `develop` branch, then get me all branches.") – torek Oct 10 '16 at 17:50
  • 2
    Another case would be that one did a shallow clone, like `git clone --depth 1`, hence only got the the latest commit of the default remote branch. Then this change is necessary to get all other branches. – ryenus May 17 '18 at 16:14
4

you can check your branch cat .git/config in terminal

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
[remote "origin"]
    url = { git remote url }
    fetch = +refs/heads/develop:refs/remotes/origin
[branch "develop"]
    remote = origin
    merge = refs/heads/develop

and git remote update but not update remote branch. you will replace origin fetch

first, check remote fetch git config --get remote.origin.fetch

+refs/heads/develop:refs/remotes/origin/develop

second, unset remote fetch use git config --unset-all remote.origin.fetch and add another git config --add remote.origin.fetch +refs/heads/\*:refs/remotes/origin/feature/\*

then, you can check replace remote git config --get remote.origin.fetch

+refs/heads/*:refs/remotes/origin/*
홍성호
  • 71
  • 4
  • 1
    Note that the original question states that the OS is Windows, not *nix or macOS, so commands like `cat` and applications like "terminal" do not exist. – Dykotomee Aug 28 '20 at 17:43
  • 1
    This is true, but there are at least 3 environments available for Windows that do: `cygwin`, `gitbash`, and the `Windows Subsystem for Linux` available within `PowerShell`. – Phil Ryan Sep 07 '21 at 06:27
  • Thanks! - The `feature` part was confusing. I used only `git config --add remote.origin.fetch +refs/heads/\*:refs/remotes/origin/\*` which worked for me. – Martin Rüegg Apr 15 '23 at 13:00