列出 Git 存储库中的子模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12641469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
List submodules in a Git repository
提问by tpg2114
I have a Git repository that has several submodules in it. How do I list the names of all the submodules after git submodule init
has been run?
我有一个 Git 存储库,其中包含多个子模块。运行后如何列出所有子模块的名称git submodule init
?
The git submodule foreach
command could echo the names of the submodule, but that only works once they have been checked out which has not happened after the init step. There are more steps in the chain that need to happen before they can be checked out, and I don't want to have to hard-wire names of submodules into the script.
该git submodule foreach
命令可以回显子模块的名称,但只有在它们被检出后才有效,这在 init 步骤之后没有发生。链中还有更多步骤需要在它们被检出之前发生,我不想将子模块的名称硬连接到脚本中。
So is there a Git command to get the names of all currently registered, but not yet checked out submodules?
那么是否有一个 Git 命令来获取所有当前注册但尚未签出的子模块的名称?
采纳答案by Ikke
You could use the same mechanism as git submodule init
uses itself, namely, look at .gitmodules
. This files enumerates each submodule path and the URL it refers to.
您可以使用与git submodule init
使用本身相同的机制,即查看.gitmodules
. 此文件枚举每个子模块路径及其引用的 URL。
For example, from root of repository, cat .gitmodules
will print contents to the screen (assuming you have cat
).
例如,从存储库的根目录,cat .gitmodules
会将内容打印到屏幕上(假设您有cat
)。
Because .gitmodule files have the Git configuration format, you can use git config to parse those files:
由于 .gitmodule 文件具有 Git 配置格式,因此您可以使用 git config 来解析这些文件:
git config --file .gitmodules --name-only --get-regexp path
Would show you all submodule entries, and with
将向您显示所有子模块条目,并使用
git config --file .gitmodules --get-regexp path | awk '{ print }'
you would only get the submodule path itself.
你只会得到子模块路径本身。
回答by Jon Koops
You can use git submodule status
or optionally git submodule status --recursive
if you want to show nested submodules.
如果要显示嵌套的子模块,可以使用git submodule status
或 可选git submodule status --recursive
。
From the Git documentation:
从 Git 文档:
Show the status of the submodules. This will print the SHA-1 of the currently checked out commit for each submodule, along with the submodule path and the output of git describe for the SHA-1. Each SHA-1 will be prefixed with - if the submodule is not initialized, + if the currently checked out submodule commit does not match the SHA-1 found in the index of the containing repository and U if the submodule has merge conflicts.
显示子模块的状态。这将打印每个子模块的当前检出提交的 SHA-1,以及子模块路径和 SHA-1 的 git describe 输出。每个 SHA-1 将带有前缀 - 如果子模块未初始化,+ 如果当前检出的子模块提交与包含存储库索引中的 SHA-1 不匹配,如果子模块有合并冲突,则为 U。
回答by mholm815
To return just the names of the registered submodules, you can use this command:
要仅返回已注册子模块的名称,您可以使用以下命令:
grep path .gitmodules | sed 's/.*= //'
Think of it as git submodule --list
which doesn't exist.
把它想象成git submodule --list
不存在的。
回答by A.T.
The following command will list the submodules:
以下命令将列出子模块:
git submodule--helper list
The output is something like this:
输出是这样的:
<mode> <sha1> <stage> <location>
Note: It requires Git 2.7.0 or above.
注意:它需要 Git 2.7.0 或更高版本。
回答by Sandeep Gill
Use:
用:
$ git submodule
It will list all the submodules in the specified Git repository.
它将列出指定 Git 存储库中的所有子模块。
回答by Robert Sj?dahl
I use this:
我用这个:
git config --list|egrep ^submodule
回答by Max Cantor
I noticed that the command provided in an answer to this question gave me the information I was looking for:
我注意到在这个问题的答案中提供的命令给了我我正在寻找的信息:
No submodule mapping found in .gitmodule for a path that's not a submodule
对于不是子模块的路径,在 .gitmodule 中找不到子模块映射
git ls-files --stage | grep 160000
回答by benesch
If you don't mind operating only on initialized submodules, you can use git submodule foreach
to avoid text parsing.
如果您不介意仅对初始化的子模块进行操作,则可以使用git submodule foreach
来避免文本解析。
git submodule foreach --quiet 'echo $name'
回答by fabceolin
You can use:
您可以使用:
git submodule | awk '{ print }'
回答by LTK
This worked for me:
这对我有用:
git ls-files --stage | grep ^160000
It is based on this great article: Understanding Git Submodules
它基于这篇很棒的文章:Understanding Git Submodules
It must read grep ^160000
.
它必须阅读grep ^160000
。