Difference in the paths in .gitignore file?
I've been using git but still having confusion about the .gitignore file paths.
So, what is the difference between the following two paths in .gitignore file?
tmp/* public/documents/**/*
I can understand that tmp/*
will ignore all the files and folders inside it. Am I right?
But what does that second line path mean?
This depends on the behavior of your shell. Git doesn't do any work to determine how to expand these. In general, *
matches any single file or folder:
/a/*/z
matches /a/b/z
matches /a/c/z
doesn't match /a/b/c/z
**
matches any string of folders:
/a/**/z
matches /a/b/z
matches /a/b/c/z
matches /a/b/c/d/e/f/g/h/i/z
doesn't match /a/b/c/z/d.pr0n
Combine **
with *
to match files in an entire folder tree:
/a/**/z/*.pr0n
matches /a/b/c/z/d.pr0n
matches /a/b/z/foo.pr0n
doesn't match /a/b/z/bar.txt
Update (08-Mar-2016)
Today, I am unable to find a machine where **
does not work as claimed. That includes OSX-10.11.3 (El Capitan) and Ubuntu-14.04.1 (Trusty). Possibly git-ignore as been updated, or possibly recent fnmatch handles **
as people expect. So the accepted answer now seems to be correct in practice.
Original post
The **
has no special meaning in git. It is a feature of bash >= 4.0, via
shopt -s globstar
But git does not use bash. To see what git actually does, you can experiment with git add -nv
and files in several levels of sub-directories.
For the OP, I've tried every combination I can think of for the .gitignore
file, and nothing works any better than this:
public/documents/
The following does not do what everyone seems to think:
public/documents/**/*.obj
I cannot get that to work no matter what I try, but at least that is consistent with the git docs. I suspect that when people add that to .gitignore
, it works by accident, only because their .obj
files are precisely one sub-directory deep. They probably copied the double-asterisk from a bash script. But perhaps there are systems where fnmatch(3)
can handle the double-asterisk as bash can.
If you're using a shell such as Bash 4, then ** is essentially a recursive version of *, which will match any number of subdirectories.
This makes more sense if you add a file extension to your examples. To match log files immediately inside tmp, you would type:
/tmp/*.log
To match log files anywhere in any subdirectory of tmp, you would type:
/tmp/**/*.log
But testing with git version 1.6.0.4 and bash version 3.2.17(1)-release, it appears that git does not support ** globs at all. The most recent man page for gitignore doesn't mention **, either, so this is either (1) very new, (2) unsupported, or (3) somehow dependent on your system's implementation of globbing.
Also, there's something subtle going on in your examples. This expression:
tmp/*
...actually means "ignore any file inside a tmp directory, anywhere in the source tree, but don't ignore the tmp directories themselves". Under normal circumstances, you'd probably just write:
/tmp
...which would ignore a single top-level tmp directory. If you do need to keep the tmp directories around, while ignoring their contents, you should place an empty .gitignore file in each tmp directory to make sure that git actually creates the directory.
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
".
See commit 4c251e5cb5c245ee3bb98c7cedbe944df93e45f4:
"
foo/**/bar
" matches "foo/x/bar
", "foo/x/y/bar
"... but not "foo/bar
".
We make a special case, whenfoo/**/
is detected (and "foo/
" part is already matched), try matching "bar
" with the rest of the string."Match one or more directories" semantics can be easily achieved using "
foo/*/**/bar
".This also makes "
**/foo
" match "foo
" in addition to "x/foo
", "x/y/foo
"..Signed-off-by: Nguyễn Thái Ngọc Duy
<[email protected]>
Simon Buchan also commented:
current docs (
.gitignore
man page) are pretty clear that no subdirectory is needed,x/**
matches all files under (possibly empty)x
The .gitignore
man page does mention:
A trailing "
/**
" matches everything inside. For example, "abc/**
" matches all files inside directory "abc
", relative to the location of the.gitignore
file, with infinite depth.A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "
a/**/b
" matches "a/b
", "a/x/b
", "a/x/y/b
" and so on.
When ** isn't supported, the "/" is essentially a terminating character for the wildcard, so when you have something like:
public/documents/**/*
it is essentially looking for two wildcard items in between the slashes and does not pick up the slashes themselves. Consequently, this would be the same as:
public/documents/*/*