递归地为所有git子模块执行git pull操作

7

我的个人库有一些子模块仓库。以下是相关命令:

$ git submodule foreach git pull origin master

进入 Ruby 代码库后,我面临了以下结果,因为 Ruby 代码库似乎没有主分支,导致 "git pull" 命令无法执行。
Entering 'rails'
From git://github.com/rails/rails
 * branch            master     -> FETCH_HEAD
Already up-to-date.
Entering 'roo'
From git://github.com/hmcgowan/roo
 * branch            master     -> FETCH_HEAD
Already up-to-date.
Entering 'ruby'
fatal: Couldn't find remote ref master
Stopping at 'ruby'; script returned non-zero status.

所以我的问题是,我应该如何使用git命令仅对所有子模块进行git pull操作? 我需要编写一个脚本吗?我希望git能够提供只需一条命令的解决方案。


1
如果您不希望git submodule foreach ...在子仓库中的一个错误发生时停止,请将||:添加到命令中,例如git submodule foreach 'git pull origin master || :' - Stefan Näwe
3个回答

7
只需在子模块命令后添加|| true即可:
git submodule foreach 'git commit -m "my commit message" || true'

5
git子模块通常处于分离头状态,因此在进行合并阶段时,git pull 无法理解你的意图。如果你只是想将最新更改获取到仓库中,请尝试使用 git submodule foreach git fetch。如果你想要将每个子模块的 master 更新到各自的 origin/master,那么可以使用 git submodule foreach git checkout master; git submodule foreach git merge origin/master

然后,当然,您需要决定每个子模块的版本,以便您的主存储库使用它们(我不建议始终盲目选择 origin/master - 它可能不稳定 - 最好选择一个已知的好标签或其他),检出子模块中的这些版本,并在主存储库中进行相应的 git addgit commit 操作。


0

Git 2.18(Q2 2018)可能通过改进git submodule update来避免该错误,该错误会影响git submodule pull

"git submodule update"尝试针对上游存储库执行两种不同类型的"git fetch",以获取绑定在子模块路径上的提交,但如果第一种类型(即普通获取)失败,则会错误地放弃,从而使第二种“最后手段”(即按对象名称获取确切提交对象)无效。
这已得到纠正。

查看 提交 e30d833(2018年5月15日)由Stefan Beller(stefanbeller提供。
(由Junio C Hamano -- gitster --提交 a173ddd中合并,2018年5月30日)

git-submodule.sh:更努力地获取子模块

This is the logical continuum of fb43e31 (submodule: try harder to fetch needed sha1 by direct fetching sha1, 2016-02-23, Git 2.8.0) and fixes it as some assumptions were not correct.

The commit states:

If $sha1 was not part of the default fetch ... fail ourselves here assumes that the fetch_in_submodule only fails when the serverside does not support fetching by sha1.

There are other failures, why such a fetch may fail, such as

fatal: Couldn't find remote ref HEAD

which can happen if the remote side doesn't advertise HEAD and we do not have a local fetch refspec.

Not advertising HEAD is allowed by the protocol spec and would happen, if HEAD points at an unborn branch for example.

Not having a local fetch refspec can happen when submodules are fetched shallowly, as then git-clone doesn't setup a fetch refspec.

So do try even harder for a submodule by ignoring the exit code of the first fetch and rather relying on the following is_tip_reachable to see if we try fetching again.


注意:Git 2.22(2019年第二季度)已经改进了子模块获取错误信息。

请参见提交记录bd5e567(2019年3月13日),作者为Jonathan Tan(jhowtan
(由Junio C Hamano -- gitster --提交记录32414ce中合并,2019年4月9日)

子模块:清晰地解释第一次尝试失败的原因

When cloning with --recurse-submodules a superproject with at least one submodule with HEAD pointing to an unborn branch, the clone goes something like this:

Cloning into 'test'...
<messages about cloning of superproject>
Submodule '<name>' (<uri>) registered for path '<submodule path>'
Cloning into '<submodule path>'...
fatal: Couldn't find remote ref HEAD
Unable to fetch in submodule path '<submodule path>'
<messages about fetching with SHA-1>
From <uri>
    * branch            <hash> -> FETCH_HEAD
Submodule path '<submodule path>': checked out '<hash>'

In other words, first, a fetch is done with no hash arguments (that is, a fetch of HEAD) resulting in a "Couldn't find remote ref HEAD" error; then, a fetch is done given a hash, which succeeds.

This commit improves the notice to be clearer that we are retrying the fetch, and that the previous messages (in particular, the fatal errors from fetch) do not necessarily indicate that the whole command fails.


请注意,使用 Git 2.34(2021 年第四季度),git submodule(man) 的部分重新实现将继续使用 C 语言。

请查看 提交 c51f8f9(2021年8月24日),作者为 Atharva Raykar (tfidfwastaken)
(由Junio C Hamano -- gitster --提交 e78db9d中合并,2021年9月20日)

子模块--辅助程序:从 C 运行更新程序

指导者:Christian Couder
指导者:Shourya Shukla
签署者:Atharva Raykar

添加一个新的子模块--辅助程序子命令 run-update-procedure,如果子模块的 SHA1 值与超级项目预期的不匹配,则运行更新过程。

这意味着上述提到的 is_tip_reachable Shell 方法已不再存在。


同样的重写在 Git 2.36 (2022 年第二季度) 中继续进行,和 "git submodule update"(man)

请查看 提交 c9d2562, 提交 104744f, 提交 97cb977, 提交 29a5e9e, 提交 1012a5c, 提交 e441966, 提交 1a0b78c, 提交 f7bdb32 (2022年3月4日) 由 Glen Choo (chooglen) 提交。
请查看 提交 3ce52cb, 提交 5312a85, 提交 a77c3fc (2022年3月4日) 由 Atharva Raykar (tfidfwastaken) 提交。
请查看 提交 ed9c848, 提交 aca8568 (2022年3月4日) 由 Ævar Arnfjörð Bjarmason (avar) 提交。
(由 Junio C Hamano -- gitster -- 合并于 提交 7649bfb, 2022年3月23日)

随着 Git 2.39(2022 年第四季度)的更新,重写工作正在继续进行,其中包括准备删除 git-submodule.sh 并用内置替换它。

请查看 提交记录 69d9446, 提交记录 1b6e200, 提交记录 64f48ad, 提交记录 82ff877, 提交记录 46e87b5, 提交记录 d50d848, 提交记录 435285b, 提交记录 44874cb, 提交记录 cc74a4a (2022年11月08日) 作者为 Ævar Arnfjörð Bjarmason (avar).
(由Junio C Hamano -- gitster --合并于提交记录1107a39, 2022年11月23日)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接