bash 如何从 git ls-files 中排除文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36753573/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:32:02  来源:igfitidea点击:

How do I exclude files from git ls-files?

gitbash

提问by Nate

How do I list everything except markdown files? I have tried, as show in the code below, to run ls-files with the --exclude flag, but the excluded files are still shown in the output.

如何列出除 Markdown 文件之外的所有内容?我已经尝试过,如下面的代码所示,使用 --exclude 标志运行 ls-files,但排除的文件仍然显示在输出中。

My git version is 2.6.4 (Apple Git-63)

我的 git 版本是 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

回答by geirha

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 有自己的方法来扩展 glob 模式,其中包括一种排除匹配一个模式但不匹配另一个模式的文件的方法。例如,以下将列出所有不以 .md 结尾的路径:

git ls-files -- . ':!:*.md'

It's important to quote the pattern, because you want git to parse it, not the shell.

引用模式很重要,因为您希望 git 解析它,而不是 shell。

And to match all files ending with .js, except the ones ending with .min.js:

并匹配所有以 结尾的文件.js,但以 结尾的文件除外.min.js

git ls-files -- '*.js' ':!:*.min.js'

You can also use this with other commands, such as git grep:

您还可以将其与其他命令一起使用,例如 git grep:

git grep -w funcname -- '*.js' ':!:*.min.js'

This syntax is explained under pathspec in gitglossary(or git help glossaryor man gitglossary)

此语法在 gitglossary(或git help glossaryor man gitglossary中的 pathspec进行了解释

回答by VonC

That was already discussed in 2010:

已经在 2010 年讨论过

There is no indication in the man page that -x doesn't apply to -c.

手册页中没有说明 -x 不适用于 -c。

Hence the addition:

因此添加:

Since b5227d8, -x/--excludedoes not apply to cached files.
This is easy to miss unless you read the discussion in the EXCLUDE PATTERNSsection. Clarify that the option applies to untracked files and direct the reader to EXCLUDE PATTERNS.

由于b5227d8-x/--exclude不适用于缓存文件。
除非您阅读本EXCLUDE PATTERNS节中的讨论,否则很容易错过这一点。阐明该选项适用于未跟踪的文件并将读者引导至EXCLUDE PATTERNS.

The git ls-filesman pagedoes mentions:

git ls-files手册页确实提到:

-x <pattern>
--exclude=<pattern>

Skip untrackedfiles matching pattern

跳过未跟踪的文件匹配模式

If your Readme.mdis tracked, the exclude pattern won't apply.

如果您Readme.md被跟踪,则排除模式将不适用。

回答by Batiaev

You do not need to use the --exclude parameter because this key is only used for skipping untracked files:

您不需要使用 --exclude 参数,因为此键仅用于跳过未跟踪的文件:

$ man git-ls-files

-x <pattern>, --exclude=<pattern>
       Skip untracked files matching pattern. Note that pattern is a shell wildcard pattern. See EXCLUDE PATTERNS below for more information.

You should just use a mask for required files

您应该只对所需文件使用掩码

In your case (excluding *.md files):

在您的情况下(不包括 *.md 文件):

$ git ls-files
example1.txt
example2.pdf
readme.md

$ git ls-files *[^.md]
example1.txt
example2.pdf