git 为什么 gitignore 在这种情况下不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8156299/
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
Why doesn't gitignore work in this case?
提问by ripper234
I have two files I wish to ignore:
我有两个文件我想忽略:
- .idea/workspace.xml
- someapp/src/.idea/workspace.xml
- .idea/workspace.xml
- someapp/src/.idea/workspace.xml
I thought adding this single rule to .gitignore will suffice:
我认为将这条规则添加到 .gitignore 就足够了:
.idea/workspace.xml
But it only catches the top-level .idea/workspace.xml (git status shows someapp/src/.idea/workspace.xml as untracked).
但它只捕获顶级 .idea/workspace.xml (git status 显示 someapp/src/.idea/workspace.xml 为未跟踪)。
I also tried **/.idea/workspace.xml, but this doesn't work at all. Help?
我也试过**/.idea/workspace.xml,但这根本不起作用。帮助?
采纳答案by poke
- […]
- If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).
- Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. […]
- […]
- 如果模式不包含斜杠 /,则 git 将其视为 shell glob 模式并检查与相对于 .gitignore 文件位置的路径名的匹配(如果不是来自 .gitignore 则相对于工作树的顶层)文件)。
- 否则,git 将模式视为适合 fnmatch(3) 使用的 shell glob,带有 FNM_PATHNAME 标志:模式中的通配符将不匹配路径名中的 /。[…]
As soon as the path contains a slash, Git will no longer check for a match in the path but instead will use the glob behaviour directly. As such, you cannot match for .idea/workspace.xmlbut only for workspace.xml.
一旦路径包含斜杠,Git 将不再检查路径中的匹配项,而是直接使用 glob 行为。因此,您不能匹配 for.idea/workspace.xml而只能匹配workspace.xml。
回答by eddiegroves
This has changed in git 1.8.4
Use of platform fnmatch(3) function (many places like pathspec matching, .gitignore and .gitattributes) have been replaced with wildmatch, allowing "foo/**/bar" to match "foo/bar", "foo/a/bar", etc.
平台 fnmatch(3) 函数的使用(很多地方比如 pathspec 匹配、.gitignore 和 .gitattributes)已被替换为 wildmatch,允许“foo/**/bar”匹配“foo/bar”、“foo/a/bar” “, 等等。
**/.idea/workspace.xmlshould work now in this example.
**/.idea/workspace.xml在这个例子中现在应该可以工作了。
回答by manojlds
See the examples in gitignore manual:
请参阅 gitignore 手册中的示例:
"Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html"
"Documentation/*.html" 匹配 "Documentation/git.html" 但不匹配 "Documentation/ppc/ppc.html" 或"tools/perf/Documentation/perf.html"
So .idea/workspace.xmlwill match the root one but not someapp/src/.idea/workspace.xml
所以.idea/workspace.xml将匹配根一但不匹配someapp/src/.idea/workspace.xml
But depending on your fnmatch implementation, this is what you need in your .gitignore:
但是根据您的 fnmatch 实现,这是您在 .gitignore 中需要的:
.idea/workspace.xml
**/.idea/workspace.xml
回答by franci
try this
尝试这个
/**/.idea/workspace.xml
/**/.idea/workspace.xml
回答by DanR
According to the gitignore man page:
根据 gitignore 手册页:
Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file. These patterns match relative to the location of the .gitignore file.A project normally includes such .gitignore files in its repository, containing patterns for files generated as part of the project build.
从与路径相同的目录或任何父目录中的 .gitignore 文件中读取模式,高级文件中的模式(直到工作树的顶层)被低级文件中的模式覆盖到目录包含文件。这些模式相对于 .gitignore 文件的位置匹配。项目通常在其存储库中包含此类 .gitignore 文件,其中包含作为项目构建的一部分生成的文件的模式。
(emphasis mine)
(强调我的)

