-
Notifications
You must be signed in to change notification settings - Fork 0
Pattern
A pattern is basically the same as bash pattern matching, with the following extensions:
Pattern | Description |
---|---|
expr/ |
Matches any file or folder matched by expr and it's recursive contents |
/expr |
Matches expr only if it matches from the start |
!<expr> |
Negate expression. It's not possible to negate a negated expression. |
<expr> |
All other expressions match filenames. |
Examples:
Pattern | bash pattern | Description |
---|---|---|
x |
*/x|x |
Match "x" anywhere |
*.c |
*/*.c|*.c |
Match files with .c extension anywhere |
/x |
x |
Match "x" only in the top folder |
x/ |
*/x/*|x/* |
Match anything below a folder named "x" |
/x/ |
x/* |
Match anything below a top folder named "x" |
Here is a simple example to match .txt
files that are stored somewhere inside
the doc folder or subfolders. But it avoids those, that are in an "old" folder.
# txt only please, no old stuff
/doc/*.txt
!old/
If you use
[]
for defining a character set, make sure that the hyphen '-' is the last character in the set.
.gitignore is very similar but there are differences. Here's an edited copy of the gitignore PATTERN FORMAT to see the differences:
-
A blank line matches no files, so it can serve as a separator for readability.
-
A line starting with # serves as a comment. Put a backslash ("") in front of the first hash for patterns that begin with a hash.
-
Trailing spaces are ignored.
unless they are quoted with backslash (""). -
An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.
It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.Put a backslash ("") in front of the first "!" for patterns that begin with a literal "!", for example, "!important!.txt". -
If the pattern ends with a slash, it is removed for the purpose of the following description
, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in Git). -
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 (relative to the toplevel of the work tree if not from a .gitignore file).
-
Otherwise, Git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the
FNM_PATHNAME
flag: wildcards in the pattern will not match a / in the pathname. For example,Documentation/*.html
matchesDocumentation/git.html
but notDocumentation/ppc/ppc.html
ortools/perf/Documentation/perf.html
. -
A leading slash matches the beginning of the pathname. For example,
/*.c
matchescat-file.c
but notmozilla-sha1/sha1.c
. -
Two consecutive asterisks
**"
in patterns matched against full pathname may have special meaning: -
A leading
**
followed by a slash means match in all directories. For example,**/foo
matches file or directoryfoo
anywhere, the same as patternfoo
.**/foo/bar
matches file or directorybar
anywhere that is directly under directoryfoo
. -
A trailing/**
matches everything inside. For example,abc/**
matches all files inside directoryabc
, 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
matchesa/b
,a/x/b
,a/x/y/b
and so on. -
Other consecutive asterisks are considered invalid.