Pattern Syntax

3.2. Pattern Syntax#

The .gitignore file uses glob patterns to match files and directories. These patterns are similar to, but not exactly the same as, regular expressions.

Pattern scope starts at the directory containing this .gitignore.

Basic Pattern Rules

*.log

Matches names ending with .log beside this .gitignore or in its descendants.

error.log, debug.log, logs/app.log

temp.txt

Matches the name temp.txt beside this .gitignore or in its descendants.

temp.txt ✅, subdir/temp.txt ✅

/build/

Matches a directory named build directly beside this .gitignore, not deeper down.

build/ ✅, src/build/ ❌

build/

Matches directories named build at any depth within this .gitignore’s scope.

build/, src/build/, docs/build/ ✅

Negate a Specific File
*.log
!important.log

Ignores .log names within scope, except names exactly important.log, including in descendants.

Negate a Directory
/*
!/.gitignore
!/src/

Ignores other entries directly beside this .gitignore, but keeps the ignore file itself and files under src/ eligible for adding.

Complex Negation
*.js
!*.test.js
!config.js

Ignores all .js files except test files and config.js