.gitignore Guide
Learn how to use .gitignore to tell Git which files to ignore.
What is .gitignore?
The .gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
Why Use .gitignore?
Avoid committing:
- Build artifacts and compiled code
- Dependencies (
node_modules/,vendor/) - IDE and editor files (
.vscode/,.idea/) - OS-generated files (
.DS_Store,Thumbs.db) - Secrets and credentials (
.env, API keys) - Large binary files
- Temporary files
Basic Syntax
Simple Patterns
txt
# Ignore a specific file
secret.txt
# Ignore all files with extension
*.log
# Ignore entire directory
build/
node_modules/
# Ignore directory at root only
/config
# Ignore files in any directory
**/logsPattern Rules
| Pattern | Matches |
|---|---|
file.txt | Specific file in root |
*.log | All files ending in .log |
dir/ | Directory and its contents |
dir/* | Contents of directory only |
/dir | Only root directory |
**/dir | Directory anywhere |
*.py[cod] | .pyc, .pyo, .pyd files |
file[0-9].txt | file0.txt through file9.txt |
Common .gitignore Templates
Node.js Project
txt
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json
yarn.lock
# Build output
dist/
build/
.next/
out/
# Environment variables
.env
.env.local
.env.*.local
# IDE
.vscode/
.idea/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Testing
coverage/
.nyc_output/
# Logs
logs/
*.logPython Project
txt
# Byte-compiled / optimized
__pycache__/
*.py[cod]
*$py.class
# Virtual environment
venv/
env/
ENV/
.venv
# Distribution / packaging
dist/
build/
*.egg-info/
.eggs/
# Testing
.pytest_cache/
.coverage
htmlcov/
.tox/
# IDE
.vscode/
.idea/
*.swp
# Jupyter Notebook
.ipynb_checkpoints/
# Environment
.env
.env.localJava Project
txt
# Compiled class files
*.class
# Package Files
*.jar
*.war
*.ear
*.zip
*.tar.gz
*.rar
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
# Gradle
.gradle/
build/
# IDE
.idea/
*.iml
.classpath
.project
.settings/
*.swp
# Logs
*.logWeb Project (HTML/CSS/JS)
txt
# Dependencies
node_modules/
bower_components/
# Build
dist/
build/
*.min.js
*.min.css
# IDE
.vscode/
.idea/
*.swp
# OS
.DS_Store
Thumbs.db
# Logs
*.log
npm-debug.log*
# Environment
.envComplete .gitignore Example
txt
#################
# IDE & Editors #
#################
# VS Code
.vscode/
*.code-workspace
# IntelliJ IDEA
.idea/
*.iml
*.iws
*.ipr
# Sublime Text
*.sublime-project
*.sublime-workspace
# Vim
*.swp
*.swo
*~
#############
# OS Files #
#############
# macOS
.DS_Store
.AppleDouble
.LSOverride
._*
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
$RECYCLE.BIN/
# Linux
*~
.directory
.Trash-*
##############
# Languages #
##############
# Node.js
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnp/
.pnp.js
# Python
__pycache__/
*.py[cod]
*$py.class
venv/
.Python
# Java
*.class
*.jar
*.war
target/
# C/C++
*.o
*.so
*.exe
*.out
*.app
#############
# Build #
#############
dist/
build/
out/
*.min.js
*.min.css
#################
# Environment #
#################
.env
.env.local
.env.*.local
secrets.yml
config.local.js
##########
# Logs #
##########
logs/
*.log
npm-debug.log*
############
# Testing #
############
coverage/
.nyc_output/
.pytest_cache/
##########
# Misc #
##########
.cache/
.temp/
tmp/
*.tmp
*.bakAdvanced Usage
Negation (Whitelist Files)
txt
# Ignore all .env files
*.env
# But don't ignore .env.example
!.env.example
# Ignore entire folder except one file
logs/
!logs/.gitkeepComments and Blank Lines
txt
# This is a comment
# Blank lines are ignored
*.log # Comments after patterns work tooEscape Special Characters
txt
# Ignore file named #hashtag
\#hashtag
# Ignore file with space
file\ with\ space.txtBest Practices
1. Use Global .gitignore
For personal preferences (IDE, OS files):
bash
# Set global gitignore
git config --global core.excludesfile ~/.gitignore_global
# Create file
echo ".DS_Store" >> ~/.gitignore_global
echo ".vscode/" >> ~/.gitignore_global2. Use Template Generators
- gitignore.io - Generate templates
- GitHub gitignore repo - Official templates
3. Commit .gitignore Early
bash
# Create .gitignore first
touch .gitignore
# Add your patterns
echo "node_modules/" >> .gitignore
# Commit it
git add .gitignore
git commit -m "Add .gitignore"4. Keep It Updated
bash
# Test what files would be ignored
git status --ignored
# Check if file is ignored
git check-ignore -v <file>Troubleshooting
File Already Tracked
If a file is already tracked, .gitignore won't affect it:
bash
# Remove from tracking (keep file locally)
git rm --cached <file>
# Or remove directory
git rm -r --cached <directory>/
# Commit the change
git commit -m "Stop tracking file"Check What's Being Ignored
bash
# Show ignored files
git status --ignored
# Show why file is ignored
git check-ignore -v <file>
# List all ignored files
git ls-files --others --ignored --exclude-standardDebug .gitignore Rules
bash
# Check if specific file is ignored
git check-ignore -v path/to/file
# Output shows:
# .gitignore:12:*.log path/to/file.log
# (file:line:pattern matched file)Common Patterns by Technology
React
txt
node_modules/
build/
.env
.env.local
npm-debug.log*
.DS_StoreVue.js
txt
node_modules/
dist/
.env.local
.DS_Store
npm-debug.log*Angular
txt
node_modules/
dist/
.angular/
.env
npm-debug.log*Docker
txt
# Don't ignore Dockerfile
!Dockerfile
!docker-compose.yml
# Ignore build context
.dockerignoreWordPress
txt
wp-config-local.php
.htaccess
wp-content/uploads/
wp-content/cache/Templates and Resources
Official Templates
Language-Specific
Quick Commands
bash
# Create .gitignore
touch .gitignore
# Generate template (using gitignore.io API)
curl -L https://www.toptal.com/developers/gitignore/api/node,macos,windows,linux,vscode > .gitignore
# Check what's ignored
git status --ignored
# Test pattern
git check-ignore -v <file>
# Remove tracked file
git rm --cached <file>
# Force add ignored file
git add -f <file>See Also
- Git FAQ - Common Git troubleshooting and tips
- Quick Reference - Fast command lookup
- Workflows - Team workflows