git submodule foreach checkout 超级模块分支

Spa*_*ose 3 git git-submodules

考虑一个 git 存储库 Foo/,它有子模块 bar1/ 和 bar2/。

其中每一个都有相同的分支:1 和 2。

我进入超级模块,并且想要更新超级模块以包含 bar1 和 bar2 来源的最新提交。我已经初始化并更新了超级模块,因此 bar1 和 bar2 中有工作树,但它们处于分离状态。我可以执行以下操作:

cd foo;
git checkout 1
git submodule foreach git checkout 1
git pull
Run Code Online (Sandbox Code Playgroud)

现在,令我烦恼的是重复分支标识符。我可以做类似“git submodule foreach git checkout $CURRENT_BRANCH_ID”之类的事情吗?有更好的选择吗?

Von*_*onC 5

默认情况下,子模块始终处于分离 HEAD 模式

您可以使每个子模块遵循一个分支。
请参阅“如何使现有子模块跟踪分支”。

cd /path/to/your/parent/repo/Foo
git config -f .gitmodules submodule.bar1.branch branch1
git config -f .gitmodules submodule.bar2.branch branch2
Run Code Online (Sandbox Code Playgroud)

那么你需要做的就是:

git submodule update --remote
Run Code Online (Sandbox Code Playgroud)

这会将每个子模块更新为各自上游分支的最新版本(获取+签出)。