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

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


当前回答

如何“git克隆”回购,包括其子模块?

(我把它作为一个独立的答案,因为我认为这在git中很重要)

可悲的是,但在很多情况下,那些不太知情的克隆人会错过外在的东西:

vc大便(https://github.com/dirk-thomas/vcstool)数字子报表(https://github.com/ingydotnet/git-subrepo)git链接(https://github.com/chronoxor/gil).gitsvnextmodule(由smartgit处理)。git外部(https://github.com/develer-staff/git-externals)git x模块(https://gitmodules.com/)等等

作为备份,无法创建裸克隆(--bare)+完全递归(--recursive)来镜像(--mirror)所有引用(而不仅仅是所有外部引用):使用--recursive和--bare克隆git存储库

因此,对于git,没有一种内置的方法可以完全接受外部依赖关系。这意味着没有一种方法可以完全备份任何随机git存储库。

其他回答

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

$ git submodule init
$ git submodule update

我认为你可以采取三个步骤:

git clone
git submodule init
git submodule update

如果子模块是私有子模块,您可以使用凭证存储,这样它也可以递归地克隆其私有子模块。

USER=${GITHUB_ACTOR}
TOKEN=${{ secrets.JEKYLL_GITHUB_TOKEN }}

git config --global credential.helper store
echo "https://${USER}:${TOKEN}@github.com" > ~/.git-credentials

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

我用它来克隆我的子模块,其中私有模块位于第5层深处。请允许我向您展示它是如何运作的:

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

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

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

git submodule foreach "git checkout master"

[快速回答]

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

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

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

git submodule init
git submodule update