gitgit-fetchshallow-clone

git - fetching from a specific branch although repository wasn't cloned with '--single-branch'


To make a long story short, what I'm looking for is to be able to fetch only a specific branch in a local repository, even though this repository wasn't originally cloned with "--single-branch -branch <branch_name>".

To share the complete story - we would like to use shallow clone for reducing git run time in general and specifically in our custom CI tool. Also I should state that we use the Android's "repo" tool.

As far as I understand, we first need to clone the git repository using --depth=1, which the Android's "repo" tool supports, but that is not quite enough for our CI tool which needs to deepen the fetch for being able to have a common git history when pulling updates from users' repositories. So when we try to deepen the fetch (for simplicity - "git fetch --deepen=1") then git fetches all branches and tags and spends a lot of run-time doing so, although we only need to deepen the fetch for a very specific branch.

This is resolved if the git repository is initially cloned using "--single-branch -branch " (in addition to --depth=1) and then "git fetch --deepen=1" runs ultra fast. But the issue is that it seems that the Android's "repo" tool does not support cloning git repos using "--single-branch -branch ", hence my question at the top. Alternatively, if there is another way of being able to deepen the fetch for a specific branch - I would be glad to know the details.

Thank you!


Solution

  • The git remote command has user-oriented facilities for converting to or from single-branch clone setup. See specifically the set-branches sub-command.

    Besides that, you can always run git fetch directly, passing in both --depth and one or more refspecs. The only thing that single-branch-ness does is automate the refspec.

    Your best bet might still be to modify the repo tool you're using to (at least optionally) use --single-branch in the first place since you say you get significant savings on each fetch with this option. That would give you those same savings on the initial fetch (remember that cloning consists of running git init in a new empty directory, various other Git commands to configure as needed, and then git fetch and git checkout). Note that git remote can convert a single-branch clone to a "two-branch clone" (as it were) using the --add option, or by listing both branches.