我有一个Git存储库,其中有几个子模块。如何在git子模块init运行后列出所有子模块的名称?

git子模块foreach命令可以回显子模块的名称,但这只有在它们被签出后才有效,而在init步骤之后没有发生。在签出之前,链中还有更多的步骤需要执行,我不希望将子模块的名称硬连接到脚本中。

那么是否存在一个Git命令来获取所有当前已注册但尚未签出的子模块的名称?


当前回答

如果你想要显示嵌套的子模块,你可以使用git submodule status或者git submodule status——recursive。

从Git文档中:

显示子模块的状态。的SHA-1 的子模块的当前签出提交 子模块路径和git的输出为SHA-1描述。每一个 如果子模块未初始化,SHA-1将以-作为前缀,+ 如果当前检出的子模块提交不匹配SHA-1 在包含该子模块的存储库的索引中找到U 存在合并冲突。

其他回答

使用内置git函数显示每个子模块的所有信息:

git submodule foreach -q git config -l

或者只是url:

git submodule foreach -q git config remote.origin.url

从这里偷来的。

假设你目前已经注册了,但还没有签出子模块,如下:

$ cat .gitmodules
[submodule ".github/workflows/packages"]
        path = .github/workflows/packages
        url = https://github.com/MarketLeader/Packages
[submodule ".github/workflows/builders"]
        path = .github/workflows/builders
        url = https://github.com/chetabahana/runner
[submodule "docs"]
        path = docs
        url = https://github.com/eq19/lexer

下面是Git命令来获取存储库的根目录:

$ git submodule foreach -q '[[ "$sm_path" == */* ]] || git config remote.origin.url'
https://github.com/eq19/lexer

下面是Git命令在特定路径上获取列表:

$ git submodule foreach -q '[[ ! "$sm_path" == .github/* ]] || git config remote.origin.url'
https://github.com/chetabahana/runner
https://github.com/MarketLeader/Packages

当然,你也可以用其他命令修改git config remote.origin.url。

顺便说一下,我使用的是git 2.38.1版

这招对我很管用:

git ls-files --stage | grep ^160000

它基于这篇伟大的文章:理解Git子模块

它必须读取grep ^160000。

Use:

$ git submodule

它将列出指定Git存储库中的所有子模块。

下面的命令将列出子模块:

git submodule--helper list

输出如下所示:

<mode> <sha1> <stage> <location>

注意:需要Git 2.7.0或以上版本。