Skip to content
Syntax6 min readVerified Git 2.40+

.gitignore Syntax & Pattern Matching Guide

Comprehensive technical breakdown of pattern formatting, wildcard globbing, leading and trailing slashes, negation nuances, and character ranges in Git ignore files.

1. Basic Formatting: Comments & Blank Lines

Every line in a .gitignore file represents an independent pattern, with three core formatting rules:

  • Blank lines are ignored and serve as visual separators to keep files readable.
  • Lines beginning with # are comments and have no effect on file tracking.
  • Trailing spaces are ignored unless escaped with a backslash (\ ).
gitignore
# Ignore compiled Python bytecode
__pycache__/
*.py[cod]

# Ignore local secret configurations
.env
.env.local

2. Wildcard Globbing (*, ?, [ ])

Git supports standard shell globbing wildcards within a single directory level:

  • * (Asterisk): Matches zero or more characters in a single path segment. *.log matches error.log, but not nested/error.log if anchored.
  • ? (Question Mark): Matches exactly one single character. test?.js matches test1.js, but not test12.js.
  • [abc] (Range): Matches any single character within the bracket set. *.[oa] matches object files ending in .o or archive files in .a.

3. Leading, Trailing, and Middle Slashes

The placement of forward slashes (/) completely alters how Git interprets patterns:

  • Trailing Slash (logs/): Forces the pattern to match only directories. It will ignore a directory named logs/, but will not ignore a regular file named logs.
  • Leading Slash (/config.json): Anchors the match to the root level where .gitignore resides. It ignores /config.json, but allows /packages/app/config.json to remain tracked.
  • Middle Slash (docs/*.html): A slash anywhere in the middle also anchors relative to the directory containing the .gitignore file.

4. Recursive Double Asterisk (**)

The double asterisk matches across multiple directory hierarchies:

  • **/cache: Matches any folder or file named cache anywhere in the repository (cache, src/cache, a/b/c/cache).
  • logs/**: Matches all files and subdirectories inside the logs/ directory, no matter how deeply nested.
  • a/**/b: Matches a/b, a/x/b, a/x/y/b, and so forth.

5. Negation (!) and the Critical Directory Trap

An exclamation mark (!) negates a rule, telling Git to track a file even if an earlier pattern would have ignored it.

The Git Directory Trap (Why !file.txt Fails)

For performance reasons, Git stops traversing directories as soon as a directory is ignored. If a parent directory is excluded with build/, Git will NEVER re-include a file inside it using !build/important.txt!

To properly un-ignore a file inside an ignored folder, you must un-ignore the folder structure while ignoring its contents:

gitignore
# INCORRECT: Will NOT work because parent directory is skipped
node_modules/
!node_modules/my-local-package

# CORRECT: Ignore contents of directory, then whitelist the specific target
node_modules/*
!node_modules/my-local-package/

6. Debugging Rules with git check-ignore

When you aren't sure why a file is being ignored (or why it isn't), use Git's built-in diagnosis command with the verbose (-v) flag:

bash
# Shows the exact .gitignore file and line number responsible
git check-ignore -v src/temp/debug.log

Frequently Asked Questions

Practical answers to common developer questions about .gitignore rules, caching, and repository configuration.

What is the difference between * and ** in .gitignore?

A single asterisk (*) matches characters within a single folder level. A double asterisk (**) matches across arbitrary nested directory hierarchies.

Why doesn't !folder/file.txt work when folder/ is ignored?

Git stops traversing any directory once it is ignored. To whitelist a file inside an ignored folder, ignore the folder contents with 'folder/*' and whitelist the target.

How do I see which rule is ignoring a file?

Run 'git check-ignore -v <filepath>'. Git will print the exact .gitignore file and line number matching that path.

← Read GuidesOpen in Full Generator