blob: 4c17f2356c40b2fce2c588db97c3abf4f7f262b0 [file] [log] [blame]
Junio C Hamano679d22d2007-06-02 21:13:441gitignore(5)
2============
3
4NAME
5----
6gitignore - Specifies intentionally untracked files to ignore
7
8SYNOPSIS
9--------
Junio C Hamanoea1ac8d2018-07-18 20:16:4810$XDG_CONFIG_HOME/git/ignore, $GIT_DIR/info/exclude, .gitignore
Junio C Hamano679d22d2007-06-02 21:13:4411
12DESCRIPTION
13-----------
14
15A `gitignore` file specifies intentionally untracked files that
Junio C Hamano076ffcc2013-02-06 05:13:2116Git should ignore.
17Files already tracked by Git are not affected; see the NOTES
Junio C Hamano2e6ded82010-12-05 06:20:4818below for details.
Junio C Hamano679d22d2007-06-02 21:13:4419
Junio C Hamano915cd9b2008-07-20 01:24:1720Each line in a `gitignore` file specifies a pattern.
Junio C Hamano076ffcc2013-02-06 05:13:2121When deciding whether to ignore a path, Git normally checks
Junio C Hamano679d22d2007-06-02 21:13:4422`gitignore` patterns from multiple sources, with the following
Junio C Hamanode9879a2007-07-22 09:33:4223order of precedence, from highest to lowest (within one level of
24precedence, the last matching pattern decides the outcome):
Junio C Hamano679d22d2007-06-02 21:13:4425
Junio C Hamanode9879a2007-07-22 09:33:4226 * Patterns read from the command line for those commands that support
27 them.
Junio C Hamano679d22d2007-06-02 21:13:4428
29 * Patterns read from a `.gitignore` file in the same directory
Junio C Hamano20cf4932021-07-28 21:02:5230 as the path, or in any parent directory (up to the top-level of the working
31 tree), with patterns in the higher level files being overridden by those in
32 lower level files down to the directory containing the file. These patterns
33 match relative to the location of the `.gitignore` file. A project normally
34 includes such `.gitignore` files in its repository, containing patterns for
Junio C Hamano679d22d2007-06-02 21:13:4435 files generated as part of the project build.
36
Junio C Hamanode9879a2007-07-22 09:33:4237 * Patterns read from `$GIT_DIR/info/exclude`.
38
39 * Patterns read from the file specified by the configuration
Junio C Hamano042f2142016-06-27 18:05:0540 variable `core.excludesFile`.
Junio C Hamanode9879a2007-07-22 09:33:4241
Junio C Hamano99dc24a2008-03-27 23:49:5042Which file to place a pattern in depends on how the pattern is meant to
Junio C Hamano4f07aec2012-09-25 19:08:4443be used.
44
45 * Patterns which should be version-controlled and distributed to
46 other repositories via clone (i.e., files that all developers will want
47 to ignore) should go into a `.gitignore` file.
48
49 * Patterns which are
50 specific to a particular repository but which do not need to be shared
51 with other related repositories (e.g., auxiliary files that live inside
52 the repository but are specific to one user's workflow) should go into
53 the `$GIT_DIR/info/exclude` file.
54
Junio C Hamano076ffcc2013-02-06 05:13:2155 * Patterns which a user wants Git to
Junio C Hamano4f07aec2012-09-25 19:08:4456 ignore in all situations (e.g., backup or temporary files generated by
57 the user's editor of choice) generally go into a file specified by
Junio C Hamano322c6242015-03-23 21:32:4658 `core.excludesFile` in the user's `~/.gitconfig`. Its default value is
Junio C Hamano4f07aec2012-09-25 19:08:4459 $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or
60 empty, $HOME/.config/git/ignore is used instead.
Junio C Hamano99dc24a2008-03-27 23:49:5061
Junio C Hamano076ffcc2013-02-06 05:13:2162The underlying Git plumbing tools, such as
Junio C Hamano1aa40d22010-01-21 17:46:4363'git ls-files' and 'git read-tree', read
Junio C Hamano679d22d2007-06-02 21:13:4464`gitignore` patterns specified by command-line options, or from
Junio C Hamano076ffcc2013-02-06 05:13:2165files specified by command-line options. Higher-level Git
Junio C Hamano1aa40d22010-01-21 17:46:4366tools, such as 'git status' and 'git add',
Junio C Hamano679d22d2007-06-02 21:13:4467use patterns from the sources specified above.
68
Junio C Hamano2e6ded82010-12-05 06:20:4869PATTERN FORMAT
70--------------
Junio C Hamano679d22d2007-06-02 21:13:4471
72 - A blank line matches no files, so it can serve as a separator
73 for readability.
74
75 - A line starting with # serves as a comment.
Junio C Hamano6631a092012-10-10 22:50:5776 Put a backslash ("`\`") in front of the first hash for patterns
77 that begin with a hash.
Junio C Hamano679d22d2007-06-02 21:13:4478
Junio C Hamanod701ba32014-11-06 23:18:0579 - Trailing spaces are ignored unless they are quoted with backslash
Junio C Hamano81703b22014-03-14 22:00:2780 ("`\`").
81
Junio C Hamano6631a092012-10-10 22:50:5782 - An optional prefix "`!`" which negates the pattern; any
Junio C Hamano679d22d2007-06-02 21:13:4483 matching file excluded by a previous pattern will become
Junio C Hamano7abb8752016-03-18 22:52:5484 included again. It is not possible to re-include a file if a parent
85 directory of that file is excluded. Git doesn't list excluded
86 directories for performance reasons, so any patterns on contained
87 files have no effect, no matter where they are defined.
Junio C Hamano6631a092012-10-10 22:50:5788 Put a backslash ("`\`") in front of the first "`!`" for patterns
89 that begin with a literal "`!`", for example, "`\!important!.txt`".
Junio C Hamano679d22d2007-06-02 21:13:4490
Junio C Hamanod0a99402019-06-21 18:58:4191 - The slash '/' is used as the directory separator. Separators may
92 occur at the beginning, middle or end of the `.gitignore` search pattern.
Junio C Hamano26e590a2008-02-17 03:53:5193
Junio C Hamanod0a99402019-06-21 18:58:4194 - If there is a separator at the beginning or middle (or both) of the
95 pattern, then the pattern is relative to the directory level of the
96 particular `.gitignore` file itself. Otherwise the pattern may also
97 match at any level below the `.gitignore` level.
Junio C Hamano679d22d2007-06-02 21:13:4498
Junio C Hamanod0a99402019-06-21 18:58:4199 - If there is a separator at the end of the pattern then the pattern
100 will only match directories, otherwise the pattern can match both
101 files and directories.
Junio C Hamanoa574a092010-06-13 19:57:10102
Junio C Hamanod0a99402019-06-21 18:58:41103 - For example, a pattern `doc/frotz/` matches `doc/frotz` directory,
104 but not `a/doc/frotz` directory; however `frotz/` matches `frotz`
105 and `a/frotz` that is a directory (all paths are relative from
106 the `.gitignore` file).
107
108 - An asterisk "`*`" matches anything except a slash.
109 The character "`?`" matches any one character except "`/`".
110 The range notation, e.g. `[a-zA-Z]`, can be used to match
111 one of the characters in a range. See fnmatch(3) and the
112 FNM_PATHNAME flag for a more detailed description.
Junio C Hamano679d22d2007-06-02 21:13:44113
Junio C Hamanoc087f142013-01-10 23:38:50114Two consecutive asterisks ("`**`") in patterns matched against
115full pathname may have special meaning:
116
117 - A leading "`**`" followed by a slash means match in all
118 directories. For example, "`**/foo`" matches file or directory
Junio C Hamano83e0e0b2013-11-08 00:30:52119 "`foo`" anywhere, the same as pattern "`foo`". "`**/foo/bar`"
Junio C Hamanoc087f142013-01-10 23:38:50120 matches file or directory "`bar`" anywhere that is directly
121 under directory "`foo`".
122
Junio C Hamano83e0e0b2013-11-08 00:30:52123 - A trailing "`/**`" matches everything inside. For example,
124 "`abc/**`" matches all files inside directory "`abc`", relative
Junio C Hamanoc087f142013-01-10 23:38:50125 to the location of the `.gitignore` file, with infinite depth.
126
127 - A slash followed by two consecutive asterisks then a slash
128 matches zero or more directories. For example, "`a/**/b`"
129 matches "`a/b`", "`a/x/b`", "`a/x/y/b`" and so on.
130
Junio C Hamano06ce83b2018-11-13 14:06:12131 - Other consecutive asterisks are considered regular asterisks and
132 will match according to the previous rules.
Junio C Hamanoc087f142013-01-10 23:38:50133
Junio C Hamanoe0f71462019-04-09 17:54:45134CONFIGURATION
135-------------
136
137The optional configuration variable `core.excludesFile` indicates a path to a
138file containing patterns of file names to exclude, similar to
139`$GIT_DIR/info/exclude`. Patterns in the exclude file are used in addition to
140those in `$GIT_DIR/info/exclude`.
141
Junio C Hamano2e6ded82010-12-05 06:20:48142NOTES
143-----
144
145The purpose of gitignore files is to ensure that certain files
Junio C Hamano076ffcc2013-02-06 05:13:21146not tracked by Git remain untracked.
Junio C Hamano2e6ded82010-12-05 06:20:48147
Junio C Hamano2e6ded82010-12-05 06:20:48148To stop tracking a file that is currently tracked, use
Junio C Hamano7ab2d202023-05-10 18:59:24149'git rm --cached' to remove the file from the index. The filename
150can then be added to the `.gitignore` file to stop the file from
151being reintroduced in later commits.
Junio C Hamano2e6ded82010-12-05 06:20:48152
Junio C Hamano51a55bc2021-05-12 05:22:19153Git does not follow symbolic links when accessing a `.gitignore` file in
154the working tree. This keeps behavior consistent when the file is
155accessed from the index or a tree versus from the filesystem.
156
Junio C Hamano2e6ded82010-12-05 06:20:48157EXAMPLES
158--------
Junio C Hamano679d22d2007-06-02 21:13:44159
Junio C Hamano19dcd3f2021-10-29 23:20:47160 - The pattern `hello.*` matches any file or directory
Junio C Hamano458281a2021-03-22 21:20:26161 whose name begins with `hello.`. If one wants to restrict
Junio C Hamanod0a99402019-06-21 18:58:41162 this only to the directory and not in its subdirectories,
163 one can prepend the pattern with a slash, i.e. `/hello.*`;
164 the pattern now matches `hello.txt`, `hello.c` but not
165 `a/hello.java`.
166
167 - The pattern `foo/` will match a directory `foo` and
168 paths underneath it, but will not match a regular file
169 or a symbolic link `foo` (this is consistent with the
170 way how pathspec works in general in Git)
171
172 - The pattern `doc/frotz` and `/doc/frotz` have the same effect
173 in any `.gitignore` file. In other words, a leading slash
174 is not relevant if there is already a middle slash in
175 the pattern.
176
177 - The pattern "foo/*", matches "foo/test.json"
178 (a regular file), "foo/bar" (a directory), but it does not match
179 "foo/bar/hello.c" (a regular file), as the asterisk in the
180 pattern does not match "bar/hello.c" which has a slash in it.
181
Junio C Hamano679d22d2007-06-02 21:13:44182--------------------------------------------------------------
Junio C Hamanofce7c7e2008-07-02 03:06:38183 $ git status
Junio C Hamano679d22d2007-06-02 21:13:44184 [...]
185 # Untracked files:
186 [...]
187 # Documentation/foo.html
188 # Documentation/gitignore.html
189 # file.o
190 # lib.a
191 # src/internal.o
192 [...]
193 $ cat .git/info/exclude
194 # ignore objects and archives, anywhere in the tree.
195 *.[oa]
196 $ cat Documentation/.gitignore
197 # ignore generated html files,
198 *.html
199 # except foo.html which is maintained by hand
200 !foo.html
Junio C Hamanofce7c7e2008-07-02 03:06:38201 $ git status
Junio C Hamano679d22d2007-06-02 21:13:44202 [...]
203 # Untracked files:
204 [...]
205 # Documentation/foo.html
206 [...]
207--------------------------------------------------------------
208
209Another example:
210
211--------------------------------------------------------------
212 $ cat .gitignore
213 vmlinux*
214 $ ls arch/foo/kernel/vm*
215 arch/foo/kernel/vmlinux.lds.S
216 $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore
217--------------------------------------------------------------
218
Junio C Hamano076ffcc2013-02-06 05:13:21219The second .gitignore prevents Git from ignoring
Junio C Hamano679d22d2007-06-02 21:13:44220`arch/foo/kernel/vmlinux.lds.S`.
221
Junio C Hamanocf77b042013-12-17 23:54:21222Example to exclude everything except a specific directory `foo/bar`
223(note the `/*` - without the slash, the wildcard would also exclude
224everything within `foo/bar`):
225
226--------------------------------------------------------------
227 $ cat .gitignore
228 # exclude everything except directory foo/bar
229 /*
230 !/foo
231 /foo/*
232 !/foo/bar
233--------------------------------------------------------------
234
Junio C Hamano2e6ded82010-12-05 06:20:48235SEE ALSO
236--------
Junio C Hamano36d52292013-01-25 21:32:36237linkgit:git-rm[1],
Junio C Hamano36d52292013-01-25 21:32:36238linkgit:gitrepository-layout[5],
239linkgit:git-check-ignore[1]
Junio C Hamano2e6ded82010-12-05 06:20:48240
Junio C Hamano679d22d2007-06-02 21:13:44241GIT
242---
Junio C Hamanof7c042d2008-06-06 22:50:53243Part of the linkgit:git[1] suite