ripper234
ripper234

Reputation: 230376

Why doesn't gitignore work in this case?

I have two files I wish to ignore:

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?

Upvotes: 20

Views: 17500

Answers (5)

Eddie Groves
Eddie Groves

Reputation: 34888

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.

Upvotes: 28

franci
franci

Reputation: 21

try this

/**/.idea/workspace.xml

Upvotes: 2

manojlds
manojlds

Reputation: 301587

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

Upvotes: 5

DanR
DanR

Reputation: 322

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)

Upvotes: 0

poke
poke

Reputation: 388383

  • […]
  • 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.

Upvotes: 11

Related Questions