| 1 | gitignore(5) |
| 2 | ============ |
| 3 | |
| 4 | NAME |
| 5 | ---- |
| 6 | gitignore - Specifies intentionally untracked files to ignore |
| 7 | |
| 8 | SYNOPSIS |
| 9 | -------- |
| 10 | $XDG_CONFIG_HOME/git/ignore, $GIT_COMMON_DIR/info/exclude, .gitignore |
| 11 | |
| 12 | DESCRIPTION |
| 13 | ----------- |
| 14 | |
| 15 | A `gitignore` file specifies intentionally untracked files that |
| 16 | Git should ignore. |
| 17 | Files already tracked by Git are not affected; see the NOTES |
| 18 | below for details. |
| 19 | |
| 20 | Each line in a `gitignore` file specifies a pattern. |
| 21 | When deciding whether to ignore a path, Git normally checks |
| 22 | `gitignore` patterns from multiple sources, with the following |
| 23 | order of precedence, from highest to lowest (within one level of |
| 24 | precedence, the last matching pattern decides the outcome): |
| 25 | |
| 26 | * Patterns read from the command line for those commands that support |
| 27 | them. |
| 28 | |
| 29 | * Patterns read from a `.gitignore` file in the same directory |
| 30 | 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 |
| 35 | files generated as part of the project build. |
| 36 | |
| 37 | * Patterns read from `$GIT_COMMON_DIR/info/exclude`. |
| 38 | |
| 39 | * Patterns read from the file specified by the configuration |
| 40 | variable `core.excludesFile`. |
| 41 | |
| 42 | Which file to place a pattern in depends on how the pattern is meant to |
| 43 | be 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_COMMON_DIR/info/exclude` file. |
| 54 | |
| 55 | * Patterns which a user wants Git to |
| 56 | 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 |
| 58 | `core.excludesFile` in the user's `~/.gitconfig`. Its default value is |
| 59 | $XDG_CONFIG_HOME/git/ignore. If $XDG_CONFIG_HOME is either not set or |
| 60 | empty, $HOME/.config/git/ignore is used instead. |
| 61 | |
| 62 | The underlying Git plumbing tools, such as |
| 63 | 'git ls-files' and 'git read-tree', read |
| 64 | `gitignore` patterns specified by command-line options, or from |
| 65 | files specified by command-line options. Higher-level Git |
| 66 | tools, such as 'git status' and 'git add', |
| 67 | use patterns from the sources specified above. |
| 68 | |
| 69 | PATTERN FORMAT |
| 70 | -------------- |
| 71 | |
| 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. |
| 76 | Put a backslash ("`\`") in front of the first hash for patterns |
| 77 | that begin with a hash. |
| 78 | |
| 79 | - Trailing spaces are ignored unless they are quoted with backslash |
| 80 | ("`\`"). |
| 81 | |
| 82 | - An optional prefix "`!`" which negates the pattern; any |
| 83 | matching file excluded by a previous pattern will become |
| 84 | 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. |
| 88 | Put a backslash ("`\`") in front of the first "`!`" for patterns |
| 89 | that begin with a literal "`!`", for example, "`\!important!.txt`". |
| 90 | |
| 91 | - The slash "`/`" is used as the directory separator. Separators may |
| 92 | occur at the beginning, middle or end of the `.gitignore` search pattern. |
| 93 | |
| 94 | - 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. |
| 98 | |
| 99 | - Patterns read from exclude sources that are outside the working tree, |
| 100 | such as $GIT_COMMON_DIR/info/exclude and core.excludesFile, are treated as if |
| 101 | they are specified at the root of the working tree, i.e. a leading "/" |
| 102 | in such patterns anchors the match at the root of the repository. |
| 103 | |
| 104 | - If there is a separator at the end of the pattern then the pattern |
| 105 | will only match directories, otherwise the pattern can match both |
| 106 | files and directories. |
| 107 | |
| 108 | - For example, a pattern `doc/frotz/` matches `doc/frotz` directory, |
| 109 | but not `a/doc/frotz` directory; however `frotz/` matches `frotz` |
| 110 | and `a/frotz` that is a directory (all paths are relative from |
| 111 | the `.gitignore` file). |
| 112 | |
| 113 | - An asterisk "`*`" matches anything except a slash. |
| 114 | The character "`?`" matches any one character except "`/`". |
| 115 | The range notation, e.g. `[a-zA-Z]`, can be used to match |
| 116 | one of the characters in a range. See fnmatch(3) and the |
| 117 | FNM_PATHNAME flag for a more detailed description. |
| 118 | |
| 119 | - A backslash ("`\`") can be used to escape any character. E.g., "`\*`" |
| 120 | matches a literal asterisk (and "`\a`" matches "`a`", even though |
| 121 | there is no need for escaping there). As with fnmatch(3), a backslash |
| 122 | at the end of a pattern is an invalid pattern that never matches. |
| 123 | |
| 124 | Two consecutive asterisks ("`**`") in patterns matched against |
| 125 | full pathname may have special meaning: |
| 126 | |
| 127 | - A leading "`**`" followed by a slash means match in all |
| 128 | directories. For example, "`**/foo`" matches file or directory |
| 129 | "`foo`" anywhere, the same as pattern "`foo`". "`**/foo/bar`" |
| 130 | matches file or directory "`bar`" anywhere that is directly |
| 131 | under directory "`foo`". |
| 132 | |
| 133 | - A trailing "`/**`" matches everything inside. For example, |
| 134 | "`abc/**`" matches all files inside directory "`abc`", relative |
| 135 | to the location of the `.gitignore` file, with infinite depth. |
| 136 | |
| 137 | - A slash followed by two consecutive asterisks then a slash |
| 138 | matches zero or more directories. For example, "`a/**/b`" |
| 139 | matches "`a/b`", "`a/x/b`", "`a/x/y/b`" and so on. |
| 140 | |
| 141 | - Other consecutive asterisks are considered regular asterisks and |
| 142 | will match according to the previous rules. |
| 143 | |
| 144 | CONFIGURATION |
| 145 | ------------- |
| 146 | |
| 147 | The optional configuration variable `core.excludesFile` indicates a path to a |
| 148 | file containing patterns of file names to exclude, similar to |
| 149 | `$GIT_COMMON_DIR/info/exclude`. Patterns in the exclude file are used in |
| 150 | addition to those in `$GIT_COMMON_DIR/info/exclude`. |
| 151 | |
| 152 | NOTES |
| 153 | ----- |
| 154 | |
| 155 | The purpose of gitignore files is to ensure that certain files |
| 156 | not tracked by Git remain untracked. |
| 157 | |
| 158 | To stop tracking a file that is currently tracked, use |
| 159 | 'git rm --cached' to remove the file from the index. The filename |
| 160 | can then be added to the `.gitignore` file to stop the file from |
| 161 | being reintroduced in later commits. |
| 162 | |
| 163 | Git does not follow symbolic links when accessing a `.gitignore` file in |
| 164 | the working tree. This keeps behavior consistent when the file is |
| 165 | accessed from the index or a tree versus from the filesystem. |
| 166 | |
| 167 | EXAMPLES |
| 168 | -------- |
| 169 | |
| 170 | - The pattern `hello.*` matches any file or directory |
| 171 | whose name begins with `hello.`. If one wants to restrict |
| 172 | this only to the directory and not in its subdirectories, |
| 173 | one can prepend the pattern with a slash, i.e. `/hello.*`; |
| 174 | the pattern now matches `hello.txt`, `hello.c` but not |
| 175 | `a/hello.java`. |
| 176 | |
| 177 | - The pattern `foo/` will match a directory `foo` and |
| 178 | paths underneath it, but will not match a regular file |
| 179 | or a symbolic link `foo` (this is consistent with the |
| 180 | way how pathspec works in general in Git) |
| 181 | |
| 182 | - The pattern `doc/frotz` and `/doc/frotz` have the same effect |
| 183 | in any `.gitignore` file. In other words, a leading slash |
| 184 | is not relevant if there is already a middle slash in |
| 185 | the pattern. |
| 186 | |
| 187 | - The pattern `foo/*`, matches `foo/test.json` |
| 188 | (a regular file), `foo/bar` (a directory), but it does not match |
| 189 | `foo/bar/hello.c` (a regular file), as the asterisk in the |
| 190 | pattern does not match `bar/hello.c` which has a slash in it. |
| 191 | |
| 192 | -------------------------------------------------------------- |
| 193 | $ git status |
| 194 | [...] |
| 195 | # Untracked files: |
| 196 | [...] |
| 197 | # Documentation/foo.html |
| 198 | # Documentation/gitignore.html |
| 199 | # file.o |
| 200 | # lib.a |
| 201 | # src/internal.o |
| 202 | [...] |
| 203 | $ cat .git/info/exclude |
| 204 | # ignore objects and archives, anywhere in the tree. |
| 205 | *.[oa] |
| 206 | $ cat Documentation/.gitignore |
| 207 | # ignore generated html files, |
| 208 | *.html |
| 209 | # except foo.html which is maintained by hand |
| 210 | !foo.html |
| 211 | $ git status |
| 212 | [...] |
| 213 | # Untracked files: |
| 214 | [...] |
| 215 | # Documentation/foo.html |
| 216 | [...] |
| 217 | -------------------------------------------------------------- |
| 218 | |
| 219 | Another example: |
| 220 | |
| 221 | -------------------------------------------------------------- |
| 222 | $ cat .gitignore |
| 223 | vmlinux* |
| 224 | $ ls arch/foo/kernel/vm* |
| 225 | arch/foo/kernel/vmlinux.lds.S |
| 226 | $ echo '!/vmlinux*' >arch/foo/kernel/.gitignore |
| 227 | -------------------------------------------------------------- |
| 228 | |
| 229 | The second .gitignore prevents Git from ignoring |
| 230 | `arch/foo/kernel/vmlinux.lds.S`. |
| 231 | |
| 232 | Example to exclude everything except a specific directory `foo/bar` |
| 233 | (note the `/*` - without the slash, the wildcard would also exclude |
| 234 | everything within `foo/bar`): |
| 235 | |
| 236 | -------------------------------------------------------------- |
| 237 | $ cat .gitignore |
| 238 | # exclude everything except directory foo/bar |
| 239 | /* |
| 240 | !/foo |
| 241 | /foo/* |
| 242 | !/foo/bar |
| 243 | -------------------------------------------------------------- |
| 244 | |
| 245 | SEE ALSO |
| 246 | -------- |
| 247 | linkgit:git-rm[1], |
| 248 | linkgit:gitrepository-layout[5], |
| 249 | linkgit:git-check-ignore[1] |
| 250 | |
| 251 | GIT |
| 252 | --- |
| 253 | Part of the linkgit:git[1] suite |