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/
*.tsbuildinfoPython
__pycache__/
*.py[cod]
*.pyo.venv/
venv/
*.egg-info/
dist/
build/.pytest_cache/Java
target/
*.class
*.jar
*.war.gradle/
build/Go
bin/
vendor/
*.exe
*.testGlob Syntax Reference
| Pattern | Meaning |
|---|---|
* | Match any file |
** | Match directories recursively |
*.log | All.log files |
dir/ | Ignore entire directory |
!important.log | Force-track despite earlier ignore |
/root-only.txt | Only 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/
*.swpRemoving 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 files —
package-lock.jsonandyarn.lockshould 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.