Why.gitignore Matters

A .gitignore file tells Git which files and directories to exclude from version control. Without it, you risk committing secrets (.env), bloated dependencies (node_modules/), build artifacts, and OS-specific junk files. A well-crafted.gitignore keeps your repository clean, secure, and fast.

Universal Patterns (Every Project)

# Environment & secrets.env.env.local.env.*.local # OS files.DS_Store
Thumbs.db
Desktop.ini # Editor files.vscode/settings.json.idea/
*.swp
*.swo
*~ # Logs
*.log
npm-debug.log*
yarn-debug.log*

Language-Specific Templates

Node.js / JavaScript

node_modules/
dist/
build/.cache/
coverage/
*.tsbuildinfo

Python

__pycache__/
*.py[cod]
*.pyo.venv/
venv/
*.egg-info/
dist/
build/.pytest_cache/

Java

target/
*.class
*.jar
*.war.gradle/
build/

Go

bin/
vendor/
*.exe
*.test

Glob Syntax Reference

PatternMeaning
*Match any file
**Match directories recursively
*.logAll.log files
dir/Ignore entire directory
!important.logForce-track despite earlier ignore
/root-only.txtOnly ignore in root, not subdirs

Global.gitignore

Personal preferences (editor configs, OS files) should go in a global gitignore so they don't clutter every project:

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

Removing Already-Tracked Files

Adding a file to.gitignore doesn't remove it from Git history if it's already tracked. You need to explicitly untrack it:

# Remove from Git but keep local file
git rm --cached.env
git rm -r --cached node_modules/ # Then commit the removal
git commit -m "Remove tracked files that should be ignored"

Common Mistakes

  • Committing.env files — Always add.env to.gitignore before the first commit
  • Ignoring lock filespackage-lock.json and yarn.lock should be committed
  • Ignoring.gitignore itself — Never ignore the.gitignore file
  • Not using a template — Start with GitHub's official templates at github/gitignore

Generate a.gitignore file instantly with our .gitignore Generator — select your language, framework, and IDE and get a complete template.