DTTooleras

The Complete .gitignore Guide: Templates for Every Language and Framework

Everything you need to know about .gitignore — how pattern matching works, templates for Node.js, Python, Java, Go, Rust, and more, plus common mistakes to avoid.

DevToolsHub Team18 min read531 words

What is .gitignore?

A .gitignore file tells Git which files and directories to ignore — they won't be tracked, committed, or pushed to your repository. This is essential for keeping your repo clean and secure.

Why .gitignore Matters

Without a proper .gitignore, you risk:

  • Exposing secrets.env files, API keys, private keys pushed to public repos
  • Bloating your reponode_modules/ (hundreds of MB), build artifacts, compiled binaries
  • Causing conflicts — IDE settings, OS files, cache directories that differ between developers
  • Slowing down Git — Large binary files make clone, pull, and push painfully slow

Pattern Syntax

# Comments start with #
*.log           # Ignore all .log files
!important.log  # But NOT important.log (negation)
/build          # Ignore build/ in root only
build/          # Ignore build/ anywhere
doc/**/*.pdf    # Ignore PDFs in doc/ subdirectories
**/logs         # Ignore logs/ directory anywhere
PatternMatches
*.logAny file ending in .log
build/Directory named build (and contents)
/buildbuild in root directory only
!keep.logException — don't ignore keep.log
**/*.js.js files in any subdirectory
doc/**Everything inside doc/
?.txtSingle character + .txt (a.txt, b.txt)

Templates by Language

Node.js / JavaScript / TypeScript

node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*
.env
.env.local
.env.*.local
dist/
build/
.next/
out/
coverage/
.nyc_output/
*.tsbuildinfo
.turbo

Python

__pycache__/
*.py[cod]
*$py.class
*.so
.venv/
venv/
.env
*.egg-info/
dist/
build/
.mypy_cache/
.pytest_cache/
.coverage
htmlcov/

Go

*.exe
*.dll
*.so
*.dylib
*.test
*.out
vendor/
.env

Rust

/target
Cargo.lock
**/*.rs.bk

Java

*.class
*.jar
*.war
*.ear
.gradle/
build/
.idea/
*.iml
out/
target/

Generate a complete .gitignore for your project with our .gitignore Generator — select your languages, frameworks, and IDEs and get a ready-to-use file.

Common Mistakes

1. Adding .gitignore After Committing Files

If you already committed node_modules/ and then add it to .gitignore, Git will continue tracking it. You need to remove it from tracking first:

# Remove from Git tracking (keeps local files)
git rm -r --cached node_modules/
git commit -m "Remove node_modules from tracking"

2. Forgetting .env Files

Always add .env to .gitignore before your first commit. If you accidentally commit secrets:

# Remove from history (use with caution)
git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch .env" \
  --prune-empty --tag-name-filter cat -- --all

Better yet, rotate any exposed credentials immediately.

3. Ignoring Lock Files

Don't ignore lock files (package-lock.json, yarn.lock, Cargo.lock for applications). They ensure reproducible builds. Only ignore Cargo.lock for libraries.

Global .gitignore

Set up a global .gitignore for OS and IDE files so you don't need them in every project:

git config --global core.excludesfile ~/.gitignore_global
# ~/.gitignore_global
.DS_Store
Thumbs.db
.idea/
.vscode/
*.swp
*.swo
*~

Related Tools

gitignoregit ignoregitignore templategitignore nodegitignore pythongitgitignore generator

Related articles

All articles

Practice with free tools

200+ free developer tools that run in your browser.

Browse all tools →