Why doesn't gitignore work in this case?

2020-02-05 01:55发布

I have two files I wish to ignore:

  • .idea/workspace.xml
  • someapp/src/.idea/workspace.xml

I thought adding this single rule to .gitignore will suffice:

.idea/workspace.xml

But it only catches the top-level .idea/workspace.xml (git status shows someapp/src/.idea/workspace.xml as untracked).

I also tried **/.idea/workspace.xml, but this doesn't work at all. Help?

标签: git gitignore
5条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-05 02:18

According to the gitignore man page:

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.

(emphasis mine)

查看更多
3楼-- · 2020-02-05 02:20

try this

/**/.idea/workspace.xml

查看更多
ら.Afraid
4楼-- · 2020-02-05 02:33
  • […]
  • 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. […]

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.xml but only for workspace.xml.

Git manual: gitignore.

查看更多
成全新的幸福
5楼-- · 2020-02-05 02:41

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.

**/.idea/workspace.xml should work now in this example.

查看更多
Emotional °昔
6楼-- · 2020-02-05 02:43

See the examples in gitignore manual:

"Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html"

So .idea/workspace.xml will match the root one but not someapp/src/.idea/workspace.xml

But depending on your fnmatch implementation, this is what you need in your .gitignore:

.idea/workspace.xml
**/.idea/workspace.xml
查看更多
登录 后发表回答