git - 解释哪个 gitignore 规则忽略了我的文件

标签 git gitignore

有什么方法可以查看为什么某些文件被 git 忽略(即 .gitignore 文件中的哪个规则导致文件被忽略)?

想象一下我有这个(或更复杂的场景,有数百个文件夹和数十个 .gitignore 文件:

/
-.gitignore
-folder/
    -.gitignore
    -subfolder/
              -.gitignore
              -file.txt

如果我运行 git add folder/subfolder/file.txt git 可能会提示它被忽略了:

The following paths are ignored by one of your .gitignore files:
folder/subfolder/file.txt
Use -f if you really want to add them.

有没有办法知道所有可能的 .gitignore 中的哪一个有忽略这个文件的规则,并显示规则?喜欢:

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.

或者只是:

$ git why-is-ignored folder/subfolder/file.txt
folder/.gitignore:12:*.txt

最佳答案

git check-ignore -v filename

参见 the man page了解更多详情。

原回答如下:

git 目前不提供这样的东西。但在看到你的问题后,我做了一些谷歌搜索,发现早在 2009 年 this feature was requested and partially implemented .读完帖子后,我意识到正确地完成它并不会太多工作,所以我已经开始着手制作补丁,希望在接下来的一两天内完成。准备就绪后,我会更新此答案。

更新:哇,这比我预期的要难得多。 git 的排除处理的内部结构非常神秘。不管怎样,这里是an almost finished series of commits适用于今天的上游 master 分支。测试套件已完成 99%,但我还没有完成 --stdin 选项的处理。希望这个周末我能解决这个问题,然后将我的补丁提交到 git 邮件列表。

与此同时,我绝对欢迎任何能够这样做的人进行测试 - 只需从 my git fork 克隆即可。 , 检查 check-ignore 分支,并正常编译它。

更新 2: 完成了!最新版本在 github 上,我有 submitted the patch series to the git mailing list供同行评审。让我们看看他们是怎么想的......

更新 3: 经过几个月的黑客攻击/补丁审查/讨论/等待,我很高兴能够这样说 this feature has now reached git's master branch ,并将在下一版本(1.8.2,预计 2013 年 3 月 8 日)中可用。这是 check-ignore manual page .哇,这比我预期的要多得多!

更新 4:如果您对这个答案如何演变以及该功能如何实现的完整故事感兴趣,请查看 episode #32 of the GitMinutes podcast .

关于git - 解释哪个 gitignore 规则忽略了我的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12144633/

相关文章:

javascript - 赫斯基 5 .huskey gitignore

eclipse - 如何解决 Eclipse 中的脏索引?

GIT:从远程服务器推送到远程

git - 如何让 Git 忘记已跟踪但现在位于 .gitignore 中的文件?

git - gitignore 是否适用于以前版本化的文件?

git - 忽略的文件在“提交更改”窗口中仍然可见 - IntelliJ 13

git - 删除本地不存在的远程分支

ruby-on-rails - git push heroku master 权限被拒绝

git - 对错误放置的 .git 目录进行故障排除(无需提交)

gitignore 的 Git pull 问题