Search code examples
gitgitignore

Gitignore - Allow only one folder and in that folder, dont allow those files without name ending with `.json`


I want to do this: allow only one folder and in that folder, don't allow those files without name ending with .json

Is this possible ?

Example:

a/one.txt
a/one.config.yaml
a/two.json
b/one.text
b/one.config.yaml
b/three.json

In this, only 2 should be allowed

a/one.txt
a/one.config.yaml

Thanks!


Solution

  • I'd recommend against the "everything except" pattern when you can. It especially gets tricky with sub-directories.

    This should work for you though, in a .gitignore:

    # ignore all folders
    */
    
    # except this folder
    !*a/
    
    # business as usual for files
    # ignore the extensions you don't want
    *.json