19

I'm trying to make a gitignore file that will ignore all .jar files unless they're in a folder called libs. Here's my basic file structure:

-.gitignore 
-libs/
    -goodFile.jar
    -someFolder/
        -subFolder/
            -alsoGood.jar
-otherCode/
    -fileToExclude.jar
-otherOtherCode/
    -otherSubfolder/
        -alsoExclude.jar

Currently in .gitignore I've tried:

*.jar
!libs
!libs/
!libs/*
!libs/**
!libs/**/
!libs/**/*.jar
!libs/*.jar

Either on their own, in combination, or even all together. None of them work. The only way I've found to do it is to either put in another .gitignore file into libs/ (which I would prefer to avoid) or use a !libs/*/*/*.jar line for every possible level of subdirectory. Is there a way to make it ignore all jars except the ones in libs?

1 Answer 1

31

How about:

*.jar
!libs/**/*.jar

The order is important.

Edit I used your project structure and have the following output after I did a git add and git status

$ git stat
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   .gitignore
    new file:   libs/goodFile.jar
    new file:   libs/someFolder/subFolder/alsoGood.jar
    new file:   libs/someFolder/subFolder/test/anotherFolder/test.jar



$ cat .gitignore 
*.jar
!libs/**/*.jar
10
  • I've tried that, but it still ignores any .jars that aren't in libs/ directly and are instead in subfolders.
    – CSturgess
    Commented Mar 20, 2014 at 15:31
  • are you sure. I just tried it with your suggested structure and it works perfectly
    – peshkira
    Commented Mar 20, 2014 at 15:32
  • Okay, it'll pick it up if it's one subdirectory down, but most of the jars I need to include are 4 or 5 levels deep. Those ones aren't being picked up.
    – CSturgess
    Commented Mar 20, 2014 at 15:34
  • 2
    Check also your .git/info/exclude file for possible hidden excludes. Commented Mar 20, 2014 at 15:42
  • 1
    This worked. Turns out the issue was my git version. I was running 1.7 and support for "**" was only added in 1.8.
    – CSturgess
    Commented Mar 21, 2014 at 19:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.