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 · If the pattern ends with a slash, it is removed for the purpose of
84 the following description, but it would only find a match with a
85 directory. In other words, foo/ will match a directory foo and
86 paths underneath it, but will not match a regular file or a
87 symbolic link foo (this is consistent with the way how pathspec
88 works in general in Git).
89
90 · If the pattern does not contain a slash /, Git treats it as a shell
91 glob pattern and checks for a match against the pathname relative
92 to the location of the .gitignore file (relative to the toplevel of
93 the work tree if not from a .gitignore file).
94
95 · Otherwise, Git treats the pattern as a shell glob: "*" matches
96 anything except "/", "?" matches any one character except "/" and
97 "[]" matches one character in a selected range. See fnmatch(3) and
98 the FNM_PATHNAME flag for a more detailed description.
99
100 · A leading slash matches the beginning of the pathname. For example,
101 "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
102
103 Two consecutive asterisks ("**") in patterns matched against full
104 pathname may have special meaning:
105
106 · A leading "**" followed by a slash means match in all directories.
107 For example, "**/foo" matches file or directory "foo" anywhere, the
108 same as pattern "foo". "**/foo/bar" matches file or directory "bar"
109 anywhere that is directly under directory "foo".
110
111 · A trailing "/**" matches everything inside. For example, "abc/**"
112 matches all files inside directory "abc", relative to the location
113 of the .gitignore file, with infinite depth.
114
115 · A slash followed by two consecutive asterisks then a slash matches
116 zero or more directories. For example, "a/**/b" matches "a/b",
117 "a/x/b", "a/x/y/b" and so on.
118
119 · Other consecutive asterisks are considered regular asterisks and
120 will match according to the previous rules.
121
123 The purpose of gitignore files is to ensure that certain files not
124 tracked by Git remain untracked.
125
126 To stop tracking a file that is currently tracked, use git rm --cached.
127
129 $ git status
130 [...]
131 # Untracked files:
132 [...]
133 # Documentation/foo.html
134 # Documentation/gitignore.html
135 # file.o
136 # lib.a
137 # src/internal.o
138 [...]
139 $ cat .git/info/exclude
140 # ignore objects and archives, anywhere in the tree.
141 *.[oa]
142 $ cat Documentation/.gitignore
143 # ignore generated html files,
144 *.html
145 # except foo.html which is maintained by hand
146 !foo.html
147 $ git status
148 [...]
149 # Untracked files:
150 [...]
151 # Documentation/foo.html
152 [...]
153
154
155 Another example:
156
157 $ cat .gitignore
158 vmlinux*
159 $ ls arch/foo/kernel/vm*
160 arch/foo/kernel/vmlinux.lds.S
161 $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore
162
163
164 The second .gitignore prevents Git from ignoring
165 arch/foo/kernel/vmlinux.lds.S.
166
167 Example to exclude everything except a specific directory foo/bar (note
168 the /* - without the slash, the wildcard would also exclude everything
169 within foo/bar):
170
171 $ cat .gitignore
172 # exclude everything except directory foo/bar
173 /*
174 !/foo
175 /foo/*
176 !/foo/bar
177
178
180 git-rm(1), gitrepository-layout(5), git-check-ignore(1)
181
183 Part of the git(1) suite
184
185
186
187Git 2.20.1 12/15/2018 GITIGNORE(5)