如何克隆 git 存储库及其默认分支上的子模块检出?

How to clone a git repository with its submodules checkout on their default branches?

关于问题:

我们发现有必要在执行 git clone --recursive 时将 git 子模块检出到它们的默认分支,但该怎么做?

我尝试搜索并发现了另一个问题:

  1. Easy way pull latest of all submodules

建议使用命令 git clone --recurse-submodules 但克隆存储库后,其子模块仍未在其默认分支上检出。

git submodule foreach git checkout master

您可以在每个子模块中使用 git submodule foreach 到 运行 任意命令。 --recursive 标志将通过子模块的子模块递归。 git remote show [name-of-remote] 会说哪个分支 [name-of-remote] 当前处于活动状态。将它们与其他一些工具结合使用以清理 git remote show 的输出结果:

git submodule foreach --recursive "git checkout $(git remote show origin | grep 'HEAD branch' | sed 's/.*: //')"

当然,这取决于已经克隆了子模块。