A well-crafted .gitignore file keeps your repository clean by excluding build artifacts, dependencies, secrets, and OS-specific files. This comprehensive guide covers gitignore syntax, templates for every major language and framework, IDE patterns, common mistakes, and our generator tool.
What is .gitignore โ Syntax Rules
A .gitignore file tells Git which files and directories to exclude from version control. It uses a simple pattern-matching syntax:
#โ Comments โ Lines starting with # are comments and ignored by Git.!โ Negation โ Prefix a pattern with ! to re-include a previously excluded file./โ Directories โ Append / to match only directories, not files with the same name.***?โ Globbing โ Use * to match any characters (except /), ** to match nested directories, and ? to match a single character.- Anchoring โ A pattern without / matches anywhere in the tree. A leading / anchors to the repo root.
Syntax Examples
# Ignore all .log files
*.log
# Ignore the build directory
build/
# Ignore all files in temp/ recursively
temp/**
# But keep .gitkeep files
!temp/.gitkeep
# Ignore .env in root only
/.env
# Ignore all .txt files in doc/ and subdirectories
doc/**/*.txt
# Ignore any file named "secret" anywhere
secretGlobal vs Local .gitignore
Git supports three levels of ignore rules, each serving a different purpose:
The standard .gitignore at the repo root. Shared with all collaborators. Use for project-specific patterns like build output, dependencies, and environment files.
Your personal ignore rules applied to every repository on your machine. Use for OS files (.DS_Store, Thumbs.db) and editor files (.vscode, .idea) that should not pollute shared .gitignore.
Set up your global gitignore:
# Create a global gitignore file
touch ~/.gitignore_global
# Tell Git to use it
git config --global core.excludesfile ~/.gitignore_global
# Add common patterns
echo ".DS_Store" >> ~/.gitignore_global
echo "Thumbs.db" >> ~/.gitignore_global
echo ".idea/" >> ~/.gitignore_global
echo ".vscode/" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_globalPer-repository ignore rules that are NOT committed. Use for personal files specific to one project that other collaborators do not need to know about (e.g., local test scripts, personal notes).
Node.js .gitignore Template
The essential .gitignore for Node.js projects. Covers npm/yarn/pnpm dependency folders, build output, environment files, and caches:
# Dependencies
node_modules/
.pnp
.pnp.js
# Build output
dist/
build/
out/
# Environment files
.env
.env.local
.env.*.local
# Testing
coverage/
# Cache
.cache/
.parcel-cache/
.next/
.nuxt/
.turbo/
# Debug logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
*.tsbuildinfo- node_modules/ โ Dependencies installed by npm/yarn/pnpm. Always regenerated from package.json.
- dist/ and build/ โ Compiled output. Regenerated by your build tool.
- .env files โ Contains API keys, database passwords, and other secrets. Never commit these.
- coverage/ โ Test coverage reports generated by Jest, Istanbul, or c8.
- .cache/ โ Build caches from Babel, Parcel, Webpack, etc.
- Debug logs โ npm-debug.log, yarn-debug.log, yarn-error.log files.
Python .gitignore Template
Python projects generate many bytecode and build artifacts. This template covers all common patterns:
# Byte-compiled / optimized
__pycache__/
*.py[cod]
*$py.class
# Virtual environments
venv/
.venv/
env/
ENV/
# Distribution / packaging
dist/
build/
*.egg-info/
*.egg
.eggs/
*.whl
# Testing
.pytest_cache/
.coverage
htmlcov/
.tox/
.nox/
# Type checking
.mypy_cache/
.pytype/
.pyre/
# IDE
.spyderproject
.spyproject
# Jupyter
.ipynb_checkpoints/
# Environment
.env
.env.local- __pycache__/ and *.pyc โ Python bytecode files. Regenerated automatically on import.
- venv/ and .venv/ โ Virtual environment directories. Each developer creates their own.
- *.egg-info/ and dist/ โ Package distribution artifacts from setuptools/pip.
- .mypy_cache/ โ Type checking cache from mypy.
- .pytest_cache/ โ Pytest session cache.
- .tox/ โ Tox testing environment directories.
Java / Kotlin .gitignore Template
Java and Kotlin projects using Maven or Gradle produce compiled classes and build directories:
# Compiled class files
*.class
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
# Gradle
build/
.gradle/
gradle-app.setting
!gradle-wrapper.jar
!gradle-wrapper.properties
# Package files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# IDE - IntelliJ
.idea/
*.iws
*.iml
*.ipr
out/
# IDE - Eclipse
.classpath
.project
.settings/
bin/- *.class โ Compiled Java bytecode files.
- target/ โ Maven build output directory.
- build/ โ Gradle build output directory.
- .gradle/ โ Gradle wrapper cache and configuration.
- *.jar, *.war, *.ear โ Compiled archive files. Use dependency management instead.
- .idea/ โ IntelliJ IDEA project files (also covered in IDE section).
React / Next.js .gitignore Template
React and Next.js projects combine Node.js patterns with framework-specific directories:
# Dependencies
node_modules/
# Next.js
.next/
out/
# Production build
build/
dist/
# Environment files
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Testing
coverage/
# Vercel
.vercel/
# TypeScript
*.tsbuildinfo
next-env.d.ts
# Debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Cache
.cache/
.turbo/- .next/ โ Next.js build output and cache. Regenerated on every build.
- out/ โ Next.js static export output directory.
- node_modules/ โ Same as any Node.js project.
- .env.local and .env*.local โ Local environment overrides with secrets.
- .vercel/ โ Vercel CLI deployment configuration.
Go .gitignore Template
Go projects are typically lean, but there are still files to exclude:
# Binaries
bin/
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary
*.test
# Output of go coverage
*.out
# Vendor (optional โ some teams commit vendor/)
vendor/
# Go workspace
go.work
# IDE
.idea/
.vscode/
# OS
.DS_Store
Thumbs.db
# Environment
.env- bin/ โ Compiled binary output directory.
- vendor/ โ Vendored dependencies (if not using Go modules or if you prefer to exclude).
- *.exe, *.exe~, *.dll, *.so, *.dylib โ Compiled binary files for various platforms.
- *.test โ Compiled test binaries from go test -c.
- *.out โ Coverage output files from go test -coverprofile.
Rust .gitignore Template
Rust and Cargo projects have a straightforward ignore setup:
# Build artifacts
/target/
# Cargo.lock โ ONLY ignore for libraries
# For binaries (applications), DO commit Cargo.lock
# Cargo.lock
# Debug symbols (Windows)
*.pdb
# IDE
.idea/
.vscode/
*.swp
*.swo
# OS
.DS_Store
Thumbs.db
# Environment
.env- target/ โ All build artifacts, debug and release builds, incremental compilation cache.
- Cargo.lock โ For libraries, Cargo.lock should NOT be committed (it is regenerated for consumers). For binaries/applications, DO commit Cargo.lock.
- *.pdb โ Windows debug symbol files.
IDE & OS Patterns
These patterns should ideally go in your global ~/.gitignore_global to keep the repository .gitignore clean. But if your team agrees, add them to the project .gitignore:
# โโโ VS Code โโโ
.vscode/
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/extensions.json
# โโโ JetBrains IDEs โโโ
.idea/
*.iws
*.iml
*.ipr
# โโโ Vim โโโ
*.swp
*.swo
*~
.netrwhist
# โโโ Sublime Text โโโ
*.sublime-workspace
*.sublime-project
# โโโ macOS โโโ
.DS_Store
.AppleDouble
.LSOverride
._*
# โโโ Windows โโโ
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
Desktop.ini
# โโโ Linux โโโ
*~
.fuse_hidden*
.Trash-*- .vscode/ โ VS Code workspace settings. Alternatively, commit .vscode/settings.json but ignore .vscode/launch.json.
- .idea/ โ JetBrains IDEs (IntelliJ, WebStorm, PyCharm) project directory.
- .DS_Store โ macOS Finder metadata file. Created in every directory you open in Finder.
- Thumbs.db โ Windows thumbnail cache file.
- *.swp, *.swo โ Vim swap files created during editing.
- *.sublime-workspace โ Sublime Text workspace files.
Common Mistakes & Fixes
Even experienced developers make these .gitignore mistakes. Here is how to diagnose and fix each one:
Adding a pattern to .gitignore does NOT remove files that are already tracked by Git. You must untrack them first:
Fix: Remove from Git cache, then commit
# Remove file from Git tracking (keeps it on disk)
git rm --cached .env
# Remove an entire directory from tracking
git rm -r --cached node_modules/
# Commit the change
git add .gitignore
git commit -m "Stop tracking ignored files"
# Verify it worked
git statusNegation patterns (!) only re-include files excluded by earlier patterns. The order matters:
# WRONG โ negation before exclusion has no effect
!important.log
*.log
# CORRECT โ exclude first, then negate
*.log
!important.log
# Another example: ignore everything in docs/ except PDFs
docs/*
!docs/*.pdfThis is the #1 security mistake. If you have already committed a .env file, removing it from .gitignore alone is not enough โ the secrets are in Git history.
Fix: Remove from tracking and rotate all secrets
# 1. Add .env to .gitignore
echo ".env" >> .gitignore
# 2. Remove from Git tracking
git rm --cached .env
# 3. Commit
git add .gitignore
git commit -m "Remove .env from tracking"
# 4. IMPORTANT: Rotate all secrets in that .env file!
# The old values are still in Git history.A trailing slash makes a difference. Without it, the pattern matches both files and directories:
# Matches both a FILE named "logs" and a DIRECTORY named "logs"
logs
# Matches ONLY a directory named "logs"
logs/
# Example: you have a file "build" and a directory "build/"
# "build" in .gitignore ignores BOTH
# "build/" in .gitignore ignores only the directoryPatterns like .DS_Store, Thumbs.db, and .idea/ are personal preferences, not project requirements. Move them to your global gitignore to keep the project .gitignore focused.
.gitignore Generators & Tools
Instead of manually crafting .gitignore files, use a generator tool to create a comprehensive template for your project stack:
- Our Gitignore Generator tool lets you select your languages, frameworks, and IDEs to create a complete .gitignore file in seconds.
- GitHub maintains a collection of .gitignore templates at github/gitignore for most languages and frameworks.
- Pro tip: Start with a generated template, then customize it for your specific project needs. Review it periodically as your project evolves.
Generate your .gitignore file instantly
Gitignore Generator โFrequently Asked Questions
Why is my .gitignore not working?
The most common reason is that the files are already tracked by Git. Adding a pattern to .gitignore only affects untracked files. To fix this, run "git rm --cached <file>" to untrack the file, then commit the change. The file will remain on disk but Git will stop tracking it.
Should I commit .gitignore to the repository?
Yes, always commit your .gitignore file. It ensures all collaborators share the same ignore rules. However, personal preferences like IDE settings and OS-specific files should go in your global ~/.gitignore_global instead.
What is the difference between .gitignore and .git/info/exclude?
.gitignore is committed to the repository and shared with all collaborators. .git/info/exclude is local to your clone and never committed. Use .git/info/exclude for personal ignore rules that only apply to you in one specific repository.
How do I ignore a file that is already committed?
First, add the pattern to .gitignore. Then run "git rm --cached <file>" to remove it from tracking without deleting it from disk. Finally, commit both changes. Note: the file will still exist in Git history. If it contained secrets, you need to rewrite history or rotate the secrets.
Can I have multiple .gitignore files in a repository?
Yes, Git supports .gitignore files in any subdirectory. Patterns in a subdirectory .gitignore only apply to files within that directory and its children. The root .gitignore applies to the entire repository. This is useful for monorepos where different packages need different ignore rules.