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

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.

This question is related to git version-control directory gitignore subdirectory

The answer is


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.


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

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.


Examples related to git

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio?

Examples related to version-control

How can I switch to another branch in git? Do I commit the package-lock.json file created by npm 5? Project vs Repository in GitHub Remove a modified file from pull request Git push: "fatal 'origin' does not appear to be a git repository - fatal Could not read from remote repository." Git: How to squash all commits on branch git: updates were rejected because the remote contains work that you do not have locally Sourcetree - undo unpushed commits Cannot checkout, file is unmerged Git diff between current branch and master but not including unmerged master commits

Examples related to directory

Moving all files from one directory to another using Python What is the reason for the error message "System cannot find the path specified"? Get folder name of the file in Python How to rename a directory/folder on GitHub website? Change directory in Node.js command prompt Get the directory from a file path in java (android) python: get directory two levels up How to add 'libs' folder in Android Studio? How to create a directory using Ansible Troubleshooting misplaced .git directory (nothing to commit)

Examples related to gitignore

Gitignore not working How to add files/folders to .gitignore in IntelliJ IDEA? Apply .gitignore on an existing repository already tracking large number of files Ignore .classpath and .project from Git .gitignore all the .DS_Store files in every folder and subfolder Correctly ignore all files recursively under a specific folder except for a specific file type What should be in my .gitignore for an Android Studio project? Commit empty folder structure (with git) How to remove files that are listed in the .gitignore but still on the repository? What to gitignore from the .idea folder?

Examples related to subdirectory

Correctly ignore all files recursively under a specific folder except for a specific file type How do I get a list of folders and sub folders without the files? List all files and directories in a directory + subdirectories Import module from subfolder Browse files and subfolders in Python Vbscript list all PDF files in folder and subfolders Pick images of root folder from sub-folder PHP Get all subdirectories of a given directory How to create nonexistent subdirectories recursively using Bash? Import a file from a subdirectory?