5

I'm trying to configure a .gitignore file such that all files with a certain extension are ignored, except where they appear within a specific folder, or any sub folder of that folder. I've tried the following which does not work:

*.lib
!Libraries/

A second question I have is, do negated exclusions only apply to the immediately preceding rule, or do every rule you've defined up to that point?-

This almost answers my question, but doesn't help for sub folders.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72

2 Answers2

6

I think what you meant is ignore all *.lib except when they're inside /Libraries, try this out:

# The root directory's .gitignore
*.lib
!/Libraries/*.lib

# The .gitignore under /Libraries directory
!*.lib

The .gitignore is parsed from top to bottom. Any rule can override prior rules.

Tuxdude
  • 47,485
  • 15
  • 109
  • 110
4

Try using a double asterisk, **. Something like this in your .gitignore might work:

*.lib
!/Libraries/**/*.lib

The double asterisk matches any combination of subdirectories. This answer explains how it works fairly well.

As for your second question, @Tuxdude is correct: rules will override other rules up to that point due to the way the .gitignore is parsed.

Community
  • 1
  • 1
adamdunson
  • 2,635
  • 1
  • 23
  • 27
  • 1
    I tried this one, but it only seems to track the .lib files directly under `/Libraries` but not if they're under any of the sub-directories of `/Libraries` – Tuxdude Mar 01 '13 at 06:16
  • A couple of notes: `**` matches *literally* any number of directories, including none, so the second line is redundant, and `**` parsing was added in git 1.8.2, which was early 2013. Testing shows this working for me? – Simon Buchan Dec 05 '13 at 05:51