208

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.

4
  • 3
    master is your local branch. remotes/origin/master is the corresponding remote branch. What exactly is the question?
    – michas
    Commented May 17, 2014 at 6:56
  • 1
    Did you perhaps forget the verbosity? Try git branch -avv
    – jthill
    Commented May 17, 2014 at 7: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
    Commented May 17, 2014 at 18:23
  • 3
    Thanks for introducing git clone --depth=1 --no-single-branch, this is what I need in most cases. Commented Aug 10, 2020 at 9:18

3 Answers 3

404

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

  1. Run (thanks @jthill) doc about set-branches:

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

  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 following line from:

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

to (replace master with *):

fetch = +refs/heads/*:refs/remotes/origin/*
15
  • 71
    You can also use git remote set-branches origin '*' for all branches, replace the * with a branchname for one.
    – jthill
    Commented Dec 10, 2014 at 8:27
  • 10
    This defeats the purpose of shallow cloning. Commented Sep 26, 2017 at 1:12
  • 5
    @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
    Commented Sep 27, 2017 at 7:55
  • 4
    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. Commented Oct 22, 2020 at 5:02
  • 9
    For me step 2. was git fetch -v --depth=1 to keep the amount of data I downloaded bearable.
    – usr1234567
    Commented Feb 2, 2022 at 18:44
119

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.

5
  • 29
    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
    Commented Feb 7, 2017 at 16:55
  • 5
    OMG, the single quote ' is important in git remote set-branches --add origin 'remote_branch_name'
    – Weekend
    Commented Sep 14, 2018 at 9:16
  • @Weekend I couldn't get this working until I left the single quotes out
    – PandaWood
    Commented Jun 3, 2019 at 11:08
  • @PandaWood You are probably on Windows. The "$" sign in the answer implies Bash (on Unix or Cygwin/MSYS).
    – Yongwei Wu
    Commented Jul 10, 2020 at 3:07
  • I don't see anything about the single quotes being necessary in the docs and it seems to work fine without one on macOS at least.
    – Nickolay
    Commented Aug 20, 2020 at 0:51
71

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.

9
  • 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
    Commented May 17, 2014 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. Commented May 17, 2014 at 19:24
  • 1
    @minghua: i edited to reflect new findings about "shallowed clones". Commented May 18, 2014 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
    Commented May 19, 2014 at 18:18
  • 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. Commented Jan 29, 2015 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.