Search code examples
gitgitignore

gitignore all files except the .json files in all subdirectories of a particular directory


my current directory tree looks like:

.
├── test1
│   ├── test.docx
│   ├── test.jpg
│   ├── test.json
│   ├── test2
│   │   ├── test.docx
│   │   ├── test.jpg
│   │   ├── test.json
│   │   └── test4
│   │       ├── test.docx
│   │       ├── test.jpg
│   │       └── test.json
│   └── test3
│       ├── test.docx
│       ├── test.jpg
│       └── test.json
└── test5
    ├── test.docx
    ├── test.jpg
    ├── test.json
    ├── test2
    │   ├── test.docx
    │   ├── test.jpg
    │   ├── test.json
    │   └── test4
    │       ├── test.docx
    │       ├── test.jpg
    │       └── test.json
    └── test3
        ├── test.docx
        ├── test.jpg
        └── test.json

I want to ignore everything except .json files which are under test1 directory.

my .gitignore file looks like:

/*
!/.gitignore
!/test1
/test1/**/*.json

my git status -sb output is:

## No commits yet on master
?? .gitignore

So, it is not tracking the .json files under test1 directory.

What might be the solution here?


Solution

  • You can add the following lines to your .gitignore file:

    # ignore all files
    **/*.*
    
    # except json files in test1 folder
    !test1/**/*.json
    

    There is also a nice post where you can find the basic for the solution.