2376

我正在尝试将子模块放入回购中。问题是当我克隆父仓库时,子模块文件夹完全是空的。

有什么办法可以git clone parent_repo将数据实际放入子模块文件夹中?

例如, http : //github.com/cwolves/sequelize/tree/master/lib/nodejs-mysql-native指向一个外部 git 子模块,但是当我签出sequelize项目时,该文件夹是空的。

4

18 回答 18

3385

对于 Git 2.13 及更高版本,--recurse-submodules可以使用以下命令代替--recursive

git clone --recurse-submodules -j8 git://github.com/foo/bar.git
cd bar

编者注:-j8是一种可选的性能优化,在 2.8 版本中可用,一次最多可并行获取 8 个子模块 - 请参阅man git-clone.

使用 Git 的 1.9 版直到 2.12 版(-j标志仅在 2.8+ 版中可用):

git clone --recursive -j8 git://github.com/foo/bar.git
cd bar

使用 Git 1.6.5 及更高版本,您可以使用:

git clone --recursive git://github.com/foo/bar.git
cd bar

对于已克隆的存储库或较旧的 Git 版本,请使用:

git clone git://github.com/foo/bar.git
cd bar
git submodule update --init --recursive
于 2010-12-14T10:43:12.717 回答
583

在填充子模块之前,您必须做两件事:

git submodule init 
git submodule update
于 2010-09-26T07:19:04.227 回答
313

Git 2.23(2019 年第三季度):如果您想克隆子模块并将其更新到最新版本:

git clone --recurse-submodules --remote-submodules

如果您只想在他们记录的 SHA1 中克隆它们:

git clone --recurse-submodules

见下文。

请注意,Git 2.29(2020 年第四季度)对子模块处理进行了重大优化。

请参阅Orgad Shaneh ( ) 的提交 a462bee(2020 年 9 月 6 日(由Junio C Hamano 合并 -- --提交 2ce9d4e中,2020 年 9 月 18 日)orgads
gitster

submodule: 禁止检查文件名和对象 id 的引用歧义

签字人:Orgad Shaneh

的 argv 参数collect_changed_submodules()仅包含对象 ID(所有 ref 的对象引用)。

通过传递通知setup_revisions()输入不是文件名,assume_dashdash,这样可以避免每个引用的冗余统计信息。

还抑制refname_ambiguity标志以避免每个对象的文件系统查找。类似的逻辑可以在 cat-file、pack-objects 等中找到。

此更改将我的 repo中( man )的时间从 25 秒减少到 6 秒git fetch


原始答案 2010

正如joschi在评论中提到的那样,git submodule现在支持该--recursive选项(Git1.6.5 及更高版本)。

如果--recursive指定,此命令将递归到已注册的子模块中,并更新其中的任何嵌套子模块。

有关init 部分,请参阅以递归方式使用 git 子模块。
git submodule解释更多。

使用 git 1.6.5 及更高版本,您可以通过使用以下选项克隆超级项目来自动执行此操作–-recursive

git clone --recursive git://github.com/mysociety/whatdotheyknow.git

2016 年更新,使用 git 2.8:请参阅“如何使用 git 子模块加速/并行下载git clone --recursive

您可以使用多个线程并行启动获取子模块。
为实例:

git fetch --recurse-submodules -j2

更好的是,使用 Git 2.23(2019 年第三季度),您可以通过一个命令将子模块克隆并签出到其跟踪分支!

请参阅Ben Avison ( ) 的提交 4c69101(2019 年 5 月 19 日(由Junio C Hamano 合并 -- --提交 9476094中,2019 年 6 月 17 日)bavison
gitster

clone: 添加--remote-submodules标志

在使用之前,如果您希望子模块在其远程跟踪分支上而不是在超级项目中记录的 SHA-1 中签出,git clone --recurse-submodules则以前无法将--remote开关传递给隐式命令。git submodule update

这个补丁纠正了这种情况。
它实际上也传递--no-fetchgit submodule update它们,因为子模块刚刚被克隆,因此再次从远程获取只会减慢速度。

这意味着:

--[no-]remote-submodules:

所有被克隆的子模块将使用子模块的远程跟踪分支的状态来更新子模块,而不是超级项目记录的 SHA-1。相当于传递--remotegit submodule update

于 2010-09-26T08:17:08.913 回答
157

[快速回答]

您可以使用此命令克隆包含所有子模块的 repo:

git clone --recursive YOUR-GIT-REPO-URL

或者,如果您已经克隆了项目,您可以使用:

git submodule init
git submodule update
于 2017-10-16T15:47:26.617 回答
72

[快速回答]

克隆父 repo(包括一些子模块 repo)后,执行以下操作:

git submodule update --init --recursive
于 2019-07-01T09:39:55.503 回答
38

如果您的子模块已添加到分支中,请务必将其包含在您的克隆命令中...

git clone -b <branch_name> --recursive <remote> <directory>
于 2012-03-07T22:28:58.290 回答
33

使用此命令克隆包含所有子模块的 repo

git clone --recurse-submodules git@gitlab.staging-host.com:yourproject

更新所有子模块的代码

git submodule update --recursive --remote
于 2021-03-18T12:44:09.093 回答
32

尝试这个:

git clone --recurse-submodules

假设您已经将子模块添加到父项目,它会自动提取子模块数据。

于 2013-01-16T18:39:31.563 回答
29

我认为您可以执行 3 个步骤:

git clone
git submodule init
git submodule update
于 2019-01-16T14:11:07.983 回答
23

迟到的答复

// git CLONE INCLUDE-SUBMODULES ADDRESS DESTINATION-DIRECTORY
git clone --recursive https://USERNAME@bitbucket.org/USERNAME/REPO.git DESTINATION_DIR

正如我刚刚花了整整一个小时和一个朋友摆弄一样:即使您对 BitBucket 拥有管理员权限,也请始终克隆原始存储库并使用拥有该存储库的人的密码。很烦人发现你遇到了这个陷阱:P

于 2013-02-16T16:10:25.333 回答
22

您可以--recursive在克隆存储库时使用该标志。此参数强制 git 克隆存储库中所有已定义的子模块。

git clone --recursive git@repo.org:your_repo.git

克隆后,有时子模块分支可能会改变,所以在它之后运行这个命令:

git submodule foreach "git checkout master"
于 2019-01-17T04:44:37.940 回答
21

试试这个在 git 存储库中包含子模块。

git clone -b <branch_name> --recursive <remote> <directory>

或者

git clone --recurse-submodules
于 2018-02-05T04:54:53.597 回答
16

只需在您的项目目录中执行这些操作。

$ git submodule init
$ git submodule update
于 2021-03-08T04:54:29.407 回答
13

子模块并行获取旨在通过一次获取多个存储库来减少获取存储库及其所有相关子模块所需的时间。这可以通过使用新的 --jobs 选项来完成,例如:

git fetch --recurse-submodules --jobs=4

根据 Git 团队的说法,这可以大大加快包含许多子模块的存储库的更新速度。当使用没有新的 --jobs 选项的 --recurse-submodules 时,Git 将一个一个地获取子模块。

来源: http: //www.infoq.com/news/2016/03/git28-released

于 2016-04-06T12:07:15.040 回答
11

对于 GitHub 存储库,我遇到了同样的问题。我的帐户缺少 SSH 密钥。过程是

  1. 生成 SSH 密钥
  2. 将新的 SSH 密钥添加到您的 GitHub 帐户

git clone --recursive YOUR-GIT-REPO-URL然后,您可以使用子模块 ( )克隆存储库

或者

运行git submodule initgit submodule update获取已克隆存储库中的子模块。

于 2019-08-26T20:51:34.357 回答
10

如果它是一个新项目,您可以这样做:

$ git clone --recurse-submodules https://github.com/chaconinc/YourProjectName 

如果它已经安装比:

$ cd YourProjectName (for the cases you are not at right directory) 
$ git submodule init
$ git submodule update
于 2019-08-26T08:46:46.037 回答
7

尝试这个。

git clone -b <branch_name> --recursive <remote> <directory>

如果您在分支中添加了子模块,请确保将其添加到克隆命令中。

于 2018-06-18T08:29:44.407 回答
-5
git submodule foreach git pull origin master
于 2021-06-21T05:58:47.447 回答