Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Files and Formats

The .gitignore file

Reference for .gitignore: where ignore files are read from, precedence between them, what they cannot do for tracked files, and how to debug a rule.

A .gitignore file lists patterns for untracked files that Git should not offer to add. Ignored files do not show up in git status, and git add . skips them. The file is a normal part of the repository, so it is committed and shared with everyone who clones it; it is the right place for build output, dependency folders and editor droppings that every contributor produces.

Ignore rules apply only to files Git does not already track. Once a file has been committed, adding it to .gitignore changes nothing; you must remove it from the index first. The pattern syntax itself is described in Gitignore-style patterns.

GITIGNORE
# dependencies and build output
node_modules/
/dist/
*.log
!important.log

Try it

Bash Ignore rules in action, and tracked files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Why a rule seems not to work

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Each line of the file is a pattern; blank lines and lines starting with # are ignored. Rules are read from several places, from highest to lowest priority:

Source Scope
Patterns given on the command line (e.g. git ls-files -x) That command only.
.gitignore in the same directory as the path, then in each parent up to the top of the working tree Shared, committed. A deeper file overrides a shallower one.
$GIT_DIR/info/exclude This clone only; see .git/info/exclude.
The file named by core.excludesFile (default $XDG_CONFIG_HOME/git/ignore, i.e. ~/.config/git/ignore) All your repositories; see core.excludesFile.

Within one source, the last matching pattern wins, so negation lines (!pattern) must come after the rule they undo.

Errors and edge cases: Git does not report malformed patterns; a rule that matches nothing is silently useless. Files are read as plain text, and a leading UTF-8 byte order mark can break the first pattern.

Usage notes

  • Tracked files are immune. To ignore a file that was already committed, run git rm --cached <file> (or -r for a directory), commit, and keep the rule in .gitignore. The file stays on disk but leaves the index.
  • Check what would be ignored with git status --ignored, and see why with git check-ignore -v <path>. Use --no-index to test paths that are tracked, as in the second example.
  • A file inside an ignored directory cannot be re-included by a negation. Exclude dir/* instead of dir/ if you want exceptions.
  • Empty directories are never tracked, with or without ignore rules. The convention is to add a placeholder file such as .gitkeep and ignore everything else in it with * and !.gitkeep.
  • Forcing an ignored file in: git add -f path adds it anyway, after which it is tracked like any other file.
  • What belongs where. Project artifacts (build output, caches, generated files) go in .gitignore. Personal tool noise (.idea/, .DS_Store) belongs in your global excludes file or .git/info/exclude, so you do not need every project to know about your editor.
  • Line endings and encoding. .gitignore can use LF or CRLF; trailing whitespace in a pattern is stripped unless escaped with a backslash.
  • Nested ignore files are fine and apply only to their directory and below; a pattern with a slash in the middle is anchored to the file that contains it.
  • Cleaning: git clean -X removes only ignored files, -x removes ignored and untracked files; try -n first. See git clean.
  • Sparse and submodule cases: ignore rules do not affect submodules (their content belongs to the other repository), and .gitignore is not what controls a sparse checkout; see git sparse-checkout.