Correctly ignore all files recursively under a specific folder except for a specific file type Correctly ignore all files recursively under a specific folder except for a specific file type git git

Correctly ignore all files recursively under a specific folder except for a specific file type


The best answer is to add a Resources/.gitignore file under Resources containing:

# Ignore any file in this directory except for this file and *.foo files*!/.gitignore!*.foo

If you are unwilling or unable to add that .gitignore file, there is an inelegant solution:

# Ignore any file but *.foo under Resources. Update this if we add deeper directoriesResources/*!Resources/*/!Resources/*.fooResources/*/*!Resources/*/*/!Resources/*/*.fooResources/*/*/*!Resources/*/*/*/!Resources/*/*/*.fooResources/*/*/*/*!Resources/*/*/*/*/!Resources/*/*/*/*.foo

You will need to edit that pattern if you add directories deeper than specified.


This might look stupid, but check if you haven't already added the folder/files you are trying to ignore to the index before. If you did, it does not matter what you put in your .gitignore file, the folders/files will still be staged.