What's the difference between adding bin
, bin/
, bin/*
and bin/**
in my .gitignore file? I've been using bin/
, but looking at other .gitignore files (in the eclipse file the double and single star are even used together like this: tmp/**/*
what's up with that?) I see that the first two patterns are also widely used as well. Can someone please explain the differences between the three?
-
4@unutbu: The accepted answer for that question is apparently disputed. One of the top comments claims the answer is in fact a complete myth. – chandsie Jan 09 '12 at 04:17
-
The behavior is completely specified in the manpage, and I'm sure there's a question/answer around here (or ten) that include all that information. – Cascabel Jan 09 '12 at 06:14
-
3With respect to `**`: http://stackoverflow.com/questions/1470572/gitignore-ignore-any-bin-directory – Cascabel Jan 09 '12 at 06:20
6 Answers
bin
matches any files or directories named 'bin'.
bin/
matches any directories named 'bin', which in effect means all of its contents since Git doesn't track directories alone.
bin/*
matches all files and directories directly in any bin/
. This prevents Git automatically finding any files in its subdirectories, but if, say a bin/foo
subdirectory is created, this rule will not match foo
's contents.
bin/**
matches all files and directories in any bin/
directory and all of its subdirectories.
The word "any" is critical here since rules are not relative to the repository root and apply anywhere in the filesystem tree. You must begin rules with a /
(or !/
to un-ignore) which means the repository's root, not the system's root, in order to match only what was intended.
WARNING: You should never use rules like dir/*
, /dir/**
, etc. alone unless you also un-ignore something that exists inside that directory. Omit the asterisk or you could permanently lose a lot of data from certain invocations of git gc
, git stash
and more.
I don't really know what tmp/**/*
is meant to do. I initially thought it could be used to match files in the sub-directories of tmp/
but not files directly present in tmp/
itself. But a simple test seems to suggest that this ignores all files in tmp/
.

- 8,535
- 2
- 44
- 59

- 6,130
- 1
- 33
- 20
-
10
-
1I suspect `bin/` will ignore the bin directory, whereas `bin/**` will include the bin directory but not any of its contents – Robin Winslow Aug 08 '12 at 10:00
-
1That seems inconsistent with Siddhartha's answer. Drawing from the answer, `bin/` will ignore the directory itself (including all sub-directories and files), whereas `bin/**` will ignore all files in the bin directory and its sub-directories, but _not_ the bin directory itself. Whether or not that's accurate, I'm unsure. – Christopher Berman Nov 02 '12 at 20:47
-
3note that, if you want to track all files in the bin/ directory but ignore all files in its subdirectories, you can do (on subsequent lines) `bin/** \n !bin/*` (since I can't see how to force a linebreak in mini-Markdown) – TomRoche Mar 03 '13 at 06:00
-
1If you're wondering why ignoring `bin/` is distinct from ignoring everything inside `bin/**`, try [excluding at least one of the files inside](http://stackoverflow.com/a/8025106/700195) from the match. Adding the double asterisk lowers the precedence of the pattern below matches to specific files in the directory. – Griffin Mar 03 '14 at 01:47
-
the ** was the solution for me, but i had to upgrade git - 1.6 was too old and did not support ** http://linuxg.net/how-to-install-git-1-8-4-on-ubuntu-13-10-13-04-12-10-12-04-linux-mint-16-15-14-13-elementary-os-0-2-and-pear-os-8/ – Benjamin May 26 '14 at 13:13
-
-
11This answer is wrong in so many ways. First, git does not keep track of directories, so a .gitignore entry can only ever match directory contents, never a directory as such. Second, `bin` matches **both** a file named `bin` **and** the contents of the `bin` folder. Third, `bin/*` **does match** any files in its subdirectories. Did you guys even test this? – ThomasR Feb 23 '16 at 03:34
-
Git doesn't track directories so why would it ignore a directory? Seems weird :) – Joe Phillips Feb 23 '16 at 05:03
-
This answer is still incorrect. But the "Suggested edit queue is full" so I can't do anything about it myself. beginning a rule with `/` does not match the repository root - it matches the directory the .gitignore file is in. See https://stackoverflow.com/questions/5861280/gitignore-folder-vs-folder (I also double checked this (in git version 2.17.1) myself) – Phi Sep 09 '20 at 20:42
bin
and bin/
differ only in that the latter will only match a directory.
bin/**/*
is the same as bin/**
(apparently since 1.8.2, according to @VonC's answer).
The tricky one, that I just spent an hour or so ripping my hair out over, is that bin/
and bin/**
are not quite the same! Since the earlier ignores the directory as a whole, and the latter ignores each of the files within it, and git in nearly all cases doesn't care about directories, there is normally no difference. However, if you try to use !
to un-ignore a subpath, then you will find that git (ahem) ignores it if you ignored the parent directory! (again, rather than the directory contents)
This is clearest by example, so for a newly init-ed repository set up so:
$ cat .gitignore
ignored-file
or-dir
dir-only/
!dir-only/cant-reinclude
dir-contents/**
!dir-contents/can-reinclude
$ mkdir or-dir dir-only dir-contents
$ touch file ignored-file or-dir/ignored-file dir-only/cant-reinclude dir-contents/can-reinclude
The following untracked files exist:
$ git ls-files --other
.gitignore
dir-contents/can-reinclude
dir-only/cant-reinclude
file
ignored-file
or-dir/ignored-file
But you can see the following files are not ignored:
$ git ls-files --other --exclude-standard
.gitignore
dir-contents/can-reinclude
file
And if you try to add, you get:
$ git add dir-only/cant-reinclude
The following paths are ignored by one of your .gitignore files:
dir-only/cant-reinclude
Use -f if you really want to add them.
fatal: no files added
I consider this behavior a bug. (This is all on git version 1.8.4.msysgit.0
)

- 12,707
- 2
- 48
- 55
-
1+1, indeed. You should consider filing an actual bug report on this because the behavior does seem unexpected. – chandsie Dec 06 '13 at 05:46
-
1
-
3The differing behavior of `dir/` and `dir/**` re. un-ignoring with `!` happens because "It is not possible to re-include a file if a parent directory of that file is excluded" [[source](http://git-scm.com/docs/gitignore)]. Confusing, but done for performance reasons. See a [related SO question](http://stackoverflow.com/q/3667986). – tanius Jan 05 '15 at 03:18
Note that, strictly speaking, git does not track directories, only files. It is hence not possible to add a directory, only its contents.
In the context of .gitignore
however, git pretends to understand directories for the sole reason that
It is not possible to re-include a file if a parent directory of that file is excluded.
https://git-scm.com/docs/gitignore#_pattern_format
What does this mean for the exclude patterns? Let's go through them in detail:
bin
This ignores
- files named
bin
. - the contents of folders named
bin
You can whitelist ignored bin
files and folders by adding subsequent !
entries, but you cannot whitelist the contents of folders named bin
bin
!bin/file_in_bin # has no effect, since bin/ is blacklisted!
!bin/* # has no effect, since bin/ is blacklisted!
!file_in_bin # has no effect, since bin/ is blacklisted!
!bin # this works
bin/
Same as above, except it does not match files named bin
. Adding a trailing /
tells git to match directories only.
bin/*
This ignores
- files contained in a folder named
bin
- contents of direct subfolders of folders named
bin
bin/* # blacklists bin/file_in_bin and bin/subfolder/
!bin/subfolder/file_in_sub # has no effect, since bin/subfolder is blacklisted!
!bin # whitelists files named bin/bin, since bin/ itself is not blacklisted
!bin/ # has no effect, since bin/ itself is not blacklisted
!bin/file_in_bin # works since bin/ itself is not blacklisted
!file_in_bin # works too
!bin/subfolder # works (so implicitly whitelists bin/subfolder/file_in_sub)
!bin/subfolder/ # works just as well
!bin/* # works for file_in_bin and subfolder/
bin/**
This ignores
- contents of
bin
- contents of subfolders (any level of nesting) within
bin
bin/** # blacklists bin/file_in_bin and
# bin/subfolder/ and bin/subfolder/file_in_sub and
# bin/subfolder/2/ and bin/subfolder/2/file_in_sub_2
!bin/subfolder/file_in_sub # has no effect, since bin/subfolder is blacklisted
!bin/subfolder/2/ # has no effect, since bin/subfolder is blacklisted
!bin/subfolder/2/file_in_sub_2 # has no effect, since bin/subfolder is blacklisted
!bin/subfolder # works only in combinations with other whitelist entries,
# since all contents of subfolder are blacklisted (1)
!bin/file_in_bin # works since bin itself is not blacklisted
!bin/* # works for file_in_bin and subfolder; see (1)

- 1,067
- 1
- 11
- 16
Note that the '**
', when combined with a sub-directory (**/bar
), must have changed from its default behavior, since the release note for git1.8.2 now mentions:
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
".
The rule to remember (and which help understand the difference of intent behind those syntax) is:
It is not possible to re-include a file if a parent directory of that file is excluded.
Typically, if you want to exclude files from a subfolder of an ignore folder f, you would do:
f/**
!f/**/
!f/a/sub/folder/someFile.txt
That is:
- If the first rule was
f/
, the folderf/
would be ignored and rules below regardingf
would not matter. f/**
achieve the same asf/
, but ignore all sub-elements (files and subfolders).
That gives you the opportunity to whitelist (exclude from gitignore) the subfolders:!f/**/
.- Since all
f
subfolders are not ignored, you can add a rule to exclude a file (!f/a/sub/folder/someFile.txt
)
I just made a new repo and tried some things. Here are my results:
NEW RESULTS
git version 2.10.1.windows.1
- Initialize nearly-empty repo. Only the README file
- Populate the
bin
directory several layers deepbin.txt
Test.txt
bin/a/b/bin.txt
bin/a/b/Test.txt
bin/a/bin/bin.txt
bin/a/bin/Test.txt
bin/a/bin.txt
bin/a/Test.txt
bin/bin.txt
bin/Test.txt
- Add
bin
to gitignore: Results- Everything beneath the
bin
directory (and deeper) is now ignored - Root level is not ignored (/bin.txt and /Test.txt still show)
- Everything beneath the
- Edit
bin
tobin/
in the gitignore: Results- No change
- Edit
bin/
tobin/*
- No change
- Edit
bin/*
tobin/**
- No change
- Edit
bin/**
tobin/**/
bin/bin.txt
andbin/Test.txt
are no longer ignored
- Edit
bin/**/
tobin/**/*
bin/bin.txt
andbin/Test.txt
are back to being ignored
OLD RESULTS
git version: 2.7.0.windows.1
- Initialize nearly-empty repo. Only the README file
- Populate the
bin
directory several layers deepbin/a/b/Test.txt
bin/a/bin/Test.txt
bin/a/Test.txt
bin/Test.txt
- Add
bin
to gitignore: Results- Everything beneath the
bin
directory (and deeper) is now ignored
- Everything beneath the
- Edit
bin
tobin/
in the gitignore: Results- Everything beneath the
bin
directory (and deeper) is still ignored (no change)
- Everything beneath the
- Edit
bin/
tobin/*
- Everything beneath the
bin
directory (and deeper) is still ignored (no change)
- Everything beneath the
- Edit
bin/*
tobin/**
- Everything beneath the
bin
directory (and deeper) is still ignored (no change)
- Everything beneath the
- Edit
bin/**
tobin/**/
bin/Test.txt
is no longer ignored
- Edit
bin/**/
tobin/**/*
- Everything beneath the
bin
directory (and deeper) is ignored again
- Everything beneath the

- 49,743
- 32
- 103
- 159
-
1This is a good test, I think that renaming text.txt to bin.txt would better show some key differences. If you did this I would vote for this answer. – Matt Johnson Jan 26 '17 at 16:58
-
@MattJohnson True... unfortunately I am on a newer git version at the moment – Joe Phillips Jan 27 '17 at 00:30
-
-
Nice summary but typo/incorrect: #3: "Root level is not ignored" ... no change no change no change ... "no longer ignored"? Wait, **when was it ignored**? Now 5 years old the detail here doesn't matter anyway and should be rechecked in current systems. – TonyG Oct 31 '22 at 15:58
There's another difference between bin/*
and bin/
.
bin/
matches foo/bin/test.txt
(as expected), but bin/*
does not, which seems strange, but it's documented: https://git-scm.com/docs/gitignore
"Documentation/*.html" matches "Documentation/git.html" but not "Documentation/ppc/ppc.html" or "tools/perf/Documentation/perf.html".
The reason for this appears to be these rules:
If the pattern ends with a slash, it is removed for the purpose of the following description …
If the pattern does not contain a slash /, Git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file …
Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag …
So if the pattern ends with a slash, the slash is removed and it's treated as a shell glob pattern, in which case bin
matches foo/bin/test.txt
.
If it ends with /*
, the slash isn't removed and it's passed to fnmatch, which doesn't match in subdirectories.
However, the same is not true for foo/bin/
and foo/bin/*
, because even after removing the trailing slash from foo/bin/
, it still contains a slash, so it's treated as an fnmatch pattern, not a glob. I.e. it will not match bar/foo/bin/test.txt

- 2,674
- 1
- 21
- 18