How to git ignore subfolders / subdirectories?
Solution 1
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
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
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
The question isn't asking about ignoring all subdirectories, but I couldn't find the answer anywhere, so I'll post it: */*
.
Solution 5
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.
Marcel
Updated on July 08, 2022Comments
-
Marcel almost 2 years
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
orbin/Release
in the.gitignore
file does not exclude the directories unless I fully qualify the ignore pattern asSolution/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?
-
Paddy about 14 yearsWhy add the DLL's? If you're referencing third party DLLs, it may make sense to add a common folder that all your projects then reference.
-
Cascabel about 14 yearsPaddy's right - if those are third-party DLLs, they should probably be organized away from your code. To me, though, it sounds like you're checking in your own build products, which is not generally the way you want to go. You use git to track the information, which is fully contained in the code and build configuration. The products are, well, products. Tracking them is asking for trouble - for example, what if you change the code but forget to build and check in new products?
-
RoadBump over 11 yearsI had a problem in excluding subfolders. Tried everything, including the exact samples written here, but with no success. Finally i added a additional blank line between folder patterns, and it's working. Maybe a encoding problem. I have windows and encoded this in UTF8.
-
-
Marcel about 14 yearsIt works, except if there is a sub-folder with another project in e.g. Solution/Module/Project so for now I've added /*/*/bin/Debug and /*/*/*/bin/Debug (for sub folders). Looks like you have to add a wildcard sub folder for each level in your directory structure.
-
Cascabel about 14 years@Marcel: Well, git certainly isn't going to ignore any files you don't tell it to - it ignores whatever matches the wildcards, no more, no less.
-
Tim Henigan about 14 yearsThe same could be done by adding the patterns to the OPs
$GIT_DIR/.git/info/exclude
file. -
Andreas about 14 yearsThe difference is that the .gitignore file will follow the code, so it applies everywhere. Whereas the exclude file is local to only your repository, meaning it only applies for that specific repository. Unless other committers have a reason to want to commit these directories, I would recommend using .gitignore.
-
Marcel about 14 yearsAnother problem with this is if you have a project in your solution called Debug, which will then also be ignored. I think the solution is a combination of all the answers and comments - restructure the solution to keep common or referenced dll's in a different folder, then ignoring "bin/" and/or use wildcards.
-
Trent over 12 yearsI think that double star will cross directory boundaries, ie: /**/bin/Debug
-
Tuukka Mustonen over 12 years@Taras: Unfortunately, it does not.
-
levibostian over 10 years+1 For comment on ensuring directories not already added to git. If already added to git, do:
git rm -rf DirectoryName/
-
Chris over 9 yearsAny chance you could clarify? I've tried all sorts of variations on this and can't get
git add
to ignore a directory and all files in all subdirectories of that directory. I have trieddirname/
,dirname/*
,dirname/**
,dirname/*/*
, even in desperationdirname/*/*/*
,dirname/*/*/*/*
,dirname/*/*/*/*/*
. -
mgold over 9 years
*/*
ignores all subdirectories but not files in the current directory. To ignore a specific subdirectory is normal gitignore usage.dirname
,dirname/
, anddirname/*
all work for me. Has anything in that directory already been committed? -
Chris over 9 yearsIt is a brand new repo - I delete the whole
.git
directory between attempts, thengit init
thengit add --all .
(have also tried without--all
), using 1.9.4.msysgit.1. Every non-empty subdirectory under my named directory is added to the index (checked in TortoiseGit andgit status
). There are no relevant lines in.gitignore
starting!
but must be some conflict. Solved for now by removing the folders duringadd
. Guess this is something that'll require a bit more digging to provide the necessary info to diagnose. Thanks for reassuring me that I'm not misunderstanding the syntax. -
Bogac about 9 yearsThere was a bug on Visual Studio related to
**
. Should be fixed by now. link -
Erik Reppen almost 9 yearsgit rm --cached if you want them untracked/irrelevant to history but want to keep the files intact locally.
-
Daniel Hári almost 8 yearsIf only one file exists in "bin", and no subfolders, then it not works. (want to delete ignored dirs with : git clean -Xfdn)
-
Jean Carlos Racoski over 7 yearsWhat if I need to ignore a folder inside a folder, but not the first one? As in forum/ignorethisfolder/ ...but he is not to ignore the folder called forum
-
ManuelSchneid3r over 7 yearsHow is this different to just use "bin/Debug/"?
-
bvj about 6 yearsI'm noticing that the
**/
prefix is required for directory paths with multiple levels; e.g.,**/wwwroot/dist/
, but specifyingdist/
alone works without the**/
prefix when attempting to match the path spec somewhere beneath the root. -
Tony about 6 yearsThis doesn't work for me in ignoring a directory whose name starts with a dot. "/." and ".*/*" don't work either.
-
NotTooTechy almost 4 yearsSimply: git reset Solution , than add to .gitignore Solution/*/bin/Debug, Solution/*/bin/Release, and than git add Solutions
-
Lewis Levin about 3 yearsDoesn't work for me. directories created by build continue to pop up as unstaged. Driving me crazy. git rm -rf reports foo/ reports that foo is not present.
-
Andreas about 3 yearsStrange, I can't recreate that behavior (on Mac OS X 11.2 using git v2.31.1). Here's my session: gist.github.com/andreaja/c2a07ecc6cc5bb332a198fd2cc78c8e8
-
OceanFire about 3 yearsIt should be
git rm -r --cached .
-
Meryan about 3 yearsThanks I used .vscode/*/ I would add to your solution "ignore subfolders but include the files at the root of this folder."
-
Specimen almost 3 yearsThank you, was wondering why adding the files to .gitignore was not working; they were already committed into the repo
-
d0rf47 over 2 yearsbest answer in this thread thank you!
-
Motla about 2 yearsexactly what I was looking for
-
user3748973 almost 2 yearsHow can undo this command?