6

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 git@host.com:some/large/repo

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

Stafford Williams
  • 9,696
  • 8
  • 50
  • 101

1 Answers1

6

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 git@host.com:some/large/repo large_repo
git submodule add -b myBranch git@host.com: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.

Leon
  • 31,443
  • 4
  • 72
  • 97
  • 2
    The first part of your answer is irrelevant because `git submodule add` does not support `--single-branch` as noted in the question. However, the second part looks legit. – Stafford Williams Jun 29 '16 at 05:41
  • @StaffordWilliams I didn't argue that `git submodule add` supports `--single-branch`. My point was that `git submodule add -b myBranch --depth 1` calls `git clone -b myBranch --depth 1`, and the latter would imply `--single-branch`. – Leon Jun 29 '16 at 05:45
  • I think you're saying you feel like `--single-branch` should exist on `submodule add`, due to the presence of `submodule add --depth`, even though it doesn't? – Stafford Williams Jun 29 '16 at 05:48
  • @StaffordWilliams No, I am saying that `git submodule add` delegates some of its work to `git clone`, so if it passes the `-b` and `--depth` options to `git clone`, then cloning would be performed in `--single-branch` mode implicitly. But, if you have doubts, go with the safer 2nd method (that's why I included it in my answer). – Leon Jun 29 '16 at 05:51
  • So I should be able to use `--single-branch` with `submodule add`? – Stafford Williams Jun 29 '16 at 05:53
  • @StaffordWilliams I don't think so, but you can try. – Leon Jun 29 '16 at 05:55
  • Second part worked (thanks!), but the commands are slightly different. Do you wanna update your commands so I can mark this as the answer? Commands are; `git clone -b myBranch --single-branch --depth=1 git@host.com:some/large/repo large_repo` and `git submodule add -b myBranch git@host.com:some/large/repo large_repo` – Stafford Williams Jun 29 '16 at 06:31
  • @StaffordWilliams Updated – Leon Jun 29 '16 at 09:10
  • Not sure the second way works, at least the way I was expecting. Set things up that way, but `git pull --recurse-submodules` from the top level pulled in all the other branches in the submodule, even though it was explicitly cloned --single-branch --depth 1 and the top level repo. was told which branch to track. – Terry Brown Jul 27 '18 at 15:32