Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git ignore all files of a certain type except in all subdirectories of a certain directory?

Tags:

git

gitignore

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?

like image 371
CSturgess Avatar asked Mar 20 '14 15:03

CSturgess


People also ask

How do I ignore a specific file type in Git?

If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.

Does .gitignore work in subdirectories?

gitignore file is usually placed in the repository's root directory. However, you can create multiple . gitignore files in different subdirectories in your repository.

Can you have multiple Git ignore files?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .


1 Answers

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
like image 55
peshkira Avatar answered Oct 04 '22 13:10

peshkira