1451

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. There are also files and directories that have the substring 'bin' in their names, which I want to keep.

This is on Windows using the latest msysgit.

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
Ben Hymers
  • 25,586
  • 16
  • 59
  • 84
  • 22
    Probably somebody will find as helpful: if you have few repositories and each has the same .gitignore file, you can put it into "common" location and launch > git config --global core.excludesfile ~/.gitignore_global The last parameter is path to the common location. P.S. I'm new to git and not sure if that is 'best practice' recommendation. So please let me know if you should not want to do that. Thank you. – Budda Nov 28 '11 at 02:04
  • The `**` syntax seems now (March 2013, git 1.8.2) officially documented: see [my answer below](http://stackoverflow.com/a/16165581/6309) – VonC Apr 23 '13 at 09:21
  • 3
    If anyone wants to read the most up-to-date version of the actual manual page, see [gitignore(5) Manual Page](http://git-scm.com/docs/gitignore). –  Jul 18 '14 at 17:01
  • friendlier option if you're tired of reading essays for one line's worth of information https://www.atlassian.com/git/tutorials/saving-changes/gitignore#git-ignore-patterns – suazi May 03 '20 at 01:57
  • Possibly worth noting, when creating a global ignore file, it's beneficial to name it `~/.cvsignore`. Other version control systems use this name, making it even more "global". – evanthegrayt Sep 16 '21 at 13:29

16 Answers16

2119

Before version 1.8.2, ** didn't have any special meaning in the .gitignore. As of 1.8.2 git supports ** to mean zero or more sub-directories (see release notes).

The way to ignore all directories called bin anywhere below the current level in a directory tree is with a .gitignore file with the pattern:

bin/

In the man page, there an example of ignoring a directory called foo using an analogous pattern.

If you already have any bin folders in your git index which you no longer wish to track then you need to remove them explicitly. Git won't stop tracking paths that are already being tracked just because they now match a new .gitignore pattern. Execute a folder remove (rm) from index only (--cached) recursivelly (-r). Command line example for root bin folder:

git rm -r --cached bin
Paulo Mattos
  • 18,845
  • 10
  • 77
  • 85
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 75
    Starting with git 1.8.2, git will accept `**` in `.gitignore` files (source: [1.8.2 changelog](https://github.com/git/git/blob/master/Documentation/RelNotes/1.8.2.txt)) – Carlos Campderrós Feb 18 '13 at 10:58
  • 18
    is `bin` the same of `bin/` ? – Nisba Dec 05 '16 at 17:16
  • 39
    @Nisba, they are different, `bin` will ignore files named bin, while `bin/` is for directories named bin – Rani Kheir Jan 29 '17 at 04:59
  • 8
    since I fell over this: folder name in git-command IS case sensitive :) "bin !=Bin" - maybe I spare someone a few minutes by this hint :) – dba Feb 21 '18 at 13:04
  • 1
    In windows10 you cannot create a .gitignore file, you have to create a gitignore.txt and then run ren gitignore.txt .gitignore in command prompt to create this file – shabby Jan 02 '19 at 14:45
  • 1
    So what is the difference between `**.thing/` and `.thing/` in top level gitignore assuming using git 1.8.2 or greater? – Startec May 19 '20 at 23:22
  • 1
    @shabby not entirely true. You can actually create a `.gitignore` file if you have displaying file extensions enabled. At that point you simply remove the ".txt" from the end when naming the file.Also, when renaming, it only works if you do a "Save As" in whatever program, and either surround .gitignore in quotes for the filename, or select "All files (*.*)" in the "Save as type" dropdown. – GrowingCode247 Sep 03 '20 at 12:02
  • Is this the same for any file? Meaning if I had a file named "foo.txt" that I wanted to ignore anywhere. I could just put `foo.txt/` into my .gitignore and it would be ignored anywhere? – Alec Mather May 17 '21 at 20:45
  • Are **/bin/ and bin/ equal? – Filippo Grazioli Apr 26 '22 at 06:45
  • *I actually had to do `git rm -r --cached **/bin` – Veles Jul 21 '23 at 10:05
523

The .gitignore of your dream seems to be:

bin/

on the top level.

Bombe
  • 81,643
  • 20
  • 123
  • 127
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
296

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/

plancys
  • 3,833
  • 2
  • 19
  • 26
76

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/
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
50
[Bb]in/

matches both upper and lower case

wisbucky
  • 33,218
  • 10
  • 150
  • 101
40

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

# 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/

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.

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

PM console Then the issue will be completely solved.

Shadi Alnamrouti
  • 11,796
  • 4
  • 56
  • 54
Bravo Yeung
  • 8,654
  • 5
  • 38
  • 45
37

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.

Cory
  • 860
  • 8
  • 10
  • 3
    This isn't an answer to the original question which was explicitly about `bin`, not `Bin`. – CB Bailey Oct 09 '09 at 07:08
  • 57
    Though not an answer, it does add to the completeness for others searching on the same issue (myself, just now). – Jay Jan 12 '10 at 19:44
27

[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*/
Jaider
  • 14,268
  • 5
  • 75
  • 82
  • 3
    Note: Stuff like this should be in your GLOBAL gitignore (usually in your home directory.) The project gitignore should only ignore things specific to your project (i.e. .o files for a C app.) – BraveNewCurrency Feb 13 '15 at 21:53
19

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.

Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
  • 5
    This doesn't really answer the question, but it's exactly what I was looking for. – Dave Feb 03 '16 at 15:52
17

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 
AnthonyC
  • 1,880
  • 2
  • 20
  • 27
7

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

bin

5

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/*

Kerem
  • 11,377
  • 5
  • 59
  • 58
4

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

Dhwaneel
  • 541
  • 5
  • 8
2

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.

Norman
  • 3,279
  • 3
  • 25
  • 42
1

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.

prosti
  • 42,291
  • 14
  • 186
  • 151
0

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

Jacek Plesnar
  • 479
  • 3
  • 6