Skip to main content

.gitignore

What is .gitignore?

A .gitignore file tells Git which files and folders to ignore — meaning they won't be tracked, staged, or committed. You place it in the root of your repository.

Files you almost always want to ignore:

  • Dependenciesnode_modules/, venv/, .venv/
  • Environment files.env, .env.local (contain secrets!)
  • Build outputdist/, build/, .next/
  • IDE/editor settings.idea/, .vscode/ (optional, personal preference)
  • OS files.DS_Store (macOS), Thumbs.db (Windows)
  • Logs*.log, npm-debug.log

Creating a .gitignore

Create a file named exactly .gitignore (note the leading dot) in your project root:

touch .gitignore

Then add patterns, one per line:

.gitignore
# Dependencies
node_modules/

# Environment variables — NEVER commit these
.env
.env.local
.env.*.local

# Build output
dist/
build/
.next/
out/

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

# OS files
.DS_Store
Thumbs.db

# IDE settings
.idea/
.vscode/
*.swp
*.swo

Pattern Syntax

PatternWhat it ignores
node_modules/The folder named node_modules anywhere
*.logAny file ending in .log
.envA file named exactly .env
*.envAny file ending in .env
dist/The dist folder
!dist/index.jsBut not this specific file (negate with !)
**/logslogs folder anywhere in the tree
debug.logOnly at root level
/debug.logSame — / anchors to root

Ready-Made Templates

GitHub provides official .gitignore templates for every language and framework. When creating a new repo on GitHub, you can select a template from the dropdown.

Or use gitignore.io — type in your tech stack (e.g., Node,React,macOS) and it generates a complete .gitignore for you.

Common starting points:

Node.js / Express:

node_modules/
.env
.env.*
dist/
*.log
.DS_Store

React (Create React App):

node_modules/
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
build/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.DS_Store

React (Vite):

node_modules/
dist/
.env
.env.local
*.local
.DS_Store

Ignoring Already-Tracked Files

.gitignore only prevents untracked files from being tracked. If you accidentally committed a file (like .env) and later added it to .gitignore, Git still tracks it.

To stop tracking it without deleting it locally:

git rm --cached .env
git commit -m "Remove .env from tracking"

Now .env stays in your working directory but is no longer in Git history going forward.

If you committed secrets

If you've committed API keys, passwords, or tokens to a public repository, treat them as compromised immediately:

  1. Revoke and rotate the exposed credentials
  2. Use git filter-branch or BFG Repo Cleaner to purge them from history
  3. Force-push the cleaned history

Removing from history is complex — it's far easier to never commit secrets in the first place.

A Practical Tip: Global .gitignore

You can set a global .gitignore for files you always want to ignore regardless of project — like OS and editor files. This way you don't add .DS_Store to every project's .gitignore:

git config --global core.excludesfile ~/.gitignore_global

Then add your personal ignores to ~/.gitignore_global:

~/.gitignore_global
# macOS
.DS_Store
.AppleDouble

# Windows
Thumbs.db
Desktop.ini

# Editor
.vscode/
.idea/
*.swp