Skip to content

Commit 8579d82

Browse files
committed
Merge branch 'an/ignore-doc-update' into next
The description about slashes in gitignore patterns (used to indicate things like "anchored to this level only" and "only matches directories") has been revamped. * an/ignore-doc-update: gitignore.txt: make slash-rules more readable
2 parents 8e4d85b + 1a58bad commit 8579d82

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

Documentation/gitignore.txt

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@ PATTERN FORMAT
8989
Put a backslash ("`\`") in front of the first "`!`" for patterns
9090
that begin with a literal "`!`", for example, "`\!important!.txt`".
9191

92-
- If the pattern ends with a slash, it is removed for the
93-
purpose of the following description, but it would only find
94-
a match with a directory. In other words, `foo/` will match a
95-
directory `foo` and paths underneath it, but will not match a
96-
regular file or a symbolic link `foo` (this is consistent
97-
with the way how pathspec works in general in Git).
98-
99-
- If the pattern does not contain a slash '/', Git treats it as
100-
a shell glob pattern and checks for a match against the
101-
pathname relative to the location of the `.gitignore` file
102-
(relative to the toplevel of the work tree if not from a
103-
`.gitignore` file).
104-
105-
- Otherwise, Git treats the pattern as a shell glob: "`*`" matches
106-
anything except "`/`", "`?`" matches any one character except "`/`"
107-
and "`[]`" matches one character in a selected range. See
108-
fnmatch(3) and the FNM_PATHNAME flag for a more detailed
109-
description.
110-
111-
- A leading slash matches the beginning of the pathname.
112-
For example, "/{asterisk}.c" matches "cat-file.c" but not
113-
"mozilla-sha1/sha1.c".
92+
- The slash '/' is used as the directory separator. Separators may
93+
occur at the beginning, middle or end of the `.gitignore` search pattern.
94+
95+
- If there is a separator at the beginning or middle (or both) of the
96+
pattern, then the pattern is relative to the directory level of the
97+
particular `.gitignore` file itself. Otherwise the pattern may also
98+
match at any level below the `.gitignore` level.
99+
100+
- If there is a separator at the end of the pattern then the pattern
101+
will only match directories, otherwise the pattern can match both
102+
files and directories.
103+
104+
- For example, a pattern `doc/frotz/` matches `doc/frotz` directory,
105+
but not `a/doc/frotz` directory; however `frotz/` matches `frotz`
106+
and `a/frotz` that is a directory (all paths are relative from
107+
the `.gitignore` file).
108+
109+
- An asterisk "`*`" matches anything except a slash.
110+
The character "`?`" matches any one character except "`/`".
111+
The range notation, e.g. `[a-zA-Z]`, can be used to match
112+
one of the characters in a range. See fnmatch(3) and the
113+
FNM_PATHNAME flag for a more detailed description.
114114

115115
Two consecutive asterisks ("`**`") in patterns matched against
116116
full pathname may have special meaning:
@@ -152,6 +152,28 @@ To stop tracking a file that is currently tracked, use
152152
EXAMPLES
153153
--------
154154

155+
- The pattern `hello.*` matches any file or folder
156+
whose name begins with `hello`. If one wants to restrict
157+
this only to the directory and not in its subdirectories,
158+
one can prepend the pattern with a slash, i.e. `/hello.*`;
159+
the pattern now matches `hello.txt`, `hello.c` but not
160+
`a/hello.java`.
161+
162+
- The pattern `foo/` will match a directory `foo` and
163+
paths underneath it, but will not match a regular file
164+
or a symbolic link `foo` (this is consistent with the
165+
way how pathspec works in general in Git)
166+
167+
- The pattern `doc/frotz` and `/doc/frotz` have the same effect
168+
in any `.gitignore` file. In other words, a leading slash
169+
is not relevant if there is already a middle slash in
170+
the pattern.
171+
172+
- The pattern "foo/*", matches "foo/test.json"
173+
(a regular file), "foo/bar" (a directory), but it does not match
174+
"foo/bar/hello.c" (a regular file), as the asterisk in the
175+
pattern does not match "bar/hello.c" which has a slash in it.
176+
155177
--------------------------------------------------------------
156178
$ git status
157179
[...]

0 commit comments

Comments
 (0)