Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何可靠地检查文件是否被git忽略?_Git_Bash_Gitignore - Fatal编程技术网

如何可靠地检查文件是否被git忽略?

如何可靠地检查文件是否被git忽略?,git,bash,gitignore,Git,Bash,Gitignore,我遇到了一个问题,git check ignore不遵守.gitignore的“not”(!)规则。如果文件与.gitignore文件中的任何条目匹配,则无论该条目是否告诉它不要忽略路径(通过!操作符),它都会返回“忽略” 按如下说明返回代码映射: .gitignore files/ path-not-ignored ignored-files/ path-ignored path-not-ignored 退出状态 0:忽略一个或多个提供的路径 1:提供的任何路径都不

我遇到了一个问题,
git check ignore
不遵守
.gitignore
的“not”(
)规则。如果文件与
.gitignore
文件中的任何条目匹配,则无论该条目是否告诉它不要忽略路径(通过
操作符),它都会返回“忽略”

按如下说明返回代码映射:

.gitignore
files/
    path-not-ignored
ignored-files/
    path-ignored
    path-not-ignored
退出状态

  • 0:忽略一个或多个提供的路径
  • 1:提供的任何路径都不会被忽略
  • 128:遇到致命错误
这与我在添加“请勿忽略”规则时看到的不匹配,即粘贴
在条目前面

例如,让我们创建一个文件结构,如下所示:

.gitignore
files/
    path-not-ignored
ignored-files/
    path-ignored
    path-not-ignored
和一个
.gitignore
包含:

ignored-files/*
!ignored-files/path-not-ignored
根据文档,我希望
files/
ignored files/path not ignored
中的文件返回退出状态1(未忽略),而
ignored files/path is ignored
中的文件返回0(忽略)

我看到的内容,而不是
忽略文件中的所有内容
返回退出状态0(忽略):

(正在使用git check ignore;echo$?
检查退出状态代码)

这与Git的内部行为不匹配,因为我可以
Git添加被忽略的文件/path not ignored
很好:

File                             | `git add`?                   | Matches `git check-ignore` output?
-------------------------------- | ---------------------------- | ----------------------------------
`files/path-not-ignored`         | Adds file (as expected)      | Yes
`ignored-files/path-ignored`     | Throws warning (as expected) | Yes
`ignored-files/path-not-ignored` | Adds file (as expected)      | No


git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   files/path-not-ignored
    new file:   ignored-files/path-not-ignored

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    .gitignore
File |`git add`?|匹配'git check ignore'输出?
-------------------------------- | ---------------------------- | ----------------------------------
`文件/路径未被忽略`|添加文件(如预期)|是
`忽略文件/路径忽略`|抛出警告(如预期)|是
`忽略的文件/未忽略的路径`|添加文件(如预期)|否
git状态
论分行行长
初始提交
要提交的更改:
(使用“git rm--cached…”取消存储)
新文件:未忽略文件/路径
新文件:忽略的文件/未忽略的路径
未跟踪的文件:
(使用“git add…”包含在将提交的内容中)
.gitignore

在这种情况下,
git-check-ignore
是不可靠的,我是否可以使用其他命令来检查git是否忽略/不忽略给定的文件


返回退出代码的东西是理想的,因为我在bash脚本中使用了它。

用Git 2.18(2018年7月)检查问题是否自2017年7月以来一直存在

可能有助于消除假阳性

检查忽略
:修复目录和其他文件类型的混合 在
check_ignore()
中,第一个pathspec项确定任何 后面的那些。
这意味着匹配常规文件的pathspec可以阻止以下pathspec匹配目录,这毫无意义


一个选项可能是使用
git add--dry run
。不幸的是Git有这个bug。。。看起来有一些代码应该处理否定,但我打赌它不能正确处理globbing.Hmmm。。。查看手册页,我不确定它在主题(“调试gitignore/exclude文件”)下是否表现糟糕。OTOH,考虑到记录的返回值,我认为它表现得很糟糕。我将在git列表中查询这个问题。看起来这是一个问题,尽管调用退出状态可能需要-q选项。如果忽略顺序很重要,-v-n选项可以帮助调试,并且可以提供多个路径来划分案例。git repo中的git check ignore测试都在(全部845行)中。Junio回应说,他认为check ignore一开始是一种(而不是一种检查文件是否被忽略的方法)。因此,以这种方式使用它并不是它的初衷。如果您愿意解析<代码> Git状态< /代码>的输出,您可以考虑使用<代码> Git状态——忽略——瓷器< /代码>,查找<代码>!代码>在行的开头。不确定这将如何与目录一起工作。