查找 git 中的所有子模块及其提交

Find all submodules in git and their commits

我的 git 存储库中有一个包含子模块的项目,这些子模块包含 feather 子模块。

由于我不对所有子模块负责,所以我不知道在更新我正在查看的提交后那里发生了什么变化。出于这个原因,我想记录当前状态。

这意味着,我想知道我正在使用的所有子模块(显式和非显式)。对于每个子模块,我想知道它的标签 and\or commit.

我发现:

git ls-files --stage

有用,但只显示来自我的存储库的提交,而不是子模块内部的提交。

有什么想法吗?

git submodule foreach --recursive "git describe --tags HEAD --exact-match 2>/dev/null || git rev-parse HEAD"

在每个子模块中递归,运行命令git describe --tags HEAD --exact-match 2>/dev/null || git rev-parse HEAD

首先尝试找到恰好指向每个子模块的头部提交的最新标记。如果未找到标记,则 return 提交。

git ls-files --stage

helpful, but shows only commits from my repository and not inside the submodules.

在 Git 2.36(2022 年第 2 季度)之前,“ls-files”的许多输出模式无法使用其“--recurse-submodules”选项,但“--stage " mode 现已被教导使用它。

参见 commit 290eada (23 Feb 2022) by Jonathan Tan (jhowtan)
(由 Junio C Hamano -- gitster -- in commit 7a4e06c 合并,2022 年 3 月 6 日)

ls-files: support --recurse-submodules --stage

Signed-off-by: Jonathan Tan

e77aa33 ("ls-files: optionally recurse into submodules", 2016-10-10, Git v2.11.0-rc0 -- merge listed in batch #11) taught ls-files the --recurse-submodules argument, but only in a limited set of circumstances.

In particular, --stage was unsupported, perhaps because there was no repo_find_unique_abbrev(), which was only introduced in 8bb9557 ("sha1-name.c``: add repo_find_unique_abbrev_r()", 2019-04-16, Git v2.22.0-rc0 -- merge listed in batch #8).

This function is needed for using --recurse-submodules with --stage.

git ls-files 现在包含在其 man page 中:

--recurse-submodules:

Recursively calls ls-files on each active submodule in the repository.

Currently there is only support for the --cached and --stage modes.