Why Can't Git Fetch Remote Branches Other Than Master?

Last week I came into a problem with Git, that I can't fetch the remote branch that I just pushed to. It was so weird because I can push it. It never happens before, and it happened when I was in a rush to rebase my code, as someone in my team pushed his code.

After searching, it seemed that the configuration of fetch of that repo was different(this Stack Overflow thread for example) than before, it was specified that only master can be fetched. It worked after I changed it to +refs/heads/*:refs/remotes/origin/*.

I reflected why it happened after work, the only difference I can remember was that I clone that repo with the option --depth <N>, that is, it was a shallow clone to save some disk space.

Today I had some time at hand, and I confirmed that the option is the cause by reading the git-clone(1) manpage, it notes that:

       --depth <depth>
           Create a shallow clone with a history truncated to the specified number of commits. Implies --single-branch unless
           --no-single-branch is given to fetch the histories near the tips of all branches.

Let's demonstrate it by cloning my dotfiles repo with different options, and it shows clearly that Git will only fetch master of the remote repo if only specifying --depth:

$ cd /tmp/
$ git clone --depth 1 https://github.com/whatacold/extensible-dotfiles.git dotfiles-depth1
$ git clone --no-single-branch --depth 1 https://github.com/whatacold/extensible-dotfiles.git dotfiles-no-single-branch
$ git clone https://github.com/whatacold/extensible-dotfiles.git dotfiles-full

$ grep 'remote "origin"' -A 2 dotfiles-depth1/.git/config 
[remote "origin"]
	url = https://github.com/whatacold/extensible-dotfiles.git
	fetch = +refs/heads/master:refs/remotes/origin/master

$ grep 'remote "origin"' -A 2 dotfiles-no-single-branch/.git/config 
[remote "origin"]
	url = https://github.com/whatacold/extensible-dotfiles.git
	fetch = +refs/heads/*:refs/remotes/origin/*

$ grep 'remote "origin"' -A 2 dotfiles-full/.git/config
[remote "origin"]
	url = https://github.com/whatacold/extensible-dotfiles.git
	fetch = +refs/heads/*:refs/remotes/origin/*

I don't know why --single-branch should be implied by default, at least for me, I just want to fetch all the remote branches.


See also

comments powered by Disqus