Gitignore Format: Wildcards, Negation and Comments
November 11, 2026 · 9 min read
Gitignore Format and Syntax: Wildcards, Negation and Comments
The gitignore format is one pattern per line in a plain-text file that tells Git which untracked files to leave alone. Each line is a glob such as *.log or build/, a ! exception, or a # comment. Below you'll find every rule, a match table for each, and the commands that show which line won.
What Does a .gitignore File Do?
A .gitignore file lists patterns for files Git shouldn't track: build output, dependency folders, logs, editor settings and local secrets. When a path matches, git status stops listing it as untracked and git add . skips it. In short, that's how gitignore works: it filters what Git treats as untracked.
It never touches files Git already tracks. Ignoring a committed file needs one extra step, covered in the debugging section below. Most repos keep one .gitignore at the root, committed next to the README, and add more in subdirectories when a folder needs its own rules. Our README guide covers the other file every new repo starts with.
You rarely write one from scratch. Our .gitignore generator assembles a starter file in your browser from templates for your languages, frameworks, operating system and editor. GitHub keeps a larger template collection too. For a Python project, our Python gitignore file is copy-ready, with each line explained.
The Gitignore Format Line by Line: Blank Lines, Spaces and Escapes
The gitignore manual defines a short list of line rules. Everything else in the gitignore syntax builds on these:
- One pattern per line. Git reads the file top to bottom.
- Blank lines match nothing. Use them to separate groups.
- Trailing spaces are dropped unless you escape the last one with a backslash.
- A backslash escapes any character.
\*matches a literal asterisk. - A backslash at the very end makes an invalid pattern that never matches.
Here's a small file that uses each rule:
# Build output
dist/
*.min.js
# A file whose name really ends in a space
notes.txt\
# A file literally named *starred*.md
\*starred\*.md
Git ignores the dist directory, any file ending in .min.js, the file notes.txt with its trailing space, and a file named *starred*.md. The blank lines and comment lines change nothing.
How Do Gitignore Wildcards Work?
There are four gitignore wildcards. A single * matches anything except a slash, so it never crosses into a subfolder. ? matches exactly one character except a slash. Square brackets like [0-9] or [a-zA-Z] match one character from a range.
The fourth, **, is the only gitignore wildcard that crosses directories, and it has three forms:
**/foomatchesfooin any directory. It's the same as plainfoo.logs/**matches everything insidelogs, at any depth.a/**/bmatches zero or more directories in between:a/b,a/x/b,a/x/y/b.
Any other run of asterisks acts like a single *. We tested every row below with git check-ignore against a root-level .gitignore:
| Pattern | Matches | Doesn't match |
|---|---|---|
*.log | debug.log, logs/a.log | debug.log.bak |
debug?.log | debug1.log, debugA.log | debug.log, debug10.log |
log[0-9].txt | log1.txt | logA.txt, log10.txt |
docs/*.pdf | docs/a.pdf | docs/api/b.pdf (one level down) |
logs/** | logs/a.log, logs/2026/b.log | src/logs/x.txt |
a/**/b | a/b, a/x/y/b | x/a/b |
a/**/*.tmp | a/z.tmp, a/b/c/z.tmp | z.tmp |
The docs/*.pdf row trips people up most often. The * stops at the slash, so PDFs in docs/api/ stay tracked. Write docs/**/*.pdf if you want every level.
Slashes Change Everything: Anchoring and Directory-Only Patterns
Where a slash sits in a pattern decides what it can match. This part of the gitignore format causes more surprises than the wildcards do.
- No slash (
build): matches a file or directory with that name at any depth below the.gitignore. - Trailing slash (
build/): matches directories only, never a plain file with that name. - Leading or middle slash (
/build,src/build/): the pattern is anchored to the folder that holds this.gitignore.
| Pattern | Matches | Doesn't match |
|---|---|---|
build | build, src/build (file or directory) | builds, build.log |
build/ | directories build and src/build | a file named tmp/build |
/build | build at the root | src/build |
src/build/ | directory src/build | lib/src/build |
A middle slash anchors on its own, so doc/frotz and /doc/frotz behave the same. Patterns in .git/info/exclude and your global ignore file act as if they sat at the repository root.
How Do Gitignore Exceptions Work With the ! Prefix?
A line starting with ! re-includes a path that an earlier pattern excluded. That's how you write gitignore exceptions. Order matters, because the last matching line wins:
*.log
!keep.log
Git now ignores every .log file except keep.log. Swap the two lines and the exception does nothing, because *.log matches later and excludes it again.
The trap is directories. Git can't re-include a file whose parent directory is excluded, since it never looks inside an excluded directory. This gitignore exception fails:
logs/
!logs/keep.log
This one works, because logs/* ignores the contents rather than the folder itself:
logs/*
!logs/keep.log
The manual's example for keeping only one nested folder uses the same idea at each level:
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
To ignore a file whose name really starts with !, escape it: \!important.txt.
Gitignore Comments: # Lines and Literal Hashes
Gitignore comments are simple. A line is a comment only when # is its first character. To match a file whose name begins with a hash, escape it as \#notes.txt.
There's no inline comment syntax. In *.log # logs, everything after the pattern becomes part of it. Git looks for files whose names end in .log # logs and ignores nothing you meant to ignore. Put each gitignore comment on its own line instead:
# Dependencies
node_modules/
# Local environment, never commit
.env
.env.local
We prefer a short comment header above each group, as above. It costs one line and tells the next reader why a rule exists before they delete it. If your team also documents repo conventions in a docs folder, our guide to writing documentation in Markdown covers where that lives.
Which Rule Wins? Precedence Across .gitignore, info/exclude and Global Files
Git reads ignore patterns from four sources. From highest priority to lowest:
- Patterns passed on the command line, for commands that accept them.
.gitignorefiles in the path's own directory and each parent, up to the repo root. A deeper file overrides a higher one..git/info/exclude, which applies to your clone only and is never committed.- Your global ignore file, set by
core.excludesFile. When that's unset, Git uses~/.config/git/ignore, or the same path underXDG_CONFIG_HOMEif you've set that variable.
Within one level, the last matching pattern decides the outcome. That's why a ! line in src/.gitignore can rescue a file that the root .gitignore excludes, as long as no parent directory of that file is excluded.
Keep shared project rules in the committed .gitignore, and send personal clutter like .DS_Store to your global file or, for one clone only, to .git/info/exclude. GitHub's docs on ignoring files describe the same split.
Why Is .gitignore Not Ignoring a File?
Most often, the file is already tracked. Ignore rules only apply to untracked files, so adding a pattern for a committed file changes nothing. Remove it from the index, keep it on disk, and commit:
git rm --cached config/local.yml
git commit -m "Stop tracking local config"
For anything else, ask Git which line matched with git check-ignore:
git check-ignore -v logs/app.log
.gitignore:3:logs/* logs/app.log
The output is the source file, the line number and the pattern, then a tab and the path. Exit status 0 means at least one path is ignored, and 1 means none are. By default check-ignore skips tracked files, so add --no-index to test a pattern against a file that's already committed. To see everything Git currently ignores, run git status --ignored.
Here's the limitation. Git won't explain why a pattern didn't match. No output simply means no rule applied, so you compare the path against the tables above yourself. Also watch for a ! pattern in the -v output. It means an exception matched last, so the file is not ignored. In our tests the exit status was still 0.
Quick Answers on Case, Placement, Order and Tracked Files
The gitignore format comes down to three ideas. Wildcards stop at slashes unless you use **, a slash anchors or restricts a pattern, and the last match wins. Keep git check-ignore -v handy and most mysteries take a minute. To start a new repo, build a starter file with the .gitignore generator, then write its README in the free online editor with a live preview.