Generators5 min read

.gitignore Guide: Syntax, Examples, and Patterns for Every Project

Learn how .gitignore works, the pattern syntax, common patterns for Node.js, Python, Java, Go, and macOS, and how to fix files already tracked by git.

Try the free online tool mentioned in this guide:.gitignore Generator

What is .gitignore?

A .gitignore file tells git which files and directories to exclude from version control. Files matched by .gitignore patterns are not staged, committed, or shown in git status output.

Without a proper .gitignore, you risk committing node_modules (hundreds of megabytes), .env files with secrets, OS junk files (.DS_Store), build artifacts, and IDE config — all of which pollute history and can cause security incidents.

.gitignore pattern syntax

  • Exact filename: secrets.txt — ignores any file named secrets.txt in any directory.
  • Directory: node_modules/ — the trailing slash matches only directories.
  • Wildcard: *.log — ignores all files ending in .log.
  • Double wildcard: /logs — ignores a logs directory anywhere in the tree. logs/ ignores everything inside logs.
  • Negation: !important.log — un-ignores a previously ignored file. Must come after the ignoring pattern.
  • Comment: lines starting with # are comments.
  • Leading slash: /build — ignores build only at the root, not in subdirectories.
bash
# OS files
.DS_Store
Thumbs.db

# Secrets — never commit
.env
.env.local
.env.*.local
*.pem
*.key

# Dependencies
node_modules/
vendor/

# Build output
dist/
build/
*.min.js

# Logs
logs/
*.log
npm-debug.log*

# IDE
.idea/
.vscode/
*.swp

# Negation: keep this specific file despite *.log rule
!important.log

Language and framework-specific patterns

Node.js: node_modules/, .npm, *.tgz, dist/, .env, coverage/

Python: __pycache__/, *.pyc, *.pyo, .venv, venv/, .pytest_cache/, *.egg-info/, dist/, .mypy_cache/

Java: *.class, *.jar, *.war, target/, .gradle/, build/

Go: *.exe, *.test, vendor/ (if not committing deps)

Rust: target/, Cargo.lock (for libraries — keep for binaries)

macOS: .DS_Store, .AppleDouble, .LSOverride

Linux: *~ (backup files)

Windows: Thumbs.db, Desktop.ini, $RECYCLE.BIN/

Fixing files already tracked by git

If you add a pattern to .gitignore but the file is already tracked (committed), git will continue tracking it. You must explicitly untrack it.

bash
# Remove a specific file from tracking (keeps local file)
git rm --cached path/to/file

# Remove a directory from tracking recursively
git rm -r --cached node_modules/

# Then commit the removal
git add .gitignore
git commit -m "Remove tracked files that should be gitignored"

Global gitignore for OS and IDE files

Instead of adding .DS_Store or .idea/ to every project's .gitignore, add them once to a global gitignore file. This keeps project .gitignore files focused on project-specific exclusions.

bash
# Create a global gitignore
touch ~/.gitignore_global

# Add your patterns
echo ".DS_Store" >> ~/.gitignore_global
echo ".idea/" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_global

# Tell git to use it
git config --global core.excludesfile ~/.gitignore_global

Using a gitignore generator

The GitHub-maintained gitignore repository contains templates for hundreds of languages, frameworks, and tools. Instead of writing patterns from scratch, use a generator that pulls the correct template for your stack.

MyDevTools Gitignore Generator lets you select one or more technologies (Node, Python, Go, macOS, JetBrains, VSCode, etc.) and produces a merged, deduplicated .gitignore file ready to paste into your project root.

Frequently asked questions

Why is git still tracking my .env file after I added it to .gitignore?

.gitignore only prevents untracked files from being staged. If .env was already committed, git keeps tracking it. Run git rm --cached .env, then commit the removal.

Should I commit .gitignore?

Yes. .gitignore is project configuration that all contributors should share. Commit it alongside your source code.

What is the difference between .gitignore, .gitkeep, and .gitattributes?

.gitignore specifies files to exclude. .gitkeep is a convention (not a git feature) — an empty placeholder file used to commit otherwise-empty directories. .gitattributes controls line endings, diff behavior, and merge strategies per file pattern.

Can I have multiple .gitignore files in one repo?

Yes. A .gitignore file applies to its directory and all subdirectories. You can place more specific .gitignore files in subdirectories to override or extend the root rules for that subtree.

Try .gitignore Generator for free

Generate .gitignore files instantly for a specific tech stack (Node, Python, macOS, etc.) or combination. No install, no account required to try it.