[git] ignoring any 'bin' directory on a git project

I have a directory structure like this:

.git/
.gitignore
main/
  ...
tools/
  ...
...

Inside main and tools, and any other directory, at any level, there can be a 'bin' directory, which I want to ignore (and I want to ignore everything under it too). I've tried each of these patterns in .gitignore but none of them work:

/**/bin/**/*
/./**/bin/**/*
./**/bin/**/*
**/bin/**/*
*/bin/**/*
bin/**/*
/**/bin/* #and the others with just * at the end too

Can anyone help me out? The first pattern (the one I think should be working) works just fine if I do this:

/main/**/bin/**/*

But I don't want to have an entry for every top-level directory and I don't want to have to modify .gitignore every time I add a new one.

This is on Windows using the latest msysgit.

EDIT: one more thing, there are files and directories that have the substring 'bin' in their names, I don't want those to be ignored :)

This question is related to git gitignore

The answer is


In my case encoding of gitignore file was problematic, check if it is UTF-8


If you're looking for a great global .gitignore file for any Visual Studio ( .NET ) solution - I recommend you to use this one: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

AFAIK it has the most comprehensive .gitignore for .NET projects.


I didn't see it mentioned here, but this appears to be case sensitive. Once I changed to /Bin the files were ignored as expected.


Step 1: Add following content to the file .gitignore.

_x000D_
_x000D_
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/

# Visual Studio 2015 cache/options directory
.vs/
_x000D_
_x000D_
_x000D_

Step 2: Make sure take effect

If the issue still exists, that's because settings in .gitignore can only ignore files that were originally not tracked. If some files have already been included in the version control system, then modifying .gitignore is invalid. To solve this issue completely, you need to open Git Bash or Package Manager Console (see screenshot below) to run following commands in the repository root folder.

_x000D_
_x000D_
git rm -r --cached .
git add .
git commit -m "Update .gitignore"
_x000D_
_x000D_
_x000D_

PM console Then the issue will be completely solved.


[Bb]in will solve the problem, but... Here a more extensive list of things you should ignore (sample list by GitExtension):

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.sbr
obj/
[Rr]elease*/
_ReSharper*/

Literally none of the answers actually worked for me; the only one that worked for me was (on Linux):

**/bin
(yes without the / in the end)

git version 2.18.0 

In addition to @CB Bailey's answer:

I tried to remove multiple folders (in subfolders) named et-cache (caching folder from Wordpress theme) from the index and from being tracked.

I added

et-cache/

to the .gitignore file. But

git rm -r --cached et-cache

resulted in an error:

fatal: pathspec 'et-cache' did not match any files

So the solution was to use powershell:

Get-ChildItem et-cache -Recurse |% {git rm -r --cached $_.FullName}

This searches for all subfolders named et-cache. Each of the folders path (fullname) is then used to remove it from tracking in git.


The .gitignore of your dream seems to be:

bin/

on the top level.


for 2.13.3 and onwards,writing just bin in your .gitignore file should ignore the bin and all its subdirectories and files

bin


As a notice;

If you think about .gitignore does not work in a way (so added foo/* folder in it but git status still showing that folder content(s) as modified or something like this), then you can use this command;

git checkout -- foo/*


The ** never properly worked before, but since git 1.8.2 (March, 8th 2013), it seems to be explicitly mentioned and supported:

The patterns in .gitignore and .gitattributes files can have **/, as a pattern that matches 0 or more levels of subdirectory.

E.g. "foo/**/bar" matches "bar" in "foo" itself or in a subdirectory of "foo".

In your case, that means this line might now be supported:

/main/**/bin/

[Bb]in/

matches both upper and lower case


Adding **/bin/ to the .gitignore file did the trick for me (Note: bin folder wasn't added to index).


I think it is worth to mention for git beginners:

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

git rm --cached

So if you want add to ignore some directories in your local repository (which already exist) after editing .gitignore you want to run this on your root dir

git rm --cached -r .
git add .

It will basically 'refresh' your local repo and unstage ignored files.

See:

http://git-scm.com/docs/git-rm,

https://help.github.com/articles/ignoring-files/


If the pattern inside .gitignore ends with a slash /, it will only find a match with a directory.

In other words, bin/ will match a directory bin and paths underneath it, but will not match a regular file or a symbolic link bin.


If the pattern does not contain a slash, like in bin Git treats it as a shell glob pattern (greedy). So best would be to use simple /bin.

bin would not be the best solution for this particular problem.