19

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?

ripper234
  • 222,824
  • 274
  • 634
  • 905

5 Answers5

27

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.

Eddie Groves
  • 33,851
  • 14
  • 47
  • 48
11
  • […]
  • 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.

poke
  • 369,085
  • 72
  • 557
  • 602
  • @manojlds I hope not… :/ If the pattern does not contain a slash it will match against the pathname, but if it does contain a slash, it will no longer do that and will glob directly. – poke Nov 16 '11 at 18:30
  • yup that's backwards. no slash = glob matching. slash = no glob matching. glob means "* can match a slash" – Jared Forsyth Aug 23 '13 at 16:44
  • NB: this is no longer true for recent versions of git; refer to the answer below for the updated answer https://stackoverflow.com/a/19557737/3064736 is – Maximilian Jan 30 '21 at 00:05
5

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
manojlds
  • 290,304
  • 63
  • 469
  • 417
2

try this

/**/.idea/workspace.xml

burning_LEGION
  • 13,246
  • 8
  • 40
  • 52
franci
  • 21
  • 1
0

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)

DanR
  • 322
  • 2
  • 3