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
  • 9,282
  • 3
  • 19
  • 36

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,262,500
  • 529
  • 4,410
  • 5,250
0

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

...
!/trials/**/*.py
!/trials/**/*.sh
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73