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

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

Explain which gitignore rule is ignoring my file

gitgitignore

提问by Carlos Campderrós

Is there any way to see why some file is getting ignored by git (i.e. which rule in a .gitignorefile is causing the file to be ignored)?

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

Imagine I have this (or a much more complex scenario, with hundreds of folders and tens of .gitignorefiles:

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

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

If I run git add folder/subfolder/file.txtgit may complain of it being ignored:

如果我运行git add folder/subfolder/file.txtgit 可能会抱怨它被忽略了:

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 .gitignorehave a rule to ignore this file, and also show the rule? Like:

有什么方法可以知道所有可能.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.

Or just:

要不就:

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

回答by Adam Spiers

git check-ignore -v filename

See the man pagefor 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.

git 目前不提供这样的东西。但是在看到您的问题后,我进行了一些谷歌搜索,发现早在 2009 年就要求并部分实施了此功能。阅读该线程后,我意识到正确地完成它不会有太多工作,所以我已经开始制作补丁并希望在接下来的一两天内完成它。当它准备好时,我会更新这个答案。

UPDATE:Wow, that was a lot harder than I expected. The innards of git's exclude handling are quite cryptic. Anyway, here's an almostfinished series of commitswhich apply to today's upstream masterbranch. The test suite is 99% complete, but I haven't finished handling of the --stdinoption yet. Hopefully I'll manage that this weekend, and then submit my patches to the git mailing list.

更新:哇,这比我预期的要困难得多。的内部结构git的处理排除是相当神秘。总之,这里是一个几乎完成一系列提交其适用于今天的上游master分支。测试套件已完成 99%,但我还没有完成对--stdin选项的处理。希望这个周末我能解决这个问题,然后将我的补丁提交到 git 邮件列表。

In the meantime, I'd definitely welcome testing from anyone who's able to do so - just clone from my gitfork, check out the check-ignorebranch, and compile it as normal.

与此同时,我绝对欢迎任何有能力的人进行测试 - 只需从我的gitfork克隆,检查check-ignore分支,然后像往常一样编译它。

UPDATE 2:It's done! Latest version is on github as per above, and I have submitted the patch series to the git mailing listfor peer review. Let's see what they think ...

更新2:完成了!最新版本如上在 github 上,我已将补丁系列提交到 git 邮件列表以供同行评审。让我们看看他们是怎么想的……

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 masterbranch, and will be available in the next release (1.8.2, expected 8th March 2013). Here's the check-ignoremanual page. Phew, that was way more work than I expected!

更新 3:经过几个月的黑客/补丁/讨论/等待,我很高兴能够说这个功能现在已经到达 git 的master分支,并将在下一个版本中可用(1.8.2,预计第 8 2013 年 3 月)。这是check-ignore手册页。呼,这比我预期的要多得多!

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.

更新 4:如果您对有关此答案如何演变以及该功能如何实施的完整故事感兴趣,请查看GitMinutes 播客的第 32 集

回答by VonC

Update git 2.8 (March 2016):

更新 git 2.8(2016 年 3 月):

GIT_TRACE_EXCLUDE=1 git status

See "A way to validate .gitignorefile"

请参阅“一种验证.gitignore文件的方法

That is complementary to the git check-ignore -vdescribed below.

这是对git check-ignore -v下面描述的补充。



Original answer: Sept 2013 (git 1.8.2, then 1.8.5+):

原始答案:2013 年 9 月(git 1.8.2,然后是 1.8.5+):

git check-ignoreimproves again in git 1.8.5/1.9 (Q4 2013):

git check-ignoregit 1.8.5/1.9 (Q4 2013) 中再次改进:

"git check-ignore" follows the same rule as "git add" and "git status" in that the ignore/exclude mechanism does not take effect on paths that are already tracked.
With "--no-index" option, it can be used to diagnose which paths that should have been ignored have been mistakenly added to the index.

git check-ignore”遵循与“ git add”和“ git status”相同的规则,因为忽略/排除机制不会对已跟踪的路径生效。
使用“ --no-index”选项,它可用于诊断哪些应该被忽略的路径被错误地添加到索引中

See commit 8231fa6from https://github.com/flashydave:

https://github.com/flashydave查看提交 8231fa6

check-ignorecurrently shows how .gitignorerules would treat untracked paths. Tracked paths do not generate useful output.
This prevents debugging of why a path became tracked unexpectedly unless that path is first removed from the index with git rm --cached <path>.

The option --no-indextells the command to bypass the check for the path being in the index and hence allows tracked paths to be checked too.

Whilst this behaviour deviates from the characteristics of git addand git statusits use case is unlikely to cause any user confusion.

Test scripts are augmented to check this option against the standard ignores to ensure correct behaviour.

check-ignore当前显示.gitignore规则将如何处理未跟踪的路径。跟踪路径不会产生有用的输出。
这可以防止调试路径被意外跟踪的原因,除非该路径首先从索引中删除git rm --cached <path>

该选项--no-index告诉命令绕过对索引中路径的检查,因此也允许检查跟踪的路径。

虽然这种行为从特征偏离git addgit status它的使用情况下是不会对身体造成用户混淆。

测试脚本被扩充以根据标准忽略检查此选项以确保正确的行为。



--no-index::

Don't look in the index when undertaking the checks.
This can be used:

  • to debug why a path became tracked by e.g. git add .and was not ignored by the rules as expected by the user or
  • when developing patterns including negation to match a path previously added with git add -f.

进行检查时不要查看索引。
这可以用于:

  • 调试为什么路径被 eg 跟踪git add .并且没有被用户预期的规则忽略或
  • 在开发包括否定在内的模式以匹配之前添加的路径时git add -f

回答by edoloughlin

I can't find anything in the man page but here's a quick and dirty script that will check your file in each parent directory to see if it can be git-add'ed. Run it in the directory containing the problem file as:

我在手册页中找不到任何内容,但这里有一个快速而肮脏的脚本,它将检查每个父目录中的文件以查看它是否可以被 git-add'ed。在包含问题文件的目录中运行它:

test-add.sh STOP_DIR FILENAME

where STOP_DIRis the top level directory of the Git project and FILENAMEis the problem file name (without a path). It creates an empty file of the same name at each level of the hierarchy (if it doesn't exist) and tries a git add -nto see if it can be added (it cleans up after itself). It outputs something like:

其中STOP_DIR是 Git 项目的顶级目录,FILENAME是问题文件名(不带路径)。它在层次结构的每个级别创建一个同名的空文件(如果它不存在)并尝试git add -n查看是否可以添加它(它会自行清理)。它输出类似:

FAILED:    /dir/1/2/3
SUCCEEDED: /dir/1/2

The script:

剧本:

#!/usr/bin/env bash
TOP=
FILE=
DIR=`pwd`
while : ; do
  TMPFILE=1
  F=$DIR/$FILE
  if [ ! -f $F ]; then
    touch $F
    TMPFILE=0
  fi
  git add -n $F >/dev/null 2>&1
  if [ $? = 0 ]; then
    echo "SUCCEEDED: $DIR"
  else
    echo "FAILED:    $DIR"
  fi
  if [ $TMPFILE = 0 ]; then
    rm $F
  fi
  DIR=${DIR%/*}
  if [ "$DIR" \< "$TOP" ]; then
    break
  fi
done 

回答by rekordboy

To add to the main answer of using git check-ignore -v filename(thanks BTW) I found that my .gitignore file was blocking everything because there was a newline after a wildcard, so I had:

添加到使用的主要答案git check-ignore -v filename(感谢顺便说一句)我发现我的 .gitignore 文件阻止了所有内容,因为通配符后面有一个换行符,所以我有:

* .sublime-project

* .sublime-project

as an example. I just removed the newline, and voila! It was fixed.

举个例子。我刚刚删除了换行符,瞧!它是固定的。