解释哪个 gitignore 规则忽略了我的文件 [英] Explain which gitignore rule is ignoring my file
问题描述
有什么办法可以看出为什么某些文件会被 git 忽略(即 .gitignore
文件中的哪个规则导致文件被忽略)?
想象一下我有这个(或者更复杂的场景,有数百个文件夹和数十个 .gitignore
文件:
如果我运行 git add folder/subfolder/file.txt
git 可能会抱怨它被忽略了:
以下路径将被您的 .gitignore 文件之一忽略:文件夹/子文件夹/file.txt如果您真的想添加它们,请使用 -f.
有什么方法可以知道所有可能的 .gitignore
中哪些有忽略此文件的规则,并显示规则?喜欢:
您的文件夹/.gitignore 文件将忽略以下路径(第 12 行:*.txt)文件夹/子文件夹/file.txt如果您真的想添加它们,请使用 -f.
或者只是:
$ git 为什么忽略文件夹/子文件夹/file.txt文件夹/.gitignore:12:*.txt
git check-ignore -v filename
有关详细信息,请参阅手册页.
原答案如下:
git 目前不提供这样的东西.但在看到你的问题后,我做了一些谷歌搜索,发现早在 2009 年 请求并部分实现了此功能.阅读该线程后,我意识到正确地完成它不会有太多工作,因此我已经开始制作补丁并希望在接下来的一两天内完成.准备好后,我会更新此答案.
更新:哇,这比我预期的要困难得多.git
的排除处理的内部结构非常神秘.无论如何,这是几乎完成的一系列提交应用到今天的上游 master
分支.测试套件已完成 99%,但我还没有完成对 --stdin
选项的处理.希望这个周末我能解决这个问题,然后将我的补丁提交到 git 邮件列表.
与此同时,我绝对欢迎任何有能力的人进行测试 - 只需从 我的 git
fork,查看check-ignore
分支,正常编译
更新 2: 大功告成!上面的最新版本在 github 上,我有 将补丁系列提交到 git 邮件列表 以供同行评审.让我们看看他们的想法......
更新 3: 经过几个月的黑客攻击/补丁审查/讨论/等待,我很高兴能够说此功能现已到达git的master
分支,并将在下一个版本(1.8.2,预计在8th)可用2013).这是check-ignore
手册页.呼,这比我预期的要多得多!
更新 4:如果您对有关此答案如何演变以及该功能如何实施的完整故事感兴趣,请查看 GitMinutes 播客的第 32 集.
Is there any way to see why some file is getting ignored by git (i.e. which rule in a .gitignore
file is causing the file to be ignored)?
Imagine I have this (or a much more complex scenario, with hundreds of folders and tens of .gitignore
files:
/
-.gitignore
-folder/
-.gitignore
-subfolder/
-.gitignore
-file.txt
If I run git add folder/subfolder/file.txt
git may complain of it being ignored:
The following paths are ignored by one of your .gitignore files:
folder/subfolder/file.txt
Use -f if you really want to add them.
Is there any way to know which of all the possible .gitignore
have a rule to ignore this file, and also show the rule? Like:
The following paths are ignored by your folder/.gitignore file (line 12: *.txt)
folder/subfolder/file.txt
Use -f if you really want to add them.
Or just:
$ git why-is-ignored folder/subfolder/file.txt
folder/.gitignore:12:*.txt
git check-ignore -v filename
See the man page for more details.
Original answer follows:
git does not currently provide anything like this. But after seeing your question I did some googling and found that back in 2009 this feature was requested and partially implemented. After reading the thread, I realised it would not be too much work to do it properly, so I have started work on a patch and hope to have it finished in the next day or two. I will update this answer when it is ready.
UPDATE: Wow, that was a lot harder than I expected. The innards of git
's exclude handling are quite cryptic. Anyway, here's an almost finished series of commits which apply to today's upstream master
branch. The test suite is 99% complete, but I haven't finished handling of the --stdin
option yet. Hopefully I'll manage that this weekend, and then submit my patches to the git mailing list.
In the meantime, I'd definitely welcome testing from anyone who's able to do so - just clone from my git
fork, check out the check-ignore
branch, and compile it as normal.
UPDATE 2: It's done! Latest version is on github as per above, and I have submitted the patch series to the git mailing list for peer review. Let's see what they think ...
UPDATE 3: After several more months of hacking / patch reviews / discussions / waiting, I'm delighted to be able to say that this feature has now reached git's master
branch, and will be available in the next release (1.8.2, expected 8th March 2013). Here's the check-ignore
manual page. Phew, that was way more work than I expected!
UPDATE 4: If you're interested in the full story about how this answer evolved and the feature came to be implemented, check out episode #32 of the GitMinutes podcast.
这篇关于解释哪个 gitignore 规则忽略了我的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!