2

I can list the branches on my remote repository like this

git ls-remote https://blah.visualstudio.com/Blah/_git/Software

which gives me the list of branches available on the remote:

e7dcd105d5a03d0dc176f9e29fae020c0419c4fb HEAD
500fde10ba1498fe5c0118b095fe21521ea4e569 refs/heads/Task/150-Xamarin

(N.B. I've snipped most of them out.)

Following the documentation for git clone I should (I think) be able to clone the remote repository but only taking the tip of one branch. Here are the versions of the command that I have tried

git clone --depth 1 --branch Task/150-Xamarin -- https://blah.visualstudio.com/Blah/_git/Software  
git clone --depth 1 --branch heads/Task/150-Xamarin -- https://blah.visualstudio.com/Blah/_git/Software  
git clone --depth 1 --branch refs/heads/Task/150-Xamarin -- https://blah.visualstudio.com/Blah/_git/Software  

Every one of these return variations of the error

Cloning into 'Software'...
warning: Could not find remote branch Task/150-Xamarin -- to clone.
fatal: Remote branch Task/150-Xamarin -- not found in upstream origin

If I omit the -- I get a different error. For example from

git clone --depth 1 --branch Task/150-Xamarin https://blah.visualstudio.com/Blah/_git/Software

I get the error

You must specify a repository to clone.

I don't understand. The remote branch is listed by git ls-remote why can't I get it in the shallow clone?

dumbledad
  • 16,305
  • 23
  • 120
  • 273
  • 1
    Remove `--`. It's incorrect use. – ElpieKay Feb 05 '17 at 12:18
  • Thanks ElpieKay. You are correct, though that did not fix it for me. It certainly helped me diagnose the true cause of my problem which was that the branch name ended with a non-breaking space character which git read as part of the branch name – dumbledad Feb 06 '17 at 14:57

2 Answers2

2

My original answer worked, but is misleading. ElpieKay's comment on the question led me to dig further. My problem was not the ordering of the arguments to git clone. In fact this command works fine

git clone --depth 1 --branch Task/150-Xamarin https://blah.visualstudio.com/Blah/_git/Software

That looks absolutely identical to the failing command in my question. I only spotted the difference when I looked at them in a binary editor. Here's the fragment "Xamarin https" from the two commands. The one that succeeds is

0x58 0x61 0x6D 0x61 0x72 0x69 0x6E 0x20 0x68 0x74 0x74 0x70 0x73

While the one that fails looks like this

0x58 0x61 0x6D 0x61 0x72 0x69 0x6E 0xC2 0xA0 0x68 0x74 0x74 0x70 0x73

That's the problem. The branch name in the failing commands is not followed by a normal whitespace character (encoded in UTF8 as 0x20) but by a non-breaking space (encoded in UTF8 as 0xC2 0xA0). Git was including the trailing non-breaking space as part of the branch name and thus failing to find it on the remote.

I have no idea how a non-breaking space made it into my git command, but that was my problem.


For completeness, here is my original answer.

The order of the arguments in the documentation for git clone is misleading. This is the command that successfully colnes the tip of just one branch

git clone https://blah.visualstudio.com/Blah/_git/Software --depth 1 --branch Task/150-Xamarin

(via VonC's answer to the question "How to clone a single branch in git?")

Community
  • 1
  • 1
dumbledad
  • 16,305
  • 23
  • 120
  • 273
0

For me the problem was that

git ls-remote --heads origin

returned something like

46d0adaf08ef87d0aa840f9a4338d172bf931752    refs/heads/pilot/buildsplit
349502cbb72664c376b2392bdbe8179725a52012    refs/heads/testing/time
32670bd5f6014542d8f9f3854e5aebff5eb8dddf    refs/heads/testing/health

And if I want to clone e.g. the branch time, I need to discard the refs/heads/ part and use

git clone --single-branch -b testing/time https://git.LINK_TO_REPO.git

Note that simply using commit hash instead of name doesn't work.

Khashayar
  • 357
  • 3
  • 12