Git 🧩 4e Ignoring Files
4e Ignoring Files
What is .gitignore?
The .gitignore file is a text file that tells Git which files or directories to ignore when tracking changes in your repository. It’s essential for keeping your repository clean by excluding unnecessary files like build artifacts, temporary files, and sensitive data.
Basic .gitignore Syntax
- File Structure
Create a .gitignore file in your project root
$ touch .gitignore
- Simple Patterns
Ignore all .log files*.log
Ignore a specific directorynode_modules/
Ignore a specific file.env
Ignore all files in a directory except one
build/
!build/index.html
Common .gitignore Patterns by Technology
- Node.js Projects
# .gitignore for Node.js projects
node_modules/
npm-debug.log
yarn-debug.log
yarn-error.log
# Environment variables (don't commit sensitive data)
.env
.env.local
.env.*.local
# Build artifacts
dist/
build/
*.build.js
# IDE files
.vscode/
.idea/
*.swp
*.swo
- Python Projects
# .gitignore for Python projects
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
env/
venv/
.venv/
pip-log.txt
pip-delete-this-directory.txt
# Django specific
db.sqlite3
media/
staticfiles/
.env
- Java Projects
# .gitignore for Java projects
target/
*.jar
*.war
*.ear
.DS_Store
# IDE files
.idea/
*.iml
*.iws
Advanced Pattern Matching
- Wildcards and Special Characters
Ignore all files ending with .tmp*.tmp
Ignore all files starting with a dot.*
Ignore specific directory contents but not the directory itself
folder/*
!folder/important.txt
Ignore everything except specific files
*
!important.txt
!src/
- Directory Patterns
Ignore all directories named “build”/build/
Ignore all .gitignore files (recursive).gitignore
Ignore directories that match a pattern
*/node_modules/
*/build/
Examples of Real-World .gitignore Files
- Complete Node.js .gitignore
# Logs
logs
*.log
npm-debug.log
yarn-debug.log
yarn-error.log
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
.coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# TypeScript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm test'
test-results
# dotenv environment variables file
.env
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# Environment variables
.env.local
.env.*.local
# IDE
.vscode/
.idea/
- Python Django .gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
.Python
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# code distribution
pyi-*
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
.python-version
# pipenv
.Pipfile.lock
# PEP 582
__pypackages__/
# Celery stuff
celerybeat-schedule
Special Git Ignore Features
- Git Ignore with Specific Files
Only ignore files in current directory*.logIgnore all files but include specific ones
*
!important.txt
!.gitignore
- Directory vs File Patterns
This ignores the entire directory and its contentsfolder/
This ignores only the directory name, not its contentsfolder
Advanced Techniques
- Using Git Attributes for Specific Files
Force binary files to be treated as binary
*.png binary
*.jpg binary
*.gif binary
Set line endings
*.txt text eol=lf
*.js text eol=lf
- Including Exceptions in Ignore Patterns
Ignore all .tmp files*.tmp
But don’t ignore these specific ones
!important.tmp
!temp.log
Managing Multiple .gitignore Files
- Nested .gitignore Files
Root directory .gitignore
node_modules/
.env
In a subdirectory
/src/
/src/node_modules/
The subdirectory’s .gitignore can override root rules!/src/node_modules/
Practical Commands for .gitignore
Check what files Git is tracking$ git ls-files
Check if a file is ignored$ git check-ignore -v <filename>
Remove files from tracking but keep them locally
$ git rm --cached <file>
See all ignored filesgit status --ignored
Add specific patterns to .gitignore$ echo "*.tmp" >> .gitignore
Best Practices Summary
- Start with a template: Use templates for your technology stack (Node.js, Python, etc.)
- Keep it organized: Group similar patterns together and add comments
- Test thoroughly: Make sure the ignore rules work as expected
- Update regularly: As projects evolve, update .gitignore files accordingly
- Document exceptions: Add comments explaining why certain files are ignored or included
- Consider team needs: Collaborate with your team to agree on ignore patterns
The key to effective .gitignore usage is understanding that it’s a powerful tool for keeping repositories clean and manageable, but it must be used thoughtfully to avoid accidentally ignoring important files or including sensitive data.
Questions About Git Ignore
- How do you create a .gitignore file for a new project and what are the essential patterns to include?
- What is the difference between using * and ** in .gitignore patterns, and when should each be used?
- How can you ignore files that have already been committed to Git before adding them to .gitignore?
- What are the best practices for organizing a complex .gitignore file with multiple patterns and exceptions?
- How do you handle .gitignore files in nested directories, and what happens when a parent directory ignores a child directory’s contents?
- Can you ignore specific files within a directory that is ignored by a parent pattern, and how does this work?
- What is the difference between folder/ and folder patterns in .gitignore, and why does it matter?
- How do you debug .gitignore rules when files are still being tracked by Git?
- What are some common mistakes developers make with .gitignore files, and how can they be avoided?
- How do you handle sensitive data like API keys or passwords in a project that uses .gitignore?