227

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? Commented May 17, 2014 at 6:56
  • 1
    Did you perhaps forget the verbosity? Try git branch -avv 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. Commented May 17, 2014 at 18:23
  • 4
    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

4 Answers 4

436

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/*
Sign up to request clarification or add additional context in comments.

15 Comments

You can also use git remote set-branches origin '*' for all branches, replace the * with a branchname for one.
This defeats the purpose of shallow cloning.
@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. :)
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.
For me step 2. was git fetch -v --depth=1 to keep the amount of data I downloaded bearable.
|
125

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 Comments

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.
OMG, the single quote ' is important in git remote set-branches --add origin 'remote_branch_name'
@Weekend I couldn't get this working until I left the single quotes out
@PandaWood You are probably on Windows. The "$" sign in the answer implies Bash (on Unix or Cygwin/MSYS).
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.
74

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 Comments

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: I'm using 1.8.4 - i'll do a little investigation if there was a patch on that issue.
@minghua: i edited to reflect new findings about "shallowed clones".
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.
--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.
|
0

Shallow clones only get a single branch, if you want to get other branches of a shallow clone as well, here how did it:

1 - open .git/config , find the desired remote (usually it's origin ), copy the line containing the fetch clause and repeat the same line for the desired branch:

fetch = +refs/heads/main:refs/remotes/origin/main
fetch = +refs/heads/gtk-2-24:refs/remotes/origin/gtk-2-24
...

(you don't need to add branch section, step 3 does it. more below)

2 - git fetch --depth=1

3 - git switch <branch_name>

step 3 is required because before step 3 (after the fetch), git doesn't consider that to branch to exist locally (git branch wont show the branch). after switching once, git will add the branch locally, and update the config file accordingly.

(difference between this approach and that of other answers, using git remote set-branches is i think it will remove the whatever-default branch that was enabled by default, main for example, unless you issue a wildcard '*' which then fetches all the branches, not just selected ones.)

alternatively to all of it, you can issue a

git clone <repo_url> --depth=1 --no-single-branch

to get the last commits on all branches and tags.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.