.gitignore File
Learn about .gitignore File
Not every file in your project should be tracked by Git. Build artifacts, dependencies, environment secrets, and IDE configuration files are all things you want Git to ignore. The .gitignore file tells Git exactly which files and directories to skip.
Creating .gitignore
Create a file called .gitignore in your project root:
touch .gitignore
Each line specifies a pattern to ignore. Let's build one for a typical web project:
# Dependencies
node_modules/
vendor/
bower_components/
# Build output
dist/
build/
.next/
out/
# Environment files (contain secrets)
.env
.env.local
.env.production
# IDE and editor files
.idea/
.vscode/
*.swp
*.swo
*~
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
npm-debug.log*
# Coverage reports
coverage/
.nyc_output/
# Compiled files
*.pyc
__pycache__/
*.class
*.o
Pattern Syntax
# Ignore a specific file
secrets.json
# Ignore all files with an extension
*.log
*.tmp
# Ignore a directory (and everything inside)
node_modules/
dist/
# Ignore files in any subdirectory
**/*.pyc
# Negate a pattern (DON'T ignore this)
*.log
!important.log # Track important.log even though *.log is ignored
# Ignore files only in root (not subdirectories)
/TODO.txt # Ignores ./TODO.txt but not src/TODO.txt
# Comments
# This is a comment
Common .gitignore Templates
Node.js
node_modules/
.env
.env.local
dist/
*.log
coverage/
Python
__pycache__/
*.pyc
.env
venv/
.venv/
dist/
*.egg-info/
Go
bin/
*.exe
*.out
vendor/
.env
Fixing Already-Tracked Files
If you already committed a file and later add it to .gitignore, Git will still track it. You need to un-track it:
# Stop tracking a file
git rm --cached .env
git commit -m "Stop tracking .env"
# Stop tracking a directory
git rm -r --cached node_modules/
git commit -m "Stop tracking node_modules"
# Now the .gitignore rule will take effect
Global .gitignore
For files that should be ignored across ALL repositories on your machine (like IDE files):
# Create a global gitignore
git config --global core.excludesFile ~/.gitignore_global
# Add patterns to it
echo ".DS_Store" >> ~/.gitignore_global
echo ".idea/" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_global
Checking Ignored Files
# See which files are being ignored
git status --ignored
# Check if a specific file is ignored (and why)
git check-ignore -v config.log
What's Next
Your repository is now clean and organized with proper ignore rules. In the next episode, we'll explore Git features built into VS Code for a visual workflow.