gpt4 book ai didi

git submodule checkout 来自各自 Remote 的不同分支的最新提交

转载 作者:行者123 更新时间:2023-12-05 05:48:07 32 4
gpt4 key购买 nike

我的父项目添加了几个子模块。

出于部署目的,我想在每个子模块中 check out 一个特定的分支,例如 masterstaging 等,并在每个子模块中 pull 该分支的最新提交。

我已经检查了关于 SO 的各种答案,如下所示:

但是对于 2022 年的最佳实践是什么,我感到非常困惑。我有 git version 2.30.1 (Apple Git-130)

到目前为止,我已经了解此命令可用于获取已在 .gitmodules 文件中提到的分支的最新提交(从各自的远程)。

git submodule update --remote --merge

但是,对于差异环境部署,我该如何使用 -b 选项同时切换所有子模块中的分支?

最佳答案

我找到了适合我的解决方案。

简单的解决方案

git submodule foreach <command>

上述命令将为每个子模块运行。

所以我运行了以下 2 个命令来 check out 所需的分支并在每个子模块中提取最新提交

git submodule foreach git checkout -B staging origin/staging
git submodule foreach git pull --rebase

复杂的解决方案

以下命令更新了 .gitmodules 文件中的目标分支

git config -f .gitmodules submodule.<submodule_path>.branch <target_branch_name>

我使用以下命令对所有子模块进行批量更新

git submodule status | awk '{ print $2 }' | xargs -I {} git config -f .gitmodules submodule.{}.branch master

然后以下命令 pull (获取并 merge )每个子模块各自远程的最新提交

git submodule update --remote --merge

--merge 备选方案:

  • 省略 - 省略--merge 将对子模块的远程仓库进行硬重置
  • --rebase - 这将在远程分支最新提交的子模块之上重新定位您的本地更改

关于git submodule checkout 来自各自 Remote 的不同分支的最新提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70865847/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com