.gitignore Patterns
Comprehensive guide to .gitignore syntax, pattern matching rules, global ignores, negation, and curated patterns for every major tech stack and framework.
`.gitignore` patterns keep untracked build artifacts and local files out of ordinary `git add` operations. A tracked file remains tracked until you remove it from the index. Ignore sources have an order of precedence: applicable `.gitignore` files override parent rules, followed by repository-local and global excludes. Use `git check-ignore -v` to find which pattern matches, and use a nested negation carefully because Git cannot re-include a file beneath an excluded parent directory.
Introduction
The .gitignore file is Git’s first line of defense against committing the wrong things. Build artifacts, dependency directories, IDE configurations, environment files with secrets, OS metadata — all of these belong in .gitignore, not in your repository.
Yet .gitignore syntax is deceptively simple. Many developers struggle with patterns that don’t work as expected, files that should be ignored but aren’t, and the mysterious behavior of negation patterns. Understanding how Git matches ignore patterns is essential for maintaining clean repositories.
This comprehensive guide covers every aspect of .gitignore: the pattern syntax, precedence rules, global ignores, negation, and battle-tested patterns for every major technology stack.
When to Use / When Not to Use
Use .gitignore for:
- Excluding build artifacts and generated files
- Preventing secrets and environment files from being committed
- Ignoring OS-specific files (
.DS_Store,Thumbs.db) - Excluding IDE/editor configuration that’s user-specific
- Keeping repository size manageable
Do not use it for:
- Files that should be tracked but are large (use Git LFS instead)
- Hiding mistakes (committing then ignoring doesn’t remove from history)
- Files that every developer needs (those should be committed)
Core Concepts
Ignore sources have a priority order, so “last match wins” is not a rule across every file. Command-line patterns supported by a command have the highest priority. Applicable .gitignore files come next; a more local file can override rules from a parent. Then Git checks .git/info/exclude, followed by the global core.excludesFile. Within the applicable .gitignore level, the last matching pattern decides whether a path is ignored.
graph TD
PRIORITY["Higher priority"] --> CLI["1. Command-line patterns (where supported)"]
CLI --> IGNORE_FILES["2. Applicable .gitignore files\nMore local rules override parent rules"]
IGNORE_FILES --> INFO["3. .git/info/exclude"]
INFO --> GLOBAL["4. core.excludesFile (global)"]
FORCE["git add -f path"] -. "bypasses ignore check" .-> STAGED["Explicitly stage an ignored path"]
Architecture or Flow Diagram
flowchart TD
START["git add file.py"] --> TRACKED{"Already tracked?"}
TRACKED -->|yes| ADD["Added to staging\n(ignores .gitignore)"]
TRACKED -->|no| CHECK["Check ignore patterns"]
CHECK -->|pattern matches| IGNORED{"Negated pattern?"}
IGNORED -->|yes| ADD
IGNORED -->|no| SKIP["Skipped (ignored)"]
CHECK -->|no match| ADD
FORCE["git add -f file.py"] --> ADD
.gitignore affects untracked files. It does not untrack a path already in the index. To stop tracking a file while keeping the working copy, use git rm --cached <file> and commit that index change. git add -f <file> can explicitly add a path that an ignore rule would otherwise exclude.
Step-by-Step Guide
Pattern Syntax
| Pattern | Matches | Example |
|---|---|---|
*.log |
Any file ending in .log |
debug.log, app/error.log |
build/ |
Directory named build |
build/, src/build/ |
/dist |
dist only in repo root |
/dist but not src/dist |
**/logs |
logs in any directory |
logs, a/logs, a/b/logs |
doc/*.txt |
.txt files directly in doc/ |
doc/readme.txt but not doc/api/v1.txt |
doc/**/*.txt |
.txt files in doc/ or subdirs |
doc/readme.txt, doc/api/v1.txt |
!important.log |
Negation — un-ignore | Overrides a previous *.log |
temp/ |
Directory and all contents | temp/, temp/file.txt, temp/sub/ |
*.class |
All .class files anywhere |
Anywhere in the repository |
The Trailing Slash Rule
A trailing slash means “directory only”:
# Matches directories named "build"
build/
# Does NOT match a file named "build"
# Use "build" (no slash) to match both
Negation Patterns
Negation (!) can re-include a path matched by an earlier rule, but it cannot re-include a file while one of its parent directories remains excluded. For a nested folder, leave the parent directory traversable, then re-include the directory and the file you want:
/build/*
!/build/keep/
/build/keep/*
!/build/keep/README.md
This ignores the contents of build/ except build/keep/README.md. Order matters: a later rule can override an earlier match at the same applicable level. Use git check-ignore -v build/keep/README.md to see which rule matched; the -v output includes the source file and line.
Global .gitignore
For patterns that apply to ALL repositories:
# Create global gitignore
cat > ~/.gitignore_global << 'EOF'
# OS files
.DS_Store
Thumbs.db
Desktop.ini
# Editor files
*.swp
*~
.project
.idea/
.vscode/
# Build artifacts
node_modules/
__pycache__/
EOF
# Configure Git to use it
git config --global core.excludesFile ~/.gitignore_global
Debugging .gitignore
# Check why a file is ignored
git check-ignore -v path/to/file
# Check multiple files
git check-ignore -v file1 file2 file3
# Test a pattern without creating files
git check-ignore -v --stdin << EOF
build/output.js
src/build/output.js
EOF
Production Failure Scenarios
| Scenario | Symptoms | Mitigation |
|---|---|---|
| Tracked file won’t ignore | File still appears in git status |
git rm --cached <file> then commit |
| Negation doesn’t work | !pattern has no effect |
Ensure broader pattern comes BEFORE negation |
| Global ignore not working | OS files still show up | Verify core.excludesFile path: git config core.excludesFile |
| Pattern too broad | *.log ignores wanted files |
Use more specific paths: /logs/*.log |
| Case sensitivity issues | .DS_Store vs .ds_store |
Use case-insensitive patterns or multiple entries |
Trade-off Analysis
| Aspect | Advantage | Disadvantage |
|---|---|---|
| Per-directory .gitignore | Patterns close to what they ignore | Multiple files to maintain |
| Global .gitignore | One-time setup for all repos | May conflict with project needs |
| Negation patterns | Fine-grained control | Order-dependent, confusing |
| Tracked file exemption | Don’t accidentally ignore committed files | Requires git rm --cached to fix |
Implementation Snippets
# === Node.js / JavaScript ===
node_modules/
dist/
build/
.env
.env.local
.env.*.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.npm
.eslintcache
coverage/
.nyc_output/
# === Python ===
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
venv/
.venv/
env/
env.bak/
pip-log.txt
pip-delete-this-directory.txt
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.log
.mypy_cache/
.pytest_cache/
# === Java / Kotlin ===
*.class
*.jar
*.war
*.ear
*.nar
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
.gradle/
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
.out/
.classpath
.project
.settings/
# === Go ===
*.exe
*.exe~
*.dll
*.so
*.dylib
*.test
*.out
vendor/
go.sum
# === Rust ===
/target/
**/*.rs.bk
Cargo.lock
# === Universal ===
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
*.log
*.tmp
*.swp
*~
Observability Checklist
- Monitor: Repository size for accidentally committed large files
- Verify: Run
git check-ignore -von new file types - Audit: Periodically review
.gitignorefor outdated patterns - Track: CI/CD failures caused by missing ignored files
Security & Compliance Considerations
.gitignoredoes NOT prevent secrets from being committed — it’s a convenience, not a security control- Use pre-commit hooks (like
detect-secrets) for actual secret prevention - Environment files (
.env) should always be ignored - See Git Secrets Management for comprehensive secret prevention
Common Pitfalls / Anti-Patterns
- Ignoring already-tracked files —
.gitignoreonly affects untracked files - Putting negation before the pattern it negates — order matters
- Using
*instead of**—\*doesn’t match directory separators - Not ignoring lock files consistently — some teams commit
package-lock.json, others don’t; pick one - Over-ignoring — ignoring files that teammates need to reproduce builds
Quick Recap Checklist
.gitignoreonly affects untracked files- More local
.gitignorefiles override parent files; within a file, the last matching pattern wins - Trailing slash means directory only
/at start anchors to the directory containing the.gitignore**matches any number of directories- Use
git check-ignore -vto debug - Global
.gitignorefor OS/editor files - Already-tracked files need
git rm --cachedto stop tracking
Ignore-source precedence
The same order applies when several ignore sources define patterns for one path:
graph TD
PRIORITY["Higher priority"] --> CLI["1. Command-line patterns (where supported)"]
CLI --> LOCAL["2. Applicable .gitignore files\nChild directories can override parent rules"]
LOCAL --> EXCLUDE["3. .git/info/exclude"]
EXCLUDE --> GLOBAL["4. core.excludesFile (global)"]
FORCE["git add -f path"] -. "bypasses ignore check" .-> STAGE["Explicitly stage an ignored path"]
git add -f bypasses the ignore check for that add operation; it is not another ignore-pattern source.
Production Failure: Ignoring Critical Files
Scenario: Committing secrets and ignoring migrations
# === Problem 1: Accidentally committing secrets ===
$ git add .
$ git commit -m "Add config"
# Oops — .env with API keys was NOT in .gitignore!
# Prevention:
# Add to .gitignore BEFORE first commit
echo ".env" >> .gitignore
echo ".env.*" >> .gitignore
# If already committed, see "Removing Sensitive Data from History"
# === Problem 2: Ignoring migration files ===
$ cat .gitignore
*.sql # Too broad! Ignores database migrations
# Fix: Be specific
migrations/*.sql # Only ignore generated migrations
!migrations/001_initial.sql # Keep hand-written ones
# === Problem 3: Ignoring build output that should be committed ===
$ cat .gitignore
dist/ # But this is a library that ships compiled output!
# Fix: Use .gitattributes or a more targeted pattern
dist/*.map # Ignore source maps only
dist/*.min.js # Keep minified files
# === Debugging what's ignored ===
# Check which rule is ignoring a file
git check-ignore -v path/to/file
# Output: .gitignore:5:*.log path/to/file.log
# Check if a tracked file is being ignored (it won't be!)
git ls-files --cached | grep -f <(git check-ignore -v *)
# Tracked files are NEVER affected by .gitignore
Trade-offs: .gitignore vs .gitattributes vs Sparse Checkout
| Aspect | .gitignore | .gitattributes | Sparse Checkout |
|---|---|---|---|
| Purpose | Exclude files from tracking | Define file handling rules | Partial working tree |
| Scope | Untracked files only | All files (tracked + untracked) | Working directory only |
| Effect | Files not staged | Line endings, merge strategy, LFS | Files not checked out |
| Versioned | Yes (committed to repo) | Yes (committed to repo) | Local config only |
| Negation | Supported (!pattern) |
Not applicable | Supported |
| Common use | node_modules/, .env, build/ | eol=lf, *.png binary | Monorepo subdirectories |
| Team impact | Shared across team | Shared across team | Per-developer |
| Security | NOT a security control | NOT a security control | NOT a security control |
Key insight: .gitignore prevents files from being tracked. .gitattributes controls how tracked files are handled. Sparse checkout controls which tracked files appear in your working directory.
Quick Recap: .gitignore Audit by Stack
# === Node.js / JavaScript ===
# Must ignore:
node_modules/
dist/
.env
.env.local
.env.*.local
.npm
.eslintcache
coverage/
# === Python ===
# Must ignore:
__pycache__/
*.pyc
*.pyo
.venv/
venv/
.eggs/
*.egg-info/
.pytest_cache/
.mypy_cache/
# === Java / Kotlin ===
# Must ignore:
target/
build/
*.class
*.jar
*.war
.gradle/
.idea/
*.iml
# === Go ===
# Must ignore:
vendor/ # (if not using Go modules)
*.exe
*.test
coverage.out
# === Rust ===
# Must ignore:
target/
**/*.rs.bk
# Note: Cargo.lock is typically committed for binaries, ignored for libraries
# === Universal (all projects) ===
# Must ignore:
.DS_Store
Thumbs.db
*.log
*.tmp
*.swp
*~
.vscode/settings.json # Personal settings
.idea/workspace.xml # Personal workspace
Interview Questions
The file is already tracked by Git. .gitignore only affects untracked files. To stop tracking it, run git rm --cached <file> and commit. The file will be removed from the repository but remain in your working directory, and .gitignore will then prevent it from being re-added.
*.log matches any file ending in .log anywhere in the repository (Git implicitly searches all directories). /**/*.log explicitly matches .log files in any subdirectory from the location of the .gitignore file. In practice, they behave identically, but ** is useful when combined with other path components like logs/**/*.log.
Use a broad ignore followed by negation: *\n!*.js\n!*.ts\n. This ignores all files except .js and .ts files. For directories, you need to negate the directory too: *\n!src/\nsrc/*\n!src/**/*.ts\n. The key insight is that you must negate each directory level to reach the files inside.
Yes, patterns without a leading / apply recursively. *.log in the root .gitignore ignores .log files in all subdirectories. Patterns with a leading / are anchored to the directory containing the .gitignore file. You can also place .gitignore files in subdirectories for localized rules.
git check-ignore -v <path> shows which pattern is ignoring a file and from which line. The verbose output shows the .gitignore file path, line number, and the pattern that matched. This is the primary tool for troubleshooting why a file is or isn't being ignored.
.gitignore is committed to the repository and shared with the team. .git/info/exclude is local-only (not versioned) and applies only to your working copy. Use .gitignore for project-wide rules; use exclude for personal files you never want tracked.
No — once a file is tracked, .gitignore has no effect on it. You must explicitly untrack it with git rm --cached <file>, then commit the removal. The file stays in your working directory but Git stops monitoring it. Only then will .gitignore prevent re-addition.
A leading slash / anchors the pattern to the directory containing the .gitignore file. For example, /dist matches dist/ only in the repository root, not src/dist. Without the slash, dist matches dist in any directory.
The negation operator ! re-includes a file that was previously ignored. However, it only works if a broader pattern has already matched the file. Negation patterns must appear after the pattern they override. If a negation pattern matches a file that was never ignored by a preceding pattern, it has no effect.
A trailing slash / means the pattern matches only directories, not files. For example, build/ ignores the build directory and all its contents, but does not ignore a file named build. Without the trailing slash, build matches both files and directories named build.
** matches any number of directories, including zero. *.log matches files in any directory (implicit recursive search). logs/**/*.log matches .log files inside logs/ and any subdirectory of logs/. The pattern **/ at the start matches from the repository root; /** at the end matches everything after the anchored path.
Use a broad ignore followed by a negation: *.log then !important.log. The broader pattern ignores all .log files, and the negation re-includes important.log. For directories, you must negate each level: *\n!logs/\nlogs/*\n!logs/important.log.
core.excludesFile points to a user-wide Git ignore file (typically ~/.gitignore_global) that applies to all repositories on your machine. Configure it with: git config --global core.excludesFile ~/.gitignore_global. This is where you put OS-specific files (e.g., .DS_Store) and editor artifacts that should be ignored everywhere.
They serve different purposes: .gitignore prevents files from being tracked; .gitattributes controls how tracked files are handled (line endings, merge strategy, binary detection). A file ignored by .gitignore is never staged. A file managed by .gitattributes can still be ignored if it is also listed in .gitignore, but the attributes apply once the file is tracked by other means.
No — .gitignore uses glob patterns, not regex. The special characters are: * matches any sequence of non-slash characters; ** matches any sequence including slashes; ? matches any single non-slash character; [abc] matches any single character in the set. For advanced filtering, use pre-commit hooks or git check-ignore --stdin with external tools.
.gitignore is a convenience feature, not a security control. It only prevents untracked files from being staged. If a secret file is accidentally committed before being ignored, it remains in Git history forever. Use pre-commit hooks (e.g., detect-secrets, git-secret) or secret scanning tools to actively prevent secrets from entering the repository.
Git reads all applicable .gitignore files from the repository root down to the file's directory, and each one takes precedence over parent directories. Patterns in a subdirectory's .gitignore override conflicting patterns in parent .gitignore files. This allows localized ignore rules that don't affect the rest of the project.
.gitignore prevents files from being staged; sparse checkout controls which parts of the working tree are checked out at all. With sparse checkout, files exist in the repository but are not present in your local working directory. This is useful in monorepos where you only need a subset of packages.
Command-line patterns supported by a command have the highest priority. Applicable .gitignore files come next, with a more local file able to override parent rules. Then Git checks .git/info/exclude and the global core.excludesFile. Within an applicable .gitignore level, the last matching pattern decides the outcome. git add -f explicitly stages an ignored path; it is not an ignore source.
It depends on the ecosystem. For npm/yarn, package-lock.json is typically committed because it ensures deterministic installs across machines. For Cargo (Rust), Cargo.lock should be committed for binaries but ignored for libraries. The key is establishing a team convention and documenting it. Inconsistent lock file handling causes merge conflicts and unpredictable builds.
Further Reading
Additional Resources
The official Git documentation at git-scm.com/docs/gitignore is the definitive reference for pattern syntax. When a pattern behaves unexpectedly, the official docs are where to start.
The Gitignore Template Collection at github.com/github/gitignore has templates for over 500 languages and frameworks. Rather than writing Python ignores from scratch, you can grab the Python template which handles venv/, .pytest_cache/, and *.pyc in one pass. The Ruby template does the same for .bundle/, vendor/bundle/, and Gemfile.lock. Grab the template, paste it in, move on.
The Pro Git book explains how ignore sources and directory-local rules interact. Its examples are useful when a pattern appears to match but a nested .gitignore changes the outcome. The Atlassian tutorial provides another visual explanation.
GitHub’s help article is useful if you use GitHub Actions or Git LFS. It clarifies how .gitignore interacts with the web interface and git clone behavior.
- Gitignore Documentation — Official Git documentation on pattern format and behavior
- Gitignore Template Collection — Community-maintained templates for 500+ languages and frameworks
- Git Book: Ignoring Files — Official Pro Git book section on ignores
- Atlassian: .gitignore Tutorial — Visual walkthrough with examples
- GitHub Help: Ignoring Files — GitHub-specific patterns and repository setup
Related Posts
The Git Objects post explains how Git’s internal storage works. Blobs, trees, commits, and tags — once you see how these fit together, git rm –cached and git filter-repo behavior becomes obvious. It also explains why .gitignore only applies to untracked files, not ones you’ve already committed.
For a read-only review of staged and unstaged changes, see git status and diff. For credential scanning, see Git secrets management.
Git Secrets Management covers the layer that sits on top of .gitignore. Pre-commit hooks like gitleaks scan for credentials before they hit history. A solid .gitignore handles the obvious cases, but hooks catch what slips through.
GitHub Flow Branching Strategy connects .gitignore to your overall branching approach. Your branching structure affects which ignores apply where and whether your main branch stays clean.
- Git Objects: Blobs, Trees, Commits, Tags — Understanding how Git stores ignored files internally
- Git Reflog: Recovery Guide — Recovering from mistakes when ignoring goes wrong
- Git Secrets Management — Comprehensive secret prevention beyond .gitignore
- GitHub Flow Branching Strategy — Clean repository hygiene with proper ignores
Conclusion
A well-crafted .gitignore is the first line of defense against repository clutter — it keeps build artifacts, dependencies, and secrets out of version control. The key is understanding the pattern syntax deeply enough to write precise rules that never accidentally exclude what you need.
Category
Related Posts
Centralized vs Distributed VCS: Team Trade-offs
Compare centralized and distributed version control by offline work, repository size, access control, branching, review, and migration effort.
Automated Changelog Generation
Generate release notes from structured commits, choose a release policy, and add changelog generation to CI without treating every merge as a release.
Choosing a Git Team Workflow
Compare trunk-based development, short-lived feature branches, and release branches against your release, review, rollback, and compliance needs.