1

In my git repo, I've a directory which may contain nested subdirectories. I'd like to allow only *.py, *.sh and *.json in these nested subdirectories.

I tried adding the following to .gitignore:

/trials/*
!/trials/README
/experimental/**/
!/trials/**/*.py
!/trials/**/*.sh

But it doesn't allow to me to add something like trials/foo/bar.py.

Sharad
  • 7,257
  • 3
  • 18
  • 32

2 Answers2

3

Once a folder is ignored, you cannot exclude files.
In other words:

It is not possible to re-include a file if a parent directory of that file is excluded.

The right way is exclude files from gitignore rules is by whitelisting folders first:

/trials/*
!/trials/**/

Then you can exclude files (in the same .gitignore file):

!/trials/**/*.py
!/trials/**/*.sh

Use also git check-ignore -v -- afile to see what gitignore rule applies for a given file.

Community
  • 1
  • 1
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
0

Try changing !/trials/**/.py to !/trials/**/*.py:

...
!/trials/**/*.py
!/trials/**/*.sh
Sajib Khan
  • 20,492
  • 6
  • 56
  • 69