如何“git clone”包括子模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3796927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:09:33  来源:igfitidea点击:

How to "git clone" including submodules?

gitgit-submodules

提问by Mark

I'm trying to put a submodule into a repo. The problem is that when I clone the parent repo, the submodule folder is entirely empty.

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

Is there any way to make it so that git clone parent_repoactually puts data in the submodule folder?

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

For example, http://github.com/cwolves/sequelize/tree/master/lib/, nodejs-mysql-nativeis pointing at an external git submodule, but when I checkout the sequelizeproject, that folder is empty.

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

回答by Mathias Bynens

With version 2.13 of Git and later, --recurse-submodulescan be used instead of --recursive:

使用 Git 2.13 及更高版本,--recurse-submodules可以代替--recursive

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

Editor's note: -j8is an optional performance optimization that became available in version 2.8, and fetches up to 8 submodules at a time in parallel — see man git-clone.

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

With version 1.9 of Git up until version 2.12 (-jflag only available in version 2.8+):

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

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

With version 1.6.5 of Git and later, you can use:

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

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

For already cloned repos, or older Git versions, use:

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

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

回答by LiraNuna

You have to do two things before a submodule will be filled:

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

git submodule init 
git submodule update

回答by VonC

Git 2.23 (Q3 2019): if you want to clone andupdate the submodules to their latest revision:

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

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

If you just want to clone them at their recorded SHA1:

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

git clone --recurse-submodules

See below.

见下文。



Original answer 2010

原始答案 2010

As joschimentions in the comments, git submodulenow supports the --recursiveoption (Git1.6.5 and more).

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

If --recursiveis specified, this command will recurse into the registered submodules, and update any nested submodules within.

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

See Working with git submodules recursivelyfor the init part.
See git submoduleexplainedfor more.

有关init 部分,请参阅以递归方式使用 git 子模块
有关更多信息,请参阅git submodule说明

With version 1.6.5 of git and later, you can do this automatically by cloning the super-project with the –-recursiveoption:

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

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


Update 2016, with git 2.8: see "How to speed up / parallelize downloads of git submodules using git clone --recursive?"

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

You can initiate fetching the submodule using multiple threads, in parallel.
For instances:

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

git fetch --recurse-submodules -j2


Even better, with Git 2.23 (Q3 2019), you can clone and checkout the submodule to their tracking branch in one command!

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

See commit 4c69101(19 May 2019) by Ben Avison (bavison).
(Merged by Junio C Hamano -- gitster--in commit 9476094, 17 Jun 2019)

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

clone: add --remote-submodulesflag

When using git clone --recurse-submodulesthere was previously no way to pass a --remoteswitch to the implicit git submodule updatecommand for any use case where you want the submodules to be checked out on their remote-tracking branch rather than with the SHA-1 recorded in the superproject.

This patch rectifies this situation.
It actually passes --no-fetchto git submodule updateas well on the grounds they the submodule has only just been cloned, so fetching from the remote again only serves to slow things down.

clone: 添加--remote-submodules标志

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

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

That means:

这意味着:

--[no-]remote-submodules:

All submodules which are cloned will use the status of the submodule's remote-tracking branch to update the submodule, rather than the superproject's recorded SHA-1. Equivalent to passing --remoteto git submodule update.

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

回答by Javier C.

[Quick Answer]

[快速回答]

You can use this command to clone your repo with all the submodules:

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

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

Or if you have already cloned the project, you can use:

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

git submodule init
git submodule update

回答by Mars Redwyne

If your submodule was added in a branch be sure to include it in your clone command...

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

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

回答by nweiler

Try this:

尝试这个:

git clone --recurse-submodules

It automatically pulls in the submodule data assuming you have already added the submodules to the parent project.

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

回答by muhammad ali e

I think you can go with 3 steps:

我认为您可以通过 3 个步骤进行:

git clone
git submodule init
git submodule update

回答by kaiser

late answer

迟到的答复

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

As I just spent a whole hour fiddling around with a friend: Even if you have Admin rights on BitBucket, always clone the ORIGINAL repository and use the password of the one who owns the repo. Annoying to find out that you ran into this minetrap :P

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

回答by radhey shyam

Try this for including submodules in git repository.

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

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

or

或者

git clone --recurse-submodules

回答by Ahmad Azimi

You can use the --recursiveflag when cloning a repository. This parameter forces git to clone all defined submodules in the repository.

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

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

After cloning, sometimes submodules branches may be changed, so run this command after it:

克隆后,有时子模块分支可能会发生变化,因此在它之后运行以下命令:

git submodule foreach "git checkout master"