30

I have a git repository that has a remote set up ("git fetch" works), but it doesn't have any remote branches: the ".git/refs/remotes" folder doesn't exist, and

git branch -r 

returns nothing (empty list, no branches). Yet the actual remote repository does have a branch. If I re-clone the remote repo, I get another local repo that does have remote branches.

I didn't think this state was possible. My questions:

  1. How on earth did I get into this state?
  2. Is there a command that can be run to put ".git/refs/remotes" back? (short of a fresh clone, which I've done already).

Additional Information

"git config --get remote.origin.fetch" is blank (i.e. the command produces no output)

"git remote show origin" shows

$ git remote show origin
* remote origin
  Fetch URL: <correct remote url here>
  Push  URL: <correct remote url here>
  HEAD branch: master
  Local ref configured for 'git push':
    master pushes to master (up to date)
stochastic
  • 3,155
  • 5
  • 27
  • 42

2 Answers2

50

[Edit, April 2017] If you made your original clone with --single-branch, your remote.origin.fetch setting will be the problem. If you used a --depth limit, that too implies --single-branch. Restore the normal remote.origin.fetch value described below and you will get a fuller clone.


Normally, just running git fetch should restore your remote-tracking branches.

If git fetch is not restoring them, then I wonder: what goes git config --get-all remote.origin.fetch print? (This assumes the remote is named origin; if not, substitute the actual name.) It should be set to +refs/heads/*:refs/remotes/origin/* (again assuming the name origin). Special cases (such as fetching notes) might add more lines.

torek
  • 448,244
  • 59
  • 642
  • 775
  • git fetch does not restore them, unfortunately. "git config --get remote.origin.fetch" produces no output – stochastic Dec 04 '13 at 18:25
  • 2
    Manually putting the value you specified into the fetch variable under the .git/config file for this repo does indeed cause "git fetch" to restore the missing remote branches. It would still be valuable for others to know how this might happen, if anybody knows. – stochastic Dec 04 '13 at 18:38
  • 1
    Odd. `git remote add` sets up remotes with one `fetch` line, either that typical one, or the special version for mirrors (with `--mirror=fetch`). Cloning a remote repo sets up an `origin` remote the same way. What removed it remains a mystery as yet. – torek Dec 04 '13 at 19:32
  • Had this issue too - I think when I cloned this repo I used `--depth=15` perhaps that's related ? – cloakedninjas Aug 06 '15 at 09:25
  • Tripped over this The only clue I can leave is that I was fetch/pushing on awful mobile internet, with --set-upstream as a default push for convenience. It makes no sense to me why that would affect anything. Thanks friends for the fix!! Screenshots at https://twitter.com/tim_abell/status/1573027018589065216 – Tim Abell Sep 22 '22 at 19:28
28

This solved my problem

git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*

When I excute vi .git/config , I loose the fetch line:

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

Ismail RBOUH
  • 10,292
  • 2
  • 24
  • 36
Xinlei Li
  • 281
  • 3
  • 3
  • 2
    Might be worth quoting the value to avoid shell globbing. `git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'` – Tim Abell Sep 22 '22 at 19:31