Ignoring Files in Git
Some files shouldn't be tracked by Git: build outputs, dependencies, secrets, IDE settings. The .gitignore file tells Git which files to ignore.
Creating .gitignore
Create a .gitignore file in your repository root:
bash
1# Create .gitignore
2touch .gitignore
3
4# Add patterns
5echo "node_modules/" >> .gitignore
6echo ".env" >> .gitignore.gitignore Patterns
Basic Patterns
gitignore
1# Ignore a specific file
2secret.txt
3
4# Ignore all files with extension
5*.log
6*.tmp
7
8# Ignore a directory
9node_modules/
10dist/
11build/Pattern Syntax
gitignore
1# Comments start with #
2# This is a comment
3
4# Ignore file in root only
5/config.local.js
6
7# Ignore file anywhere
8config.local.js
9
10# Ignore directory
11logs/
12
13# Wildcard - any characters
14*.log
15temp_*
16
17# Single character wildcard
18file?.txt # file1.txt, fileA.txt, etc.
19
20# Negation - don't ignore
21*.log
22!important.log
23
24# Double asterisk - match directories
25**/logs # logs anywhere
26logs/** # everything inside logs
27**/logs/** # logs anywhere with contentsCommon .gitignore Examples
Node.js Project
gitignore
1# Dependencies
2node_modules/
3
4# Build outputs
5dist/
6build/
7
8# Environment files
9.env
10.env.local
11.env.*.local
12
13# Logs
14npm-debug.log*
15yarn-debug.log*
16yarn-error.log*
17
18# IDE
19.vscode/
20.idea/
21
22# OS files
23.DS_Store
24Thumbs.dbPython Project
gitignore
1# Virtual environment
2venv/
3env/
4.venv/
5
6# Python cache
7__pycache__/
8*.py[cod]
9*$py.class
10
11# Distribution
12dist/
13build/
14*.egg-info/
15
16# Environment
17.env
18
19# IDE
20.idea/
21.vscode/
22*.swpGeneral Web Project
gitignore
1# Dependencies
2node_modules/
3vendor/
4
5# Build
6dist/
7build/
8.cache/
9
10# Environment
11.env
12.env.local
13
14# IDE/Editor
15.vscode/
16.idea/
17*.sublime-*
18
19# OS
20.DS_Store
21Thumbs.db
22
23# Logs
24*.log
25logs/
26
27# Temporary
28tmp/
29temp/
30*.tmpIgnoring Already Tracked Files
If a file is already tracked, adding it to .gitignore won't work:
bash
1# Remove from tracking (keep the file)
2git rm --cached filename
3
4# For directories
5git rm -r --cached directory/
6
7# Then commit
8git commit -m "Stop tracking filename"Example
bash
1# Accidentally committed node_modules
2git rm -r --cached node_modules
3echo "node_modules/" >> .gitignore
4git commit -m "Remove node_modules from tracking"Global .gitignore
Set up patterns to ignore across all repositories:
bash
1# Create global gitignore
2touch ~/.gitignore_global
3
4# Configure Git to use it
5git config --global core.excludesfile ~/.gitignore_globalGlobal Patterns
gitignore
1# OS files
2.DS_Store
3Thumbs.db
4
5# IDE files
6.idea/
7.vscode/
8*.sublime-*
9
10# Editor backups
11*~
12*.swp
13*.swoCheck If File is Ignored
bash
1# Check why a file is ignored
2git check-ignore -v filename
3
4# Output: .gitignore:5:*.log debug.log
5# (file:line:pattern filename)Template .gitignore Files
GitHub provides templates for different project types:
Or use:
bash
1# Using npx (Node.js)
2npx gitignore node
3
4# Creates .gitignore for Node.js projectsBest Practices
1. Create .gitignore First
Before making your first commit:
bash
1git init
2echo "node_modules/" > .gitignore
3echo ".env" >> .gitignore
4git add .gitignore
5git commit -m "Add .gitignore"2. Don't Ignore Important Files
Never ignore:
- Source code
- Configuration templates
- Documentation
- Package manifests (package.json, requirements.txt)
3. Use Templates
Start with a template for your project type and customize.
Summary
The .gitignore file:
- ✅ Prevents unwanted files from being tracked
- ✅ Uses pattern matching syntax
- ✅ Can be global or per-repository
- ✅ Should be created early in the project
Practice creating .gitignore files in the next exercise!
