1GITIGNORE(5) Git Manual GITIGNORE(5)
2
3
4
6 gitignore - Specifies intentionally untracked files to ignore
7
9 $XDG_CONFIG_HOME/git/ignore, $GIT_DIR/info/exclude, .gitignore
10
12 A gitignore file specifies intentionally untracked files that Git
13 should ignore. Files already tracked by Git are not affected; see the
14 NOTES below for details.
15
16 Each line in a gitignore file specifies a pattern. When deciding
17 whether to ignore a path, Git normally checks gitignore patterns from
18 multiple sources, with the following order of precedence, from highest
19 to lowest (within one level of precedence, the last matching pattern
20 decides the outcome):
21
22 • Patterns read from the command line for those commands that support
23 them.
24
25 • Patterns read from a .gitignore file in the same directory as the
26 path, or in any parent directory, with patterns in the higher level
27 files (up to the toplevel of the work tree) being overridden by
28 those in lower level files down to the directory containing the
29 file. These patterns match relative to the location of the
30 .gitignore file. A project normally includes such .gitignore files
31 in its repository, containing patterns for files generated as part
32 of the project build.
33
34 • Patterns read from $GIT_DIR/info/exclude.
35
36 • Patterns read from the file specified by the configuration variable
37 core.excludesFile.
38
39 Which file to place a pattern in depends on how the pattern is meant to
40 be used.
41
42 • Patterns which should be version-controlled and distributed to
43 other repositories via clone (i.e., files that all developers will
44 want to ignore) should go into a .gitignore file.
45
46 • Patterns which are specific to a particular repository but which do
47 not need to be shared with other related repositories (e.g.,
48 auxiliary files that live inside the repository but are specific to
49 one user’s workflow) should go into the $GIT_DIR/info/exclude file.
50
51 • Patterns which a user wants Git to ignore in all situations (e.g.,
52 backup or temporary files generated by the user’s editor of choice)
53 generally go into a file specified by core.excludesFile in the
54 user’s ~/.gitconfig. Its default value is
55 $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set
56 or empty, $HOME/.config/git/ignore is used instead.
57
58 The underlying Git plumbing tools, such as git ls-files and git
59 read-tree, read gitignore patterns specified by command-line options,
60 or from files specified by command-line options. Higher-level Git
61 tools, such as git status and git add, use patterns from the sources
62 specified above.
63
65 • A blank line matches no files, so it can serve as a separator for
66 readability.
67
68 • A line starting with # serves as a comment. Put a backslash ("\")
69 in front of the first hash for patterns that begin with a hash.
70
71 • Trailing spaces are ignored unless they are quoted with backslash
72 ("\").
73
74 • An optional prefix "!" which negates the pattern; any matching file
75 excluded by a previous pattern will become included again. It is
76 not possible to re-include a file if a parent directory of that
77 file is excluded. Git doesn’t list excluded directories for
78 performance reasons, so any patterns on contained files have no
79 effect, no matter where they are defined. Put a backslash ("\") in
80 front of the first "!" for patterns that begin with a literal "!",
81 for example, "\!important!.txt".
82
83 • The slash / is used as the directory separator. Separators may
84 occur at the beginning, middle or end of the .gitignore search
85 pattern.
86
87 • If there is a separator at the beginning or middle (or both) of the
88 pattern, then the pattern is relative to the directory level of the
89 particular .gitignore file itself. Otherwise the pattern may also
90 match at any level below the .gitignore level.
91
92 • If there is a separator at the end of the pattern then the pattern
93 will only match directories, otherwise the pattern can match both
94 files and directories.
95
96 • For example, a pattern doc/frotz/ matches doc/frotz directory, but
97 not a/doc/frotz directory; however frotz/ matches frotz and a/frotz
98 that is a directory (all paths are relative from the .gitignore
99 file).
100
101 • An asterisk "*" matches anything except a slash. The character "?"
102 matches any one character except "/". The range notation, e.g.
103 [a-zA-Z], can be used to match one of the characters in a range.
104 See fnmatch(3) and the FNM_PATHNAME flag for a more detailed
105 description.
106
107 Two consecutive asterisks ("**") in patterns matched against full
108 pathname may have special meaning:
109
110 • A leading "**" followed by a slash means match in all directories.
111 For example, "**/foo" matches file or directory "foo" anywhere, the
112 same as pattern "foo". "**/foo/bar" matches file or directory "bar"
113 anywhere that is directly under directory "foo".
114
115 • A trailing "/**" matches everything inside. For example, "abc/**"
116 matches all files inside directory "abc", relative to the location
117 of the .gitignore file, with infinite depth.
118
119 • A slash followed by two consecutive asterisks then a slash matches
120 zero or more directories. For example, "a/**/b" matches "a/b",
121 "a/x/b", "a/x/y/b" and so on.
122
123 • Other consecutive asterisks are considered regular asterisks and
124 will match according to the previous rules.
125
127 The optional configuration variable core.excludesFile indicates a path
128 to a file containing patterns of file names to exclude, similar to
129 $GIT_DIR/info/exclude. Patterns in the exclude file are used in
130 addition to those in $GIT_DIR/info/exclude.
131
133 The purpose of gitignore files is to ensure that certain files not
134 tracked by Git remain untracked.
135
136 To stop tracking a file that is currently tracked, use git rm --cached.
137
139 • The pattern hello.* matches any file or folder whose name begins
140 with hello. If one wants to restrict this only to the directory and
141 not in its subdirectories, one can prepend the pattern with a
142 slash, i.e. /hello.*; the pattern now matches hello.txt, hello.c
143 but not a/hello.java.
144
145 • The pattern foo/ will match a directory foo and paths underneath
146 it, but will not match a regular file or a symbolic link foo (this
147 is consistent with the way how pathspec works in general in Git)
148
149 • The pattern doc/frotz and /doc/frotz have the same effect in any
150 .gitignore file. In other words, a leading slash is not relevant if
151 there is already a middle slash in the pattern.
152
153 • The pattern "foo/*", matches "foo/test.json" (a regular file),
154 "foo/bar" (a directory), but it does not match "foo/bar/hello.c" (a
155 regular file), as the asterisk in the pattern does not match
156 "bar/hello.c" which has a slash in it.
157
158 $ git status
159 [...]
160 # Untracked files:
161 [...]
162 # Documentation/foo.html
163 # Documentation/gitignore.html
164 # file.o
165 # lib.a
166 # src/internal.o
167 [...]
168 $ cat .git/info/exclude
169 # ignore objects and archives, anywhere in the tree.
170 *.[oa]
171 $ cat Documentation/.gitignore
172 # ignore generated html files,
173 *.html
174 # except foo.html which is maintained by hand
175 !foo.html
176 $ git status
177 [...]
178 # Untracked files:
179 [...]
180 # Documentation/foo.html
181 [...]
182
183 Another example:
184
185 $ cat .gitignore
186 vmlinux*
187 $ ls arch/foo/kernel/vm*
188 arch/foo/kernel/vmlinux.lds.S
189 $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore
190
191 The second .gitignore prevents Git from ignoring
192 arch/foo/kernel/vmlinux.lds.S.
193
194 Example to exclude everything except a specific directory foo/bar (note
195 the /* - without the slash, the wildcard would also exclude everything
196 within foo/bar):
197
198 $ cat .gitignore
199 # exclude everything except directory foo/bar
200 /*
201 !/foo
202 /foo/*
203 !/foo/bar
204
206 git-rm(1), gitrepository-layout(5), git-check-ignore(1)
207
209 Part of the git(1) suite
210
211
212
213Git 2.31.1 2021-03-26 GITIGNORE(5)