git – 完全克隆是子模块添加分支的唯一方法吗?

我想添加一个引用特定(非主)分支的子模块.由于–depth = 1,以下只会抓取主分支,因此命令将不可避免地失败;

git submodule add -b myBranch --depth=1 git@host.com:some/large/repo

因为submodule add不支持–single-branch,这是否意味着我唯一的选择是克隆整个repo?

最佳答案 从
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.

因此,如果git submodule add使用git clone执行克隆,那么在您的用例中,暗示了单个分支.但是,只有在git子模块添加将-b选项转发给git clone时,它才能正常工作.

实现所需结果的有保证的方法(不对git子模块添加的内部工作做任何假设)是使用您选择的选项自己克隆子模块库,然后将现有目录添加为子模块:

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
.

点赞