如何从git ls-files中排除文件? [英] How do I exclude files from git ls-files?
问题描述
如何列出除markdown文件以外的所有内容?我尝试使用--exclude标志运行ls文件,但是排除的文件仍显示在输出中.
How do I list everything except markdown files? I have tried to run ls-files with the --exclude flag, but the excluded files are still shown in the output.
我的git版本是2.6.4(Apple Git-63)
My git version is 2.6.4 (Apple Git-63)
$ git ls-files
ChromeExt/read-coffee
Node/README.md
Node/web-scraping
README.md
$ git ls-files --exclude *.md
ChromeExt/read-coffee
Node/README.md
Node/web-scraping
README.md
推荐答案
Git有自己的扩展glob模式的方法,其中包括一种排除与一种模式匹配但与另一种模式不匹配的文件的方法.例如,以下内容将列出所有不以.md结尾的路径:
Git has its own way to extend glob patterns, and that includes a way to exclude files that match one pattern but not another. For example, the following will list all paths not ending in .md:
git ls-files -- . ':!:*.md'
引用模式很重要,因为您希望git解析它,而不是shell.
It's important to quote the pattern, because you want git to parse it, not the shell.
并匹配所有以.js
结尾的文件,但以.min.js
结尾的文件除外:
And to match all files ending with .js
, except the ones ending with .min.js
:
git ls-files -- '*.js' ':!:*.min.js'
您还可以将其与其他命令一起使用,例如git grep:
You can also use this with other commands, such as git grep:
git grep -w funcname -- '*.js' ':!:*.min.js'
此语法在 gitglossary中的路径规范中解释(或git help glossary
或man gitglossary
)
This syntax is explained under pathspec in gitglossary (or git help glossary
or man gitglossary
)
这篇关于如何从git ls-files中排除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!