Search code examples
gitbranchgit-branch

Git clone multiple specific branches


I am trying to clone multiple specific branches in a repository.

I know it is possible to clone one specific branch using --single-branch, but what if I want 2 specific branches and no other branch?

I know I can use git config remote.origin.fetch as shown here to change the regex of the "monitored" branches, but can I do this when I clone?


Solution

  • You cannot do this with git clone directly, but you don't need to.

    Remember that git clone is short for doing git init, git remote add, git fetch, and git checkout (more precisely there are six steps but this is the gist). The git remote add command itself can add multiple single branches, while git clone cannot.

    Hence, the two ways to go are:

    • split out the commands needed so that you can run git remote add yourself
    • or, probably easier, do a single-branch clone, then git remote set-branches origin each of the remaining branches (one per command or all at once).

    If you use the git remote set-branches method, remember to run git fetch again afterward.