Gitignore Wildcard and Glob Pattern Guide

Master gitignore wildcard syntax including *, **, ?, and character ranges. Learn the difference between directory and file matching with practical examples.

Pattern

Detailed Explanation

Gitignore uses a pattern syntax similar to shell globbing but with important differences. Understanding wildcards is the key to writing precise, maintainable ignore rules that do not accidentally exclude or include the wrong files.

The * wildcard (single level):

* matches any characters except a slash (/). It only operates within a single directory level.

  • *.log — Matches error.log, debug.log in the current directory and all subdirectories.
  • build/* — Matches files directly inside build/ but NOT files in build/sub/dir/.
  • *.py[cod] — Combines with character classes to match .pyc, .pyo, and .pyd files in one pattern.

The ** wildcard (cross-directory):

** matches zero or more directories. It is the most powerful glob pattern available:

  • **/logs — Matches a logs directory anywhere in the tree (logs/, src/logs/, a/b/c/logs/).
  • logs/** — Matches everything inside the logs/ directory at any depth.
  • **/logs/** — Matches everything inside any logs/ directory, located anywhere in the tree.
  • a/**/b — Matches a/b, a/x/b, a/x/y/b, and so on.

The ? wildcard:

? matches exactly one character (except /):

  • file?.txt — Matches file1.txt, fileA.txt but NOT file10.txt.

Character ranges [...]:

  • *.[oa] — Matches .o and .a files (C object files and archives).
  • temp[0-9].log — Matches temp0.log through temp9.log.
  • [Dd]ebug/ — Matches both Debug/ and debug/ for case flexibility.

Trailing slash (directory matching):

A trailing / means the pattern only matches directories:

  • build/ — Ignores the build directory and everything inside it.
  • build (no slash) — Ignores both a file named build AND a directory named build.

Leading slash (root-relative):

A leading / anchors the pattern to the repository root:

  • /build — Only ignores build at the repository root, NOT src/build.
  • build (no leading slash) — Ignores build at any level in the directory tree.

Escaping special characters:

Use backslash to match literal special characters: \# matches a file starting with #, \! matches a file starting with !.

Performance tip: Specific patterns (e.g., /node_modules/) are faster than broad wildcards (e.g., **/node_modules/) because git can skip directory traversal. Use the leading slash when the ignored path is always at the repository root.

Use Case

A developer writing a .gitignore for a monorepo needs to understand how ** patterns work to ignore build directories in all 15 packages without listing each one individually.

Try It — .gitignore Generator

Open full tool