如何列出被git忽略的文件,这些文件当前正在执行或提交? [英] How to list files ignored by git that are currently staged or committed?

查看:187
本文介绍了如何列出被git忽略的文件,这些文件当前正在执行或提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

ls-files 的文档并不完全清楚,但似乎下面的简单别名完成了这项工作:

  git config --global alias.showtrackedignoredls-files -i --exclude-standard

以上命令创建一个名为 showtrackedignored 的别名。要使用,请运行:

  git showtrackedignored 

,它将列出当前目录中的所有文件以及被跟踪的子目录,但如果未跟踪它们将被忽略。



git ls-files

中的错误不幸的是,这不能100%可靠地工作。显然,Git在查找应该忽略 的文件方面做得很好,但是当搜索 的文件被忽略时( -i 选项添加到 git ls-files ),它不会在目录内列出被忽略的文件,如果它是匹配忽略规则的目录。



要解决这个问题,请尝试转换您的忽略规则,以便只匹配文件,而不是目录(这不总是可行)。



(感谢克里斯托夫发现此错误和将它报告给Git邮件列表编辑:一个补丁程序现在正在开发中,并且会将其转化为git 1.7.11.2或更高版本)



另一种方法



这是一种不同的方法。它要复杂得多,可能会破坏角落案例。

  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}'

别名具有以下功能:




  • cd 回到 git showtrackedignored 运行于(Git从目录目录运行基于shell的别名,而不是当前目录;请参阅<$ c $中的别名。* 部分c> git help config

  • 定义一个名为 untracked_list 的变量。该变量保存临时文件的路径,该文件将包含当前被忽略的文件的列表。此临时文件位于 .git 目录中。

  • 将忽略的文件列表写入 $ {untracked_list }

  • 告诉Git如同索引为空并列出所有被忽略的文件。

  • 输出到 grep ,它过滤掉写入 $ {untracked_list} 的文件。

  • 删除临时文件 $ {untracked_list}



这种方法的缺点:$ b​​
$ b


  • 它会在 .git 目录中创建一个临时文件。
  • 它假定你有一个POSIX兼容的shell。

  • 它假定你有一个POSIX兼容的实现 grep



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


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?

解决方案

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

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

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

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

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

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 Christoph for 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)

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:

  • cd back to the directory where git showtrackedignored was 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 .git directory.
  • 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}.

Drawbacks to this approach:

  • It creates a temporary file in your .git directory.
  • It assumes you have a POSIX-compatible shell.
  • It assumes you have a POSIX-compatible implementation of grep.

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

这篇关于如何列出被git忽略的文件,这些文件当前正在执行或提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆