Ignore file extension except in a specific directory in `.gitignore`
13:31 23 Sep 2024

In my repository, I have scripts that generate loads of .json files, that I want to ignore, so I have *.json in .gitignore. But I have one directory config containing .json files and I want those to be in version control. I know I can run git add -f config/*.json, but is there any solution using only .gitignore to override the previous rules, so that git sees these files automatically without me needing to manually run git add -f ...?

I have tried these, but none worked:

*.json

!configs
!configs/
!configs/*
!configs/*.json

EDIT: to clarify, both .gitignore and the configs directory are both at the the top level of the repository. The structure looks something like this:

repo/
├─ .gitignore
├─ config/
│  ├─ keep_this_1.json
│  ├─ keep_this_2.json
├─ scripts/
│  ├─ results/
│  │  ├─ d1/
│  │  │  ├─ ignore_this.json
│  │  ├─ d2/
│  │  │  ├─ ignore_this_too.json
git wildcard gitignore