Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Revisions and Pathspecs

Gitignore-style patterns

Reference for the pattern language shared by .gitignore, info/exclude and sparse-checkout: wildcards, **, anchoring with a slash, directory-only rules and negation.

The same small pattern language is used in .gitignore, .git/info/exclude, the global excludes file, .gitattributes (with small differences) and non-cone sparse-checkout. Each line is one pattern. Git compares it with a path relative to the directory that holds the file, and the last matching pattern wins.

The rules are different from shell globbing in a few places that cause most mistakes: a slash in the middle of a pattern anchors it, a trailing slash restricts it to directories, ** crosses directory levels, and a leading ! negates, but cannot bring back a file whose parent directory is excluded.

GITIGNORE
*.log
/build/
docs/**/*.tmp
!keep.log

Try it

Bash Anchoring, directories and **

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 The negation trap

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

Pattern Matches
blank line Nothing; separates groups.
# text A comment. Escape a literal leading # as \#.
name A file or directory called name at any depth below the ignore file.
name/ Only a directory called name (and everything inside it).
/name Only at the level of the ignore file (a leading slash anchors).
dir/name A slash in the middle also anchors the pattern to the ignore file's directory.
* Any characters except /.
? One character except /.
[abc], [a-z] A character from the set or range. [!a] or [^a] negates.
**/name name at any depth (same as plain name).
name/** Everything inside name.
a/**/b a/b, a/x/b, a/x/y/b: zero or more directories between.
!pattern Re-include paths matched by an earlier pattern.
\!, \#, \ Escapes for a literal !, # or space.

Details: trailing spaces are ignored unless escaped with a backslash. Other characters have no special meaning. Patterns are matched case-sensitively unless core.ignoreCase is set.

Usage notes

  • Last match wins, and order matters. *.log followed by !keep.log keeps keep.log; reversed, the negation is overridden by *.log.
  • You cannot re-include inside an excluded directory. Git does not even look into vendor/, so !vendor/lib/a.txt has no effect. Exclude vendor/* (the contents) and then re-include a subdirectory, as in the second example.
  • A slash anywhere except the end anchors the pattern. doc/frotz matches doc/frotz in the ignore file's folder only. frotz/ matches a directory named frotz anywhere.
  • Trailing /** and / differ slightly. build/ ignores the directory as a whole; build/** ignores its content but the directory itself still counts as a match target for negation rules.
  • Precedence between files: command-line patterns, then .gitignore files from the deepest directory upwards, then .git/info/exclude, then core.excludesFile. A deeper file overrides a shallower one.
  • Tracked files are never ignored. Patterns only affect untracked paths; to stop tracking, use git rm --cached. Details are in .gitignore.
  • Debug with git check-ignore -v, which prints the file, line number and pattern that decided: git check-ignore.
  • Used elsewhere with changes: .gitattributes patterns have no negation and no ** inside a path segment ambiguity; non-cone sparse-checkout uses this syntax to include files, and cone mode uses plain directory names instead (see sparse-checkout file).
  • Not the pathspec language. In pathspecs * crosses slashes by default; see Pathspec.