如何“git克隆”包括子模块?

问题描述 投票:1744回答:12

我试图将子模块放入回购。问题是当我克隆父repo时,子模块文件夹是完全空的。

是否有任何方法可以使git clone parent_repo实际将数据放入子模块文件夹?

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

git git-submodules
12个回答
2736
投票

使用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

使用版本1.9的Git直到版本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

对于已经克隆的repos或较旧的Git版本,请使用:

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

14
投票

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

git clone --recursive [email protected]:your_repo.git

克隆后,有时可能会更改子模块分支,因此请在其后运行此命令:

git submodule foreach "git checkout master"

10
投票

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

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

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

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


5
投票

[Quick Answer]

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

git submodule update --init --recursive

4
投票

试试这个。

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

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


433
投票

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

git submodule init 
git submodule update

170
投票

原答案2010

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

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

有关init部分,请参阅Working with git submodules recursively。 有关更多信息,请参阅git submodule explained

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

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

使用git 2.8更新2016:请参阅“How to speed up / parallelize downloads of git submodules using git clone --recursive?

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

git fetch --recurse-submodules -j2

更好的是,使用Git 2.23(Q3 2019),您可以在一个命令中克隆并检查子模块到其跟踪分支!

commit 4c69101Ben Avison (``)(2019年5月19日)。 (由Junio C Hamano -- gitster --合并于commit 9476094,2019年6月17日)

clone:添加--remote-submodules标志

当使用git clone --recurse-submodules时,以前没有办法将--remote开关传递给隐式git submodule update命令用于任何用例,在这种情况下,您希望在远程跟踪分支上检出子模块而不是在超级项目中记录的SHA-1。

这个补丁纠正了这种情况。 它实际上将--no-fetch传递给git submodule update,因为它们只是刚刚克隆了子模块,因此从遥控器再次取出只会减慢速度。

这意味着:

--[no-]remote-submodules:

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


56
投票

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

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

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

git submodule init
git submodule update

30
投票

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

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

27
投票

试试这个:

git clone --recurse-submodules

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


21
投票

我想你可以用3个步骤:

git clone
git submodule init
git submodule update

19
投票

迟到的答案

// git CLONE INCLUDE-SUBMODULES ADDRESS DESTINATION-DIRECTORY
git clone --recursive https://[email protected]/USERNAME/REPO.git DESTINATION_DIR

因为我花了整整一个小时与朋友一起摆弄:即使你拥有BitBucket的管理员权限,也要克隆ORIGINAL存储库并使用拥有repo的人的密码。很恼火,发现你遇到了这个陷阱:P


17
投票

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

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

要么

git clone --recurse-submodules
© www.soinside.com 2019 - 2024. All rights reserved.