Skip to content

.gitignore

Excluding files from Git with .gitignore — patterns, global ignores, and unignoring files.

What you'll learn

  • How to create and use a .gitignore file
  • Common ignore patterns for different languages and frameworks
  • Ignoring files globally vs per-repository
  • How to unignore a previously ignored file

Concept

What .gitignore Does

A .gitignore file tells Git which files and directories to ignore — not track, not show in git status, and not include in git add .. This keeps your repository clean and prevents accidentally committing sensitive or generated files.

# .gitignore
node_modules/
.env
*.log
dist/

Any file matching a pattern in .gitignore will not appear in git status as untracked, and git add . will skip them.

Common Patterns

# Ignore all .log files
*.log

# Ignore a specific file
secret.key

# Ignore an entire directory
node_modules/
dist/
build/

# Ignore everything in a directory but keep the directory
logs/*
!logs/.gitkeep

# Negate a pattern (un-ignore)
*.env
!.env.example

# Ignore all .md files except README.md
*.md
!README.md

# Ignore files only in the root directory
/TODO.txt

# Ignore files in a specific path
src/temp/*.js

Language-Specific Ignores

Every language and framework has files that should be ignored:

  • Node.js: node_modules/, dist/, .env, npm-debug.log
  • Python: __pycache__/, *.pyc, .venv/, *.egg-info/
  • Java: target/, *.class, .gradle/
  • macOS: .DS_Store
  • IDE: .vscode/, .idea/

Use the .gitignore Generator tool to create a tailored .gitignore for your project.

Global .gitignore

You can set a global ignore file for files you never want to commit on any project (like OS-specific files):

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

Then create ~/.gitignore_global:

# ~/.gitignore_global
.DS_Store
Thumbs.db
.vscode/

Already-Tracked Files

.gitignore only prevents untracked files from being added. If a file is already tracked (committed), adding it to .gitignore does NOT stop Git from tracking it. You must remove it first:

git rm --cached .env
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env file"

--cached removes the file from Git but keeps it on your disk.

Where to Put .gitignore

You can place .gitignore files in the repository root or in any subdirectory. Rules in a subdirectory apply only to that directory and below. The root .gitignore is the most common location.

The Golden Rule

Never commit secrets, credentials, or large generated files. A proper .gitignore is your first line of defense. Always create it before your first git add ..

Example

              # Create a .gitignore for a Node.js project
cat > .gitignore << 'EOF'
# Dependencies
node_modules/

# Build output
dist/
build/

# Environment variables (NEVER commit these)
.env
.env.local
.env.*.local

# Logs
*.log
npm-debug.log*

# OS files
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/

# Test coverage
coverage/
EOF

# Verify it works — create files that should be ignored
mkdir -p node_modules dist
echo "secret" > .env
echo "log entry" > app.log

git status
# node_modules/, dist/, .env, app.log do NOT appear as untracked

# Force-add an ignored file (when you really need to)
git add -f .env.example

# Stop tracking a file that was already committed
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env and add to gitignore"

# Set up a global gitignore for OS files
git config --global core.excludesfile ~/.gitignore_global
echo ".DS_Store" > ~/.gitignore_global

# Check which files are being ignored
git status --ignored
            

Creates a comprehensive .gitignore for a Node.js project, verifies that ignored files do not appear in git status, force-adds an exception, stops tracking an already-committed file with git rm --cached, sets up a global ignore, and checks ignored files with git status --ignored.

Try it

Common mistakes

The mistake

Adding a file to .gitignore after it was already committed

The fix

.gitignore only affects untracked files. If a file is already tracked, adding it to .gitignore does nothing. Run git rm --cached <file> to stop tracking it (the file stays on disk), then add it to .gitignore and commit.

The mistake

Commiting .env files with secrets and then just deleting them

The fix

Deleting a file in a new commit does not remove it from history — the secret is still in past commits. Use git rm --cached to stop tracking, then consider tools like git filter-repo or BFG Repo-Cleaner to purge it from history. Always rotate leaked credentials immediately.

Related Cheatsheets

Practice .gitignore

5 exercises

Practice .gitignore

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors