G
GuideDevOps
Lesson 15 of 18

.gitignore & File Exclusions

Part of the Git & Version Control tutorial series.

What is .gitignore?

.gitignore is a file that tells Git which files/folders to ignore (not track).

Files to Ignore

  • Dependencies: node_modules/, vendor/
  • Temporary: *.tmp, cache/
  • Configuration: .env, secrets
  • Build output: dist/, build/
  • IDE settings: .vscode/, .idea/
  • OS files: .DS_Store, Thumbs.db
  • Logs: *.log

Creating .gitignore

Basic Syntax

# Comments start with #
# Blank lines are ignored
 
# Ignore specific file
secrets.txt
 
# Ignore by extension
*.log
*.tmp
 
# Ignore directory
node_modules/
build/
 
# Ignore pattern
cache/**/*.txt

Example .gitignore

# Dependencies
node_modules/
vendor/
.bundle/
 
# Environment variables
.env
.env.local
.env.*.local
 
# Logs
*.log
npm-debug.log*
yarn-debug.log*
 
# Build output
dist/
build/
.next/
out/
 
# IDE
.vscode/
.idea/
*.swp
*.swo
 
# OS
.DS_Store
Thumbs.db
*.exe
 
# Temporary
*.tmp
temp/
tmp/
 
# Test coverage
coverage/
.nyc_output/

Gitignore Patterns

Exact Match

# Ignore specific file
config.local.js
 
# Ignore anywhere
passwords.txt

Wildcard Patterns

# Match any extension
*.log       # .log files
*.tmp       # .tmp files
*.swp       # Vim swapfiles
 
# Match any name
*-local.ts  # config-local.ts, data-local.ts
node_*      # node_modules, node_temp, etc.

Directory Patterns

# Ignore entire directory
node_modules/
.vscode/
build/
 
# Ignore everywhere
**/node_modules/    # node_modules in any subfolder
**/dist/

Negate/Exception

# Ignore all .log files
*.log
 
# EXCEPT keep important log
!important.log

Path Patterns

# In root only
/config.local.js
 
# In any subdirectory
src/**/*.log
 
# All except specific
*.tmp
!keep-this.tmp

Directory-Specific .gitignore

You can have .gitignore files in subdirectories:

project/
├── .gitignore           # Root level ignores
├── frontend/
│   └── .gitignore       # Frontend-specific ignores
└── backend/
    └── .gitignore       # Backend-specific ignores

Common Patterns by Language

JavaScript/Node.js

node_modules/
npm-debug.log*
yarn-error.log*
.next/
dist/
build/

Python

__pycache__/
*.py[cod]
.env
venv/
.pytest_cache/
*.egg-info/

Java

target/
.class
*.jar
*.log
.idea/

Go

.DS_Store
*.o
*.a
dist/
vendor/

Managing Gitignore

Check What's Ignored

# See if file would be ignored
git check-ignore -v filename.txt
# Shows rule that would ignore it
 
# See all ignored files
git status --ignored

Stop Ignoring File

# By default this is ignored
*.log
 
# But keep this one
!important.log

Remove Ignored Files from Tracking

If you added file before adding it to .gitignore:

# Remove from tracking (keep locally)
git rm --cached secrets.txt
 
# Remove from tracking (delete file)
git rm secrets.txt
 
# Commit the removal
git commit -m "Remove secrets from tracking"

.gitignore Templates

GitHub Provides Templates

For common project types:

cd project
curl https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore > .gitignore

Or create from GitHub UI when initializing repo.

Popular Template Sources

Best Practices

Root-Level Gitignore

Create in project root, commit to repository:

cd my-project
echo "node_modules/" > .gitignore
git add .gitignore
git commit -m "Add gitignore"

Global Gitignore

Personal ignores for your machine (.config/git/ignore):

# Configure
git config --global core.excludesFile ~/.gitignore_global
 
# Add personal ignores
echo ".DS_Store" >> ~/.gitignore_global
echo ".vscode/" >> ~/.gitignore_global
 
# These ignored everywhere without affecting team

Document Ignored Patterns

Add comments explaining why:

# Dependencies - managed by npm
node_modules/
 
# Environment configuration - contains secrets
.env
.env.local
 
# IDE - personal preferences
.vscode/
.idea/
 
# Build - generated, don't commit
dist/

Review Before Committing

git status
# Shows what will be committed
# Make sure you're not accidentally including secrets
 
git diff --cached
# Show exactly what's being added

Troubleshooting

File Should Be Ignored But Shows Up

# Check if ignoring correctly
git check-ignore -v app/config.local.ts
 
# If not showing rule, check syntax
 
# If showing ignored but still tracked:
git rm --cached app/config.local.ts
git commit -m "Remove from tracking"

Can't Add File (Parent Ignored)

# If parent ignored, child ignored too
logs/
 
# To add file in ignored folder:
!logs/important.log

Real-World Example

Comprehensive .gitignore:

# Node
node_modules/
npm-debug.log*
 
# Build
dist/
build/
.next/
 
# Environment
.env
.env.local
.env.*.local
 
# IDE & OS
.vscode/
.idea/
.DS_Store
Thumbs.db
 
# Testing
coverage/
.nyc_output/
 
# Temporary
*.tmp
temp/
cache/
 
# Logs
*.log
logs/

Tips

✓ Create .gitignore at project start
✓ Use templates for your project type
✓ Commit .gitignore to repository
✓ Add comments explaining rules
✓ Review patterns when onboarding new languages
✓ Use global .gitignore for personal preferences
✓ Never commit secrets - always ignore .env files