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

GitVersion ControlDirectoryGitignoreSubdirectory

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

@SimonBuchan is correct.

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

Solution 2 - Git

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 directories
Resources/*
!Resources/*/
!Resources/*.foo
Resources/*/*
!Resources/*/*/
!Resources/*/*.foo
Resources/*/*/*
!Resources/*/*/*/
!Resources/*/*/*.foo
Resources/*/*/*/*
!Resources/*/*/*/*/
!Resources/*/*/*/*.foo

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

Solution 3 - Git

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.

Solution 4 - Git

Either I'm doing it wrongly, or the accepted answer does not work anymore with the current git.

I have actually found the proper solution and posted it under almost the same question here. For more details head there.

Solution:

# Ignore everything inside Resources/ directory
/Resources/**
# Except for subdirectories(won't be committed anyway if there is no committed file inside)
!/Resources/**/
# And except for *.foo files
!*.foo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnawfalView Question on Stackoverflow
Solution 1 - GitJim G.View Answer on Stackoverflow
Solution 2 - GitBen MartinView Answer on Stackoverflow
Solution 3 - GitBitcoin Cash - ADA enthusiastView Answer on Stackoverflow
Solution 4 - GitAleksander StelmaczonekView Answer on Stackoverflow