git CLI 如何执行同时更新所有子模块的检出?

git CLI how do I perform a checkout that also updates all of the submodules?

所以我可以克隆一个分支并获取所有子模块:

git clone <url> -b myBranch --recursive

这很棒 :) 但是后来我想切换到另一个分支,所以我这样做了:

git checkout myBranch2

这更新了我项目的顶层,但没有更新子模块(我在这里没有看到递归选项)。事实上,我得到的输出表明它知道子模块需要更新,但我只是不知道该怎么做:

c:\dev> git checkout myBranch2
M        SubModule1
M        SubModule2
Branch myBranch2 set up to track remote branch myBranch2 from origin.
Switched to a new branch 'myBranch2'

然后我检查了 SubModule1 的版本,它仍然指向最初签出的版本(最新),而不是它应该指向的版本(大约 3 个签入旧版本)与分支 myBranch2.

当然,我可以只克隆一个新的并同时拥有两者 - 这是我通常会做的,但它让我感到烦恼,因为我不知道该怎么做......它可能会有用。

git checkout运行

之后
git submodule update --checkout --recursive

来自 git submodule 文档:

git submodule update ...

Update the registered submodules to match what the superproject expects by cloning missing submodules and updating the working tree of the submodules. The "updating" can be done in several ways depending on command line options and the value of submodule.<name>.update configuration variable. Supported update procedures are:

  • checkout - the commit recorded in the superproject will be checked out in the submodule on a detached HEAD. This is done when --checkout option is given, or no option is given, and submodule.<name>.update is unset, or if it is set to checkout.

  • ...

--recursive

This option is only valid for foreach, update, status and sync commands. Traverse submodules recursively. The operation is performed not only in the submodules of the current repo, but also in any nested submodules inside those submodules (and so on).

您还可以设置 post-checkout hook 以便自动执行子模块的更新。