gitignore 除了特定目录的所有子目录中的 .json 文件之外的所有文件

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

我当前的目录树如下所示:

.
├── 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

我想忽略除 test1 目录下的 .json 文件之外的所有内容。

我的 .gitignore 文件看起来像:

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

我的git status -sb输出是:

## No commits yet on master
?? .gitignore

因此,它没有跟踪 test1 目录下的 .json 个文件。

这里的解决方案可能是什么?

在 .gitignore 文件中只尝试这一行

test1/!*.json

您可以将以下行添加到您的 .gitignore 文件中:

# ignore all files
**/*.*

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

还有一个很好的post,您可以在其中找到解决方案的基础。