How to git ignore subfolders / subdirectories?

GitDirectoryIgnoreGitignore

Git Problem Overview


I have a lot of projects in my .Net solution. I would like to exclude all "bin/Debug" and "bin/Release" folders (and their contents), but still include the "bin" folder itself and any dll's contained therein.

.gitignore with "bin/" ignores "Debug" and "Release" folders, but also any dll's contained in the "bin" folder.

bin/Debug or bin/Release in the .gitignore file does not exclude the directories unless I fully qualify the ignore pattern as Solution/Project/bin/Debug - which I don't want to do as I will need to include this full pattern for each project in my solution, as well as add it for any new projects added.

Any suggestions?

Git Solutions


Solution 1 - Git

Have you tried wildcards?

Solution/*/bin/Debug
Solution/*/bin/Release

With version 1.8.2 of git, you can also use the ** wildcard to match any level of subdirectories:

**/bin/Debug/
**/bin/Release/

Solution 2 - Git

You can use .gitignore in the top level to ignore all directories in the project with the same name. For example:

Debug/
Release/

This should update immediately so it's visible when you do git status. Ensure that these directories are not already added to git, as that will override the ignores.

Solution 3 - Git

All the above answers are valid, but something that I don't think is mentioned is that once you add a file from that directory into the repo, you can't ignore that directory/subdirectory that contains that file (git will ignore that directive).

To ignore already added files run

$ git rm -r --cached .

Otherwise you'll have to remove all files from the repo's target directory first - and then you can ignore that folder.

Solution 4 - Git

The question isn't asking about ignoring all subdirectories, but I couldn't find the answer anywhere, so I'll post it: */*.

Solution 5 - Git

Besides putting the correct entries in your .gitignore file, if you're trying to ignore something already added to the repo, you have to do git rm -r /path/to/dir and commit that before you add the dir to your .gitignore file. Otherwise the only thing git will ignore is your ignore directive.

Solution 6 - Git

The only way I got this to work on my machine was to do it this way:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
/*.*

#Only Include these specific directories and subdirectories:
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/

Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories 5 deep under themes, I still need to spell that out.

This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153

These were useful topics:

I also tried

*
*/*
**/**

and **/wp-content/themes/**

or /wp-content/themes/**/*

None of that worked for me, either. Lots of trail and error!

Solution 7 - Git

To exclude content and subdirectories:

**/bin/*

To just exclude all subdirectories but take the content, add "/":

**/bin/*/

Solution 8 - Git

To ignore all subdirectories you can simply use:

**/

This works as of version 1.8.2 of git.

Solution 9 - Git

The generic way to ignore all subfolders, while continuing to track the files that are in the /bin directory would be to add the following line to your project's .gitignore file:

bin/*/*

If you want to ignore only particular named subfolders, you can do:

bin/Debug/*
bin/Release/*

nb. if the bin directory is not in the root of your project (alongside the .gitignore file), then instead of eg. bin/*/* you might need path/to/bin/*/*

Solution 10 - Git

The simplest form to ignore all subfolders of the root of your repo in your .gitignore file is:

*/

If you want ignore all subfolders of another subfolder:

subfolder/*/

If you have already commited items that need to be ignored, you will need to remove them from the repo before they are ignored.

To remove all subfolders of the root of your repo:

git rm -r --cached */*

To remove all subfolders of another subfolder:

git rm -r --cached subfolder/*/*

Solution 11 - Git

Seems this page still shows up on the top of Google search after so many years...

Modern versions of Git support nesting .gitignore files within a single repo. Just place a .gitignore file in the subdirectory that you want ignored. Use a single asterisk to match everything in that directory:

echo "*" > /path/to/bin/Debug/.gitignore
echo "*" > /path/to/bin/Release/.gitignore

If you've made previous commits, remember to remove previously tracked files:

git rm -rf /path/to/bin/Debug
git rm -rf /path/to/bin/Release

You can confirm it by doing git status to show you all the files removed from tracking.

Solution 12 - Git

For ignoring subfolders but not main folder I could only get this to work in Visual Studio Code by placing a dummy readme.txt in the main folder. Only then /*/ checked in the main folder (and no subfolders).

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
QuestionMarcelView Question on Stackoverflow
Solution 1 - GitJosh LeeView Answer on Stackoverflow
Solution 2 - GitAndreasView Answer on Stackoverflow
Solution 3 - Gitajacian81View Answer on Stackoverflow
Solution 4 - GitmgoldView Answer on Stackoverflow
Solution 5 - GitMidnightJavaView Answer on Stackoverflow
Solution 6 - GitKatieView Answer on Stackoverflow
Solution 7 - GitA. MorelView Answer on Stackoverflow
Solution 8 - GitfrederickjhView Answer on Stackoverflow
Solution 9 - GitNick FView Answer on Stackoverflow
Solution 10 - GitTolgaView Answer on Stackoverflow
Solution 11 - GitslackairView Answer on Stackoverflow
Solution 12 - GitedelwaterView Answer on Stackoverflow