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 (up to the top-level of the
27 working tree), with patterns in the higher level files being
28 overridden by those in lower level files down to the directory
29 containing the file. These patterns match relative to the location
30 of the .gitignore file. A project normally includes such .gitignore
31 files in its repository, containing patterns for files generated as
32 part 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 to remove the file from the index. The filename can then be added to
138 the .gitignore file to stop the file from being reintroduced in later
139 commits.
140
141 Git does not follow symbolic links when accessing a .gitignore file in
142 the working tree. This keeps behavior consistent when the file is
143 accessed from the index or a tree versus from the filesystem.
144
146 • The pattern hello.* matches any file or directory whose name
147 begins with hello.. If one wants to restrict this only to the
148 directory and not in its subdirectories, one can prepend the
149 pattern with a slash, i.e. /hello.*; the pattern now matches
150 hello.txt, hello.c but not a/hello.java.
151
152 • The pattern foo/ will match a directory foo and paths underneath
153 it, but will not match a regular file or a symbolic link foo (this
154 is consistent with the way how pathspec works in general in Git)
155
156 • The pattern doc/frotz and /doc/frotz have the same effect in any
157 .gitignore file. In other words, a leading slash is not relevant if
158 there is already a middle slash in the pattern.
159
160 • The pattern foo/*, matches foo/test.json (a regular file), foo/bar
161 (a directory), but it does not match foo/bar/hello.c (a regular
162 file), as the asterisk in the pattern does not match bar/hello.c
163 which has a slash in it.
164
165 $ git status
166 [...]
167 # Untracked files:
168 [...]
169 # Documentation/foo.html
170 # Documentation/gitignore.html
171 # file.o
172 # lib.a
173 # src/internal.o
174 [...]
175 $ cat .git/info/exclude
176 # ignore objects and archives, anywhere in the tree.
177 *.[oa]
178 $ cat Documentation/.gitignore
179 # ignore generated html files,
180 *.html
181 # except foo.html which is maintained by hand
182 !foo.html
183 $ git status
184 [...]
185 # Untracked files:
186 [...]
187 # Documentation/foo.html
188 [...]
189
190 Another example:
191
192 $ cat .gitignore
193 vmlinux*
194 $ ls arch/foo/kernel/vm*
195 arch/foo/kernel/vmlinux.lds.S
196 $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore
197
198 The second .gitignore prevents Git from ignoring
199 arch/foo/kernel/vmlinux.lds.S.
200
201 Example to exclude everything except a specific directory foo/bar (note
202 the /* - without the slash, the wildcard would also exclude everything
203 within foo/bar):
204
205 $ cat .gitignore
206 # exclude everything except directory foo/bar
207 /*
208 !/foo
209 /foo/*
210 !/foo/bar
211
213 git-rm(1), gitrepository-layout(5), git-check-ignore(1)
214
216 Part of the git(1) suite
217
218
219
220Git 2.43.0 11/20/2023 GITIGNORE(5)