我有一个Git存储库,其中有几个子模块。如何在git子模块init运行后列出所有子模块的名称?
git子模块foreach命令可以回显子模块的名称,但这只有在它们被签出后才有效,而在init步骤之后没有发生。在签出之前,链中还有更多的步骤需要执行,我不希望将子模块的名称硬连接到脚本中。
那么是否存在一个Git命令来获取所有当前已注册但尚未签出的子模块的名称?
我有一个Git存储库,其中有几个子模块。如何在git子模块init运行后列出所有子模块的名称?
git子模块foreach命令可以回显子模块的名称,但这只有在它们被签出后才有效,而在init步骤之后没有发生。在签出之前,链中还有更多的步骤需要执行,我不希望将子模块的名称硬连接到脚本中。
那么是否存在一个Git命令来获取所有当前已注册但尚未签出的子模块的名称?
当前回答
Use:
$ git submodule
它将列出指定Git存储库中的所有子模块。
其他回答
使用内置git函数显示每个子模块的所有信息:
git submodule foreach -q git config -l
或者只是url:
git submodule foreach -q git config remote.origin.url
从这里偷来的。
我用这个:
git config --list|egrep ^submodule
下面是另一种从. Git modules解析Git子模块名称的方法,不需要sed或花哨的IFS设置。: -)
#!/bin/env bash
function stripStartAndEndQuotes {
temp="${1%\"}"
temp="${temp#\"}"
echo "$temp"
}
function getSubmoduleNames {
line=$1
len=${#line} # Get line length
stripStartAndEndQuotes "${line::len-1}" # Remove last character
}
while read line; do
getSubmoduleNames "$line"
done < <(cat .gitmodules | grep "\[submodule.*\]" | cut -d ' ' -f 2-)
假设你目前已经注册了,但还没有签出子模块,如下:
$ 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版
如果没有任何.gitmodules文件,但在.git/modules/中存在子模块配置:
find .git/modules/ -name config -exec grep url {} \;