如何克隆git存储库,以便它也克隆其子模块?

运行gitclone$REPO_URL只会创建空的子模块目录。


当前回答

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

git clone --recurse-submodules --remote-submodules <repo-URL>

如果您只想在其录制的SHA1上克隆它们:

git clone --recurse-submodules <repo-URL>

见下文。

注意,Git 2.29(2020年第4季度)为子模块处理带来了显著的优化。

请参见Orgad Shaneh(orgads)提交的462bee(2020年9月6日)。(于2020年9月18日由Junio C Hamano(吉斯特)在提交中合并)

子模块:禁止检查对象id的文件名和ref歧义签字人:Orgad Shaneh

collect_changed_submodules()的argv参数仅包含对象id(所有引用的对象引用)。通过传递assume_dashdash通知setup_revisions()输入不是文件名,这样可以避免每个引用的冗余stat。还要抑制refname_ambiguity标志,以避免对每个对象进行文件系统查找。类似的逻辑可以在cat文件、pack对象等中找到。此更改将我的回购中的git fetch(man)时间从25秒缩短到6秒。


2010年原始答案

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

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

有关init部分,请参阅递归使用git子模块。有关详细信息,请参阅git子模块。

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

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

2016年更新,使用git 2.8:请参阅“如何使用git clone加速/并行下载git子模块——递归?”

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

git fetch --recurse-submodules -j2

更棒的是,使用Git 2.23(2019年第3季度),您可以在一个命令中克隆子模块并将其检出到其跟踪分支!

参见Ben Avison(bavison)的承诺4c69101(2019年5月19日)。(由Junio C Hamano(吉斯特)于2019年6月17日提交9476094合并)

clone:add--远程子模块标志

当使用gitclone--递归子模块时,以前无法将--remote开关传递给隐式git子模块更新命令您希望子模块在其上检出的任何用例远程跟踪分支,而不是超级项目中记录的SHA-1。此修补程序纠正了这种情况。它实际上通过了——也没有获取git子模块更新,因为子模块刚刚被克隆,所以再次从远程获取只会减慢速度。

这意味着:

--[no-]remote-submodules:

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

其他回答

[快速回答]

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

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

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

git submodule init
git submodule update

迟交的答复

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

正如我刚刚花了整整一个小时和一个朋友在一起闲逛:即使你有BitBucket的管理员权限,也要克隆原始存储库,并使用拥有回购的人的密码。发现你撞上了这个地雷陷阱,我很恼火:P

我建议:

# - git submodule init initializes your local configuration file to track the submodules your repository uses, it just sets up the configuration so that you can use the git submodule update command to clone and update the submodules.
git submodule init
# - git submodule update --init initializes your local configuration file and clones the submodules for you, using the commit specified in the main repository.
#   note, command bellow will not pull the right branch -- even if it's in your .gitmodules file, for that you need remote. Likely because it looks at the origin (pointer to remote) in github for the available branches.
#   note, bellow pulls the submodules if you didn't specify them when cloning parent project, ref: https://youtu.be/wTGIDDg0tK8?t=119
git submodule update --init

如果您有子模块的特定分支,请将其更改为:

# - git submodule init initializes your local configuration file to track the submodules your repository uses, it just sets up the configuration so that you can use the git submodule update command to clone and update the submodules.
git submodule init

# - The --remote option tells Git to update the submodule to the commit specified in the upstream repository, rather than the commit specified in the main repository.
#git submodule update --init --remote
git submodule update --init --recursive --remote meta-dataset

对于测试的完整示例:

# decided against this because it seems complicated

# - note to clone uutils with its submodule do (cmd not tested):
cd $HOME
git clone --recurse-submodules git@github.com:brando90/ultimate-utils.git

# - git submodules
cd $HOME/diversity-for-predictive-success-of-meta-learning

# - in case it's needed if the submodules bellow have branches your local project doesn't know about from the submodules upstream
git fetch

# -- first repo
# - adds the repo to the .gitmodule & clones the repo
git submodule add -f -b hdb --name meta-dataset git@github.com:brando90/meta-dataset.git meta-dataset/

# - ref for init then update: https://stackoverflow.com/questions/3796927/how-do-i-git-clone-a-repo-including-its-submodules/3796947#3796947
#git submodule init
#git submodule update

# - git submodule init initializes your local configuration file to track the submodules your repository uses, it just sets up the configuration so that you can use the git submodule update command to clone and update the submodules.
git submodule init
# - git submodule update --init initializes your local configuration file and clones the submodules for you, using the commit specified in the main repository.
#   note, command bellow will not pull the right branch -- even if it's in your .gitmodules file, for that you need remote. Likely because it looks at the origin (pointer to remote) in github for the available branches.
#   note, bellow pulls the submodules if you didn't specify them when cloning parent project, ref: https://youtu.be/wTGIDDg0tK8?t=119
git submodule update --init
# - The --remote option tells Git to update the submodule to the commit specified in the upstream repository, rather than the commit specified in the main repository.
#git submodule update --init --remote
git submodule update --init --recursive --remote meta-dataset

# - check we are using the right branch https://stackoverflow.com/questions/74998463/why-does-git-submodule-status-not-match-the-output-of-git-branch-of-my-submodule
git submodule status
cd meta-dataset
git branch  # should show hdb
cd ..

# pip install -r $HOME/meta-dataset/requirements.txt

# -- 2nd repo, simplified commands from above
git submodule add -f -b hdb --name pytorch-meta-dataset git@github.com:brando90/pytorch-meta-dataset.git pytorch-meta-dataset/

git submodule init
git submodule update --init --recursive --remote meta-dataset

# - check it's in specified branch
git submodule status
cd pytorch-meta-dataset
git branch  # should show hdb
cd ..

# pip install -r $HOME/pytorch-meta-dataset/requirements.txt

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

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

or

git clone --recurse-submodules

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

生成SSH密钥向GitHub帐户添加新的SSH密钥

然后,您可以使用子模块克隆存储库(gitclone--递归YOUR-git-REPO-URL)

or

运行gitsubmodule init和gitsubmodule update以获取已克隆存储库中的子模块。