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