用于显示 .gitignore 忽略哪些特定文件的 Git 命令 [英] Git command to show which specific files are ignored by .gitignore

查看:32
本文介绍了用于显示 .gitignore 忽略哪些特定文件的 Git 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Git 感到厌烦并遇到以下问题:

我的项目源码树:

<预><代码>/|+--src/+----参考/+----...|+--供应商/+----...

我的供应商分支中有代码(当前是 MEF),我将在那里编译,然后将引用移动到 /src/refs 中,这是项目从中获取它们的地方.

我的问题是我的 .gitignore 设置为忽略 *.dll*.pdb.我可以做一个 git add -f bar.dll 来强制添加被忽略的文件,这没问题,问题是我不知道列出哪些文件存在被忽略.

我想列出被忽略的文件,以确保我不会忘记添加它们.

我已阅读 git ls-files 上的手册页,但无法使其工作.在我看来 git ls-files --exclude-standard -i 应该做我想做的.我错过了什么?

解决方案

注意事项:


也很有趣(在 qwertymkanswer),您也可以使用git check-ignore -v 命令,至少在 Unix 上(在 CMD Windows 会话中不起作用)

git check-ignore *git check-ignore -v *

第二个显示 .gitignore 的实际规则,它使您的 git 存储库中的文件被忽略.
在 Unix 上,使用扩展到当前目录中的所有文件递归?"和一个 bash4+:

git check-ignore **/*

(或一个 find -exec 命令)

注意:https://stackoverflow.com/users/351947/Rafi B. 建议 在评论中避免(有风险的)全球明星:

git check-ignore -v $(find .-type f -print)

确保从 .git/ 子文件夹中排除文件.

CervEd注释,以避免.git/:

查找.-not -path './.git/*' |git check-ignore --stdin


原答案 42009)

git ls-files -i

应该可以工作,除非其源代码表明:

if (show_ignored && !exc_given) {fprintf(stderr, "%s: --ignored 需要一些排除模式
",argv[0]);

exc_given ?

事实证明,它需要在 -i 之后再添加一个参数才能真正列出任何内容:

试试:

git ls-files -i --exclude-from=[Path_To_Your_Global].gitignore

(但这只会列出您的缓存(非忽略)对象,并带有过滤器,因此这不是您想要的)


示例:

$ cat .git/ignore# 忽略树中任何位置的对象和档案.*.[oa]$ cat 文档/.gitignore# 忽略生成的 html 文件,*.html# 除了手动维护的 foo.html!foo.html$ git ls-files --ignored --exclude='文档/*.[0-9]' --exclude-from=.git/ignore --exclude-per-directory=.gitignore


实际上,在我的gitignore"文件(称为exclude")中,我找到了一个可以帮助您的命令行:

F:proggit	est.gitinfo>type exclude# git ls-files --others --exclude-from=.git/info/exclude# 以#"开头的行是注释.# 对于主要使用 C 语言的项目,以下将是一组很好的# 排除模式(如果你想使用它们,取消注释它们):# *.[oa]#*~

所以....

git ls-files --ignored --exclude-from=.git/info/excludegit ls-files -i --exclude-from=.git/info/excludegit ls-files --others --ignored --exclude-standardgit ls-files -o -i --exclude-standard

应该可以解决问题.

(感谢 honzajde 指出 在评论中 git ls-files -o -i--exclude-from... 确实包括缓存文件:只有 git ls-files -i --exclude-from... (没有 -o) 可以.)

ls-files man 中所述page, --others 是重要的部分,为了向你展示未缓存、未提交、通常被忽略的文件.

--exclude_standard 不仅仅是一种快捷方式,而是一种包含所有标准忽略模式"的方法.设置.

<块引用>

exclude-standard
在每个目录中添加标准的 git 排除:.git/info/exclude.gitignore,以及用户的全局排除文件.

I am getting my feet wet with Git and have the following issue:

My project source tree:

/
|
+--src/
+----refs/
+----...
|
+--vendor/
+----...

I have code (currently MEF) in my vendor branch that I will compile there and then move the references into /src/refs which is where the project picks them up from.

My issue is that I have my .gitignore set to ignore *.dll and *.pdb. I can do a git add -f bar.dll to force the addition of the ignored file which is ok, the problem is I can not figure out to list what files exist that are ignored.

I want to list the ignored files to make sure that I don't forget to add them.

I have read the man page on git ls-files and can not make it work. It seems to me that git ls-files --exclude-standard -i should do what I want. What am I missing?

解决方案

Notes:


Also interesting (mentioned in qwertymk's answer), you can also use the git check-ignore -v command, at least on Unix (doesn't work in a CMD Windows session)

git check-ignore *
git check-ignore -v *

The second one displays the actual rule of the .gitignore which makes a file to be ignored in your git repo.
On Unix, using "What expands to all files in current directory recursively?" and a bash4+:

git check-ignore **/*

(or a find -exec command)

Note: https://stackoverflow.com/users/351947/Rafi B. suggests in the comments to avoid the (risky) globstar:

git check-ignore -v $(find . -type f -print)

Make sure to exclude the files from the .git/ subfolder though.

CervEd suggests in the comments, to avoid .git/:

find . -not -path './.git/*' | git check-ignore --stdin


Original answer 42009)

git ls-files -i

should work, except its source code indicates:

if (show_ignored && !exc_given) {
                fprintf(stderr, "%s: --ignored needs some exclude pattern
",
                        argv[0]);

exc_given ?

It turns out it need one more parameter after the -i to actually list anything:

Try:

git ls-files -i --exclude-from=[Path_To_Your_Global].gitignore

(but that would only list your cached (non-ignored) object, with a filter, so that is not quite what you want)


Example:

$ cat .git/ignore
# ignore objects and archives, anywhere in the tree.
*.[oa]
$ cat Documentation/.gitignore
# ignore generated html files,
*.html
# except foo.html which is maintained by hand
!foo.html
$ git ls-files --ignored 
    --exclude='Documentation/*.[0-9]' 
    --exclude-from=.git/ignore 
    --exclude-per-directory=.gitignore


Actually, in my 'gitignore' file (called 'exclude'), I find a command line that could help you:

F:proggit	est.gitinfo>type exclude
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~

So....

git ls-files --ignored --exclude-from=.git/info/exclude
git ls-files -i --exclude-from=.git/info/exclude

git ls-files --others --ignored --exclude-standard
git ls-files -o -i --exclude-standard

should do the trick.

(Thanks to honzajde pointing out in the comments that git ls-files -o -i --exclude-from... does not include cached files: only git ls-files -i --exclude-from... (without -o) does.)

As mentioned in the ls-files man page, --others is the important part, in order to show you non-cached, non-committed, normally-ignored files.

--exclude_standard is not just a shortcut, but a way to include all standard "ignored patterns" settings.

exclude-standard
Add the standard git exclusions: .git/info/exclude, .gitignore in each directory, and the user's global exclusion file.

这篇关于用于显示 .gitignore 忽略哪些特定文件的 Git 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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