如何列出当前暂存或提交的 git 忽略的文件?

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

How to list files ignored by git that are currently staged or committed?

git

提问by Hedgehog

How does one get a list of those files that match a rule in .gitignore file, but that have been staged or committed in the past?

如何获得与 .gitignore 文件中的规则匹配但过去已暂存或提交的那些文件的列表?

回答by Richard Hansen

The documentation to ls-filesis not exactly clearly written, but it appears that the following simple alias does the job:

to 的文档ls-files写得并不完全清楚,但似乎以下简单的别名可以完成这项工作:

git config --global alias.showtrackedignored "ls-files -i --exclude-standard"

The above command creates an alias called showtrackedignored. To use, run:

上面的命令创建了一个名为showtrackedignored. 要使用,请运行:

git showtrackedignored

and it will list all of the files in the current directory and subdirectories that are tracked but would be ignored if they weren't tracked.

并且它会列出当前目录和子目录中所有被跟踪但如果没有被跟踪的文件将被忽略。

Bug in git ls-files

错误 git ls-files

Unfortunately, this doesn't work 100% reliably. Apparently Git does a good job of finding files that should notbe ignored, but when searching for files that areignored (the -ioption to git ls-files), it doesn't list ignored files inside a directory if it's the directory that matches the ignore rules.

不幸的是,这并不能 100% 可靠地工作。显然Git的的确发现,应将文件的一个好工作被忽略,但搜索该文件时忽略(-i选项git ls-files),它的目录里面没有列表被忽略的文件,如果它是相匹配的忽略规则的目录。

To work around this bug, try converting your ignore rules so that only files are matched, not directories (this isn't always possible).

要解决此错误,请尝试转换您的忽略规则,以便只匹配文件,而不匹配目录(这并不总是可能的)。

(Thank you Christophfor discovering this bug and reporting it to the Git mailing list! Edit: A patch is in the works now and will make it into git 1.7.11.2 or later)

(感谢Christoph发现这个 bug 并将其报告给 Git 邮件列表!编辑:补丁正在开发中,将使其成为 git 1.7.11.2 或更高版本)

Alternative approach

替代方法

Here's a different approach. It's far more complicated and might have broken corner cases.

这是一种不同的方法。它要复杂得多,并且可能已经打破了极端情况。

git config --global alias.showtrackedignored '!
cd "${GIT_PREFIX}" &&
untracked_list=$(git rev-parse --git-dir)/ignored-untracked.txt &&
git ls-files -o -i --exclude-standard >"${untracked_list}" &&
GIT_INDEX_FILE="" git ls-files -o -i --exclude-standard | grep -Fvxf "${untracked_list}" &&
rm -rf "${untracked_list}"'

The alias does the following:

别名执行以下操作:

  • cdback to the directory where git showtrackedignoredwas run from (Git runs shell-based aliases from the toplevel directory, not the current directory; see the section on alias.*in git help config)
  • Define a variable called untracked_list. This variable holds the path to a temporary file that will contain the list of currently ignored files. This temporary file is in the .gitdirectory.
  • Write the list of ignored files to ${untracked_list}.
  • Tell Git to act as if the index is empty and list all the ignored files.
  • Pipe that output to grep, which filters out the files that were written to ${untracked_list}.
  • Delete the temporary file ${untracked_list}.
  • cd回到其中目录git showtrackedignored是从运行(GIT运行从顶层目录,而不是当前目录基于壳别名;见节上alias.*git help config
  • 定义一个名为 的变量untracked_list。此变量保存临时文件的路径,该文件将包含当前忽略的文件列表。此临时文件位于.git目录中。
  • 将忽略的文件列表写入${untracked_list}.
  • 告诉 Git 将索引视为空并列出所有被忽略的文件。
  • 将该输出通过管道传送到grep,从而过滤掉写入的文件${untracked_list}
  • 删除临时文件${untracked_list}

Drawbacks to this approach:

这种方法的缺点:

  • It creates a temporary file in your .gitdirectory.
  • It assumes you have a POSIX-compatible shell.
  • It assumes you have a POSIX-compatible implementation of grep.
  • 它会在您的.git目录中创建一个临时文件。
  • 它假设您有一个兼容 POSIX 的 shell。
  • 它假设您有 POSIX 兼容的grep.

It also suffers from the same bug as the former alias.

它也遭受与前一个别名相同的错误。

回答by Timon de Groot

Just going to leave this one here, based on Richard's answer:

根据理查德的回答,就将这个留在这里:

git ls-files -i --exclude-standard | xargs git rm --cached

git ls-files -i --exclude-standard | xargs git rm --cached

This will delete every tracked file that is ignored by .gitignore.

这将删除被忽略的每个跟踪文件.gitignore

回答by seth

This question was answered well by https://stackoverflow.com/a/467053. Basically, git clean -ndX will tell you what can be safely ignored.

https://stackoverflow.com/a/467053很好地回答了这个问题。基本上, git clean -ndX 会告诉你什么可以安全地忽略。