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

nawfal picture nawfal · Jul 23, 2013 · Viewed 111.6k times · Source

I have seen similar questions (1, 2 and 3), but I don't get a proper solution from them.

I need to ignore all files under a particular folder except for a specific file type. The folder is a subdirectory for the root path. Let me name the folder Resources. Since I don't want to complicate things, let me ignore files under all folders named Resources wherever it is.

This is the most common solution (in all the duplicate questions)

# Ignore everything
*

# Don't ignore directories, so we can recurse into them
!*/

# Don't ignore .gitignore
!.gitignore

# Now exclude our type
!*.foo

The problem with this solution is that it stops tracking newly added files (since * ignores all files). I don't want to keep excluding each and every file type. I want normal behaviour where if any new file is added, git status shows it.

I finally got a solution here. The solution is to add another .gitignore file in Resources folder. This works correctly.

Can I achieve the same with one ignore file? I find having many ignore files in different directories a bit clunky.

This is what I'm trying to achieve:

# Ignore everything under Resources folder, not elsewhere
Resources

# Don't ignore directories, so we can recurse into them
!*Resources/

# Now exclude our type
!*.foo

But this gives the opposite output. It ignores *.foo types and tracks other files.

Answer

Jim G. picture Jim G. · Sep 18, 2014

@SimonBuchan is correct.

Since git 1.8.2, Resources/** !Resources/**/*.foo works.