115

After cloning a remote repository it does not show any remote branch by -a option. What could be the problem? How to debug it? In this snippet two of the remote branches are not shown:

$ git clone --depth 1 git://git.savannah.gnu.org/pythonwebkit.git
$ cd pythonwebkit
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git --version
git version 1.8.3.1

Tried the same command on another machine, it works well:

$ git clone --depth 1 git://git.savannah.gnu.org/pythonwebkit.git
Receiving objects: 100% (186886/186886), 818.91 MiB | 3.44 MiB/s, done.
$ cd pythonwebkit/
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/debian
  remotes/origin/master
  remotes/origin/python_codegen
$ git --version
git version 1.7.1

Tried also cloning another repo, it works well. Though I can try it on this machine again, but it would be better to know what's wrong.

Any suggestions or hints will be more than welcome.

Edit: Answer summary: Since git version 1.8.3.2 the "--depth" and "--no-single-branch" need to be used together to get the same behavior as before. This is deemed a bug fix.

minghua
  • 4,544
  • 4
  • 31
  • 63
  • 3
    `master` is your local branch. `remotes/origin/master` is the corresponding remote branch. What exactly is the question? – michas May 17 '14 at 06:56
  • 1
    Did you perhaps forget the verbosity? Try `git branch -avv` – jthill May 17 '14 at 07:10
  • To michas etc: we usually do not refer master as a branch, sorry for the confusion. added "two remote branches are not shown". To jthill: thanks for reminding, you are correct. – minghua May 17 '14 at 18:23
  • 1
    Thanks for introducing `git clone --depth=1 --no-single-branch`, this is what I need in most cases. – Alireza Mohamadi Aug 10 '20 at 09:18

3 Answers3

251

After doing a shallow clone, to be able to checkout other branches from remote,

  1. Run (thanks @jthill):

    git remote set-branches origin '*'
    
  2. After that, do a git fetch -v

  3. Finally git checkout the-branch-i-ve-been-looking-for


Step 1 can also be done manually by editing .git/config.

For instance, change the folloing line from:

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

to (replace master with *):

fetch = +refs/heads/*:refs/remotes/origin/*
MiniGod
  • 3,363
  • 1
  • 23
  • 26
marlo
  • 4,525
  • 3
  • 21
  • 29
  • 61
    You can also use `git remote set-branches origin '*'` for all branches, replace the `*` with a branchname for one. – jthill Dec 10 '14 at 08:27
  • Thank you! This saved my day. – Steven Xu Mar 27 '15 at 08:05
  • What does `-vvv` mean in`git fetch -vvv`? I coundn't find any information about it in git-fetch doc – guo Jul 06 '17 at 07:10
  • @guo it is for the `verbosity` or `debug` logging level of `git`. It is not from `fetch` method. – marlo Jul 10 '17 at 06:26
  • This defeats the purpose of shallow cloning. – kawing-chiu Sep 26 '17 at 01:12
  • 1
    @kawing-chiu this is useful if you have so many branches and the size is to big for the 'internet connection' before and now could afford to get all those branches. :) – marlo Sep 27 '17 at 07:55
  • It worked fine for me, this should be the accepted answer! – Guster May 07 '18 at 06:14
  • `git remote set-branches origin '*'` has a minor issue, `'*'` single quotes in the command surrounding asterisk causes an issue. The command changes the git config file to `fetch = +refs/heads/'*':refs/remotes/origin/'*'`, which causes an issue and does not fetch remaining branches in repository. – Kruti Parekh Oct 22 '20 at 05:02
74

From reading the responses and the comment from @jthill, the thing that worked best for me was to use the set-branches option on the git remote command:

$ git clone --depth 1 https://github.com/dogescript/dogescript.git
$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

This changes the list of branches tracked by the named remote so that we can fetch and checkout just the required branch.

dumbledad
  • 12,928
  • 20
  • 97
  • 226
alejandrodnm
  • 4,232
  • 2
  • 21
  • 25
  • 20
    It might be better to use `git remote set-branches --add origin 'remote_branch_name'` so that the new branch is in addition to existing ones, rather than replacing them in the remote's list of branches (or branch patterns) to fetch in the .git/config file. – dumbledad Feb 07 '17 at 16:55
  • 2
    OMG, the single quote `'` is important in `git remote set-branches --add origin 'remote_branch_name'` – Weekend Sep 14 '18 at 09:16
  • @Weekend I couldn't get this working until I left the single quotes out – PandaWood Jun 03 '19 at 11:08
  • @PandaWood You are probably on Windows. The "$" sign in the answer implies Bash (on Unix or Cygwin/MSYS). – Yongwei Wu Jul 10 '20 at 03:07
  • I don't see anything about the single quotes being necessary in [the docs](https://git-scm.com/docs/git-remote) and it seems to work fine without one on macOS at least. – Nickolay Aug 20 '20 at 00:51
64

The behavior is correct, after the last revision the master-branch is (since this is the primary remote's HEAD) the only remote-branch in the repository:

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/master

The full clone offers new (all) branches:

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/debian
          remotes/origin/master
          remotes/origin/python_codegen

Shallow clones

Due to the shallow-description in the technical documentation, a "git-clone --depth 20 repo [...] result[s in] commit chains with a length of at most 20." A shallow clone therefore should contain the requested depth of commits, from the tip of a branch.

As - in addition - the documentation of git clone for the --single-branch-option describes:

"Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote's HEAD points at. When creating a shallow clone with the --depth option, this is the default, unless --no-single-branch is given to fetch the histories near the tips of all branches."

Therefore a shallow clone (with the depth-option) only fetches only one single branch (at your requested depth).


Unfortunately both options (--depth and --single-branch) have been faulty in the past and the use of shallow clones implicits unresolved problems (as you can read in the link I posted above), which is caused by the given history-rewrite. This leads in overall to somewhat complicated behavior in special cases.

Florian Neumann
  • 4,439
  • 1
  • 36
  • 45
  • 1
    florianb: what is your git version? thanks for trying it out. I did the --depth 1 on 1.7.1 just now it shows all the remote branches. updated the question with this. +1 for verifying the problem. – minghua May 17 '14 at 19:12
  • 1
    @minghua: I'm using 1.8.4 - i'll do a little investigation if there was a patch on that issue. – Florian Neumann May 17 '14 at 19:24
  • I'm confused. Is it a bug or not? My 1.8.3.1 behave the same as your 1.8.4, but different from 1.7. Which one is correct behavior? The release note means 1.8.3.1 should be different from 1.8.3.2 and later versions... IMHO shallow history should not cut current branches. Since I can check out shallow and still see all remote branches with 1.7.1, they are still there. The branches can be seen on the git web interface too. I don't think 1.8.4 is right... or I'm missing something? – minghua May 18 '14 at 06:51
  • 1
    @minghua: i edited to reflect new findings about "shallowed clones". – Florian Neumann May 18 '14 at 12:50
  • 1
    It's almost perfect except only one thing: what does it mean by saying "the repo-owner decided to cut the other branches off"? I think those branches are still there. – minghua May 19 '14 at 18:18
  • @minghua - you're absolutely right, i modified the answer. – Florian Neumann May 19 '14 at 18:57
  • 2
    --no-single-branch also clones all tags. We can avoid that by creating a new repo, using the same config to fetch all remotes, i.e. `fetch = +refs/heads/*:refs/remotes/origin/*`, and running `git fetch --depth 1` (without `--tags`). We can also add specific tags to be fetched, using config like `fetch = +refs/tags/v2.0.0:refs/tags/v2.0.0`. – Sam Watkins Jan 29 '15 at 13:32
  • I'm not happy with this answer as it doesn't touch upon the OPs implicit question: how to get all of the branches while not fetching the whole repo. – user239558 Dec 14 '17 at 23:32
  • @user239558 - i am sorry, but i couldn't read any implicit question about fetching. I hope the OP flagged this answer as answer because it answered her question. – Florian Neumann Dec 15 '17 at 07:49