-4

Does git clone copy every branch on the remote repository?

Is it possible to request it to clone some branches but not some other branches, just like git fetch does via its refspec argument?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Tim
  • 1
  • 141
  • 372
  • 590
  • https://stackoverflow.com/questions/1911109/how-to-clone-a-specific-git-branch – Alexan Jan 14 '19 at 16:33
  • 1
    Possible duplicate of [How do I clone a single branch in Git?](https://stackoverflow.com/questions/1778088/how-do-i-clone-a-single-branch-in-git) – LightBender Jan 14 '19 at 17:29

1 Answers1

0

Remember that git clone, like all the rest of Git, doesn't really concern itself all that much with branch names, but rather with commits. It's just that Git (and git clone) needs names in order to find commits.

The git clone command itself essentially consists of:

  • making a new directory (or using an existing but empty directory)
  • running git init in that directory
  • configuring items here as necessary and appropriate, including doing a git remote add to save the URL
  • running git fetch using the remote just added
  • running git checkout

Hence, the question really boils down to: Can you control the fetch = setting for the remote that git clone adds?

The answer is Yes, but only to a limited extent. The limits are:

  • --mirror sets the refspec to +refs/*:refs/*.
  • --bare sets the refspec to +refs/heads/*:refs/heads/* and +refs/tags/*:refs/tags/*.
  • --single-branch or any of the commands that imply it set the refspec as appropriate for that operation.

In the absence of either of these, you get a standard refspec. Hence, --single-branch lets you pick one branch to clone (by changing to a single-branch refspec), and --mirror lets you go outside the refs/heads/ namespace, but there is, at least as of today, no argument(s) that will do some intermediate subset. You can, instead, do your own mkdir / git init / git remote add / git config / git fetch / git checkout sequence of commands, if you wish to achieve some special result.

torek
  • 448,244
  • 59
  • 642
  • 775