获取指定子模块的当前提交 ID

Get the current commit id of specified submodule

我想获取指定子模块的当前提交 ID。我想当我 cd 进入子模块目录和 运行 git rev-parse HEAD 我注意到这是一个超级项目当前 ID 后我得到了这个。 也尝试了 git submodule status | grep <submodule_name>,但它对我来说很慢。知道如何更快地获取此信息吗?

首先,因为 by brookbot, git submodule status 将为每个子模块打印当前签出的提交的 SHA-1,以及子模块路径和 git 描述的输出SHA-1.
看我的 .


您可以从 git ls-files -s (as in this answer)

开始
cd /path/to/parent/repo
git ls-files -s yourSubmodule

请注意在 yourSubmodule 之后没有尾随 '/'(这是检出子模块的根文件夹)

这将给出与 gitlink (special entry in the index of the parent repo)

关联的模式和 sha1
160000 4d77d23305c5623356955ef9f908f4ec76780ba9 0       yourSubmodule

(“0”代表 the stage number

备选方案:

cd /path/to/repo/parentFolder/of/submodule

git ls-tree @ yourSubmodule
git rev-parse @:./yourSubmodule

rev-parse只有returns子模块SHA1。


正如 heloman 评论的那样,您还可以通过以下方式找到 SHA1:

git rev-parse HEAD:path-to-your-sub-module

在“How to see which commit a git submodule points at”中查看更多内容。

在所需的项目文件夹中打开 shell,然后键入以下 git 命令:

git submodule

结果输出如下:

<module commit> <module-path> (<module-branch)
... 

git submodule status | grep <submodule_name> also but its to slow to me

从 Git 2.16(2017 年第四季度,OP 提出问题两年后)开始,这应该会更快(即使我的 仍然是一个可行的选择)。

参见 commit a9f8a37, commit 9f580a6 (06 Oct 2017), and commit 74a1064 (29 Sep 2017) by Prathamesh Chavan (pratham-pc)
(由 Junio C Hamano -- gitster -- in commit a1bf46e 合并,2017 年 11 月 6 日)

submodule: port submodule subcommand 'status' from shell to C

Mentored-by: Christian Couder
Mentored-by: Stefan Beller
Signed-off-by: Prathamesh Chavan

This aims to make git submodule 'status' a built-in. Hence, the function cmd_status() is ported from shell to C. This is done by introducing four functions: module_status(), submodule_status_cb(), submodule_status() and print_status().

The function module_status() acts as the front-end of the subcommand. It parses subcommand's options and then calls the function module_list_compute() for computing the list of submodules. Then this functions calls for_each_listed_submodule() looping through the list obtained.

Then for_each_listed_submodule() calls submodule_status_cb() for each of the submodule in its list. The function submodule_status_cb() calls submodule_status() after passing appropriate arguments to the funciton. Function submodule_status() is responsible for generating the status each submodule it is called for, and then calls print_status().

Finally, the function print_status() handles the printing of submodule's status.

Function set_name_rev() is also ported from git submodule to the submodule--helper builtin function compute_rev_name(), which now generates the value of the revision name as required.

但是.. 当“git submodule”命令的一部分用 C 重写时,“git submodule 状态”报告已初始化但尚未填充的子模块的方式尚未正确重新实现,这已更正 Git 2.26(2020 年第一季度)

参见 commit f38c924 (02 Feb 2020), and commit 3b2885e, commit ace912b (24 Jan 2020) by Peter Kaestle (``)
(由 Junio C Hamano -- gitster -- in commit f2dcfcc 合并,2020 年 2 月 14 日)

submodule: fix status of initialized but not cloned submodules

Signed-off-by: Peter Kaestle

Original bash helper for "submodule status" was doing a check for initialized but not cloned submodules and prefixed the status with a minus sign in case no .git file or folder was found inside the submodule directory.

This check was missed when the original port of the functionality from bash to C was done.

这不是最漂亮的,但是如果你 cd cd 进入子模块,你可以 git log | head -1

git rev-parse HEAD:path/to/submodule

对于我的自动化用例,我发现这种方式比公认答案的替代方案更好,因为它输出 一个 hipotical path/to/submodule.[=13 的提交 sha =]

PS: Credits and thanks to heloman