Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a full clone the only way to submodule add a branch?

Tags:

git

I want to add a submodule that references a specific (non-master) branch. The following will only grab the master branch due to --depth=1, so the command will inevitably fail;

git submodule add -b myBranch --depth=1 [email protected]:some/large/repo

Because submodule add doesn't support --single-branch, does this mean my only option is to clone the entire repo?

like image 601
Stafford Williams Avatar asked Jun 29 '16 03:06

Stafford Williams


1 Answers

From the documentation of git-clone:

--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.

--[no-]single-branch

Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote’s HEAD points at. When creating a shallow clone with the --depth option, this is the default, unless --no-single-branch is given to fetch the histories near the tips of all branches. Further fetches into the resulting repository will only update the remote-tracking branch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branch clone was made, no remote-tracking branch is created.

Thus, if git submodule add performs the cloning with git clone, then in your use case --single-branch is implied. However, it will work correctly only if git submodule add forwards the -b option to git clone.

A guaranteed way of achieving the desired result (without making any assumptions about inner workings of git submodule add) is to git clone the submodule repository on your own using the options of your choice and then add the existing directory as a submodule:

git clone -b myBranch --single-branch --depth=1 [email protected]:some/large/repo large_repo
git submodule add -b myBranch [email protected]:some/large/repo large_repo

git submodule add options repository [path]

...

path is the relative location for the cloned submodule to exist in the superproject. If path does not exist, then the submodule is created by cloning from the named URL. If path does exist and is already a valid Git repository, then this is added to the changeset without cloning.

like image 127
Leon Avatar answered Oct 23 '22 05:10

Leon