Git File Ignoring: How to Prevent Files from Being Tracked or Pushed
Master the art of preventing files from being tracked or pushed in Git. Learn when to use .gitignore, assume-unchanged, and other techniques to keep sensitive or local files out of your repository.
Git File Ignoring: How to Prevent Files from Being Tracked or Pushed
Ever accidentally committed a file with sensitive information like API keys or database passwords? Or found yourself constantly dealing with local configuration files that keep showing up in your Git status? These are common problems that every developer faces, and Git provides several tools to help you manage which files get tracked and pushed to your repository.
Understanding the Problem
Why You Need File Ignoring
Common scenarios where you want to ignore files:
- Sensitive information - API keys, passwords, tokens
- Local configuration - IDE settings, personal preferences
- Generated files - Build artifacts, compiled code, logs
- Temporary files - Cache, temporary downloads, swap files
- Environment-specific files - Different configs for dev/staging/prod
Solution 1: Using .gitignore
for Untracked Files
What is .gitignore
?
The .gitignore
file tells Git which files and directories to ignore. It's a simple text file that contains patterns for files you don't want Git to track.
Creating a .gitignore
File
# Create .gitignore in your repository root
touch .gitignore
# Or create it with initial content
echo "*.log" > .gitignore
Common .gitignore
Patterns
# .gitignore
# Environment variables
.env
.env.local
.env.*.local
# Logs
*.log
logs/
npm-debug.log*
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.o
*.so
# IDE files
.vscode/
.idea/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
# Temporary files
*.tmp
*.temp
.cache/
Advanced .gitignore
Patterns
# Ignore all files in a directory except specific ones
temp/*
!temp/important.txt
# Ignore files with specific extensions
*.log
*.tmp
*.cache
# Ignore files in any directory with a specific name
**/node_modules/
**/dist/
# Ignore files matching a pattern
*_backup.*
*_old.*
# Ignore files in specific directories
src/**/*.test.js
docs/**/*.draft.md
Solution 2: Using assume-unchanged
for Tracked Files
When to Use assume-unchanged
If a file is already tracked by Git but you want to stop tracking changes to it locally, use the assume-unchanged
flag:
# Stop tracking changes to a file
git update-index --assume-unchanged file.txt
# Resume tracking changes
git update-index --no-assume-unchanged file.txt
Common Use Cases for assume-unchanged
# Local environment configuration
git update-index --assume-unchanged config/database.yml
# Personal IDE settings
git update-index --assume-unchanged .vscode/settings.json
# Local development scripts
git update-index --assume-unchanged scripts/local-setup.sh
Solution 3: Removing Files from Tracking
If a File is Already Committed
Sometimes you need to stop tracking a file that's already in your repository:
# Remove file from tracking but keep it locally
git rm --cached file.txt
# Add to .gitignore to prevent future tracking
echo "file.txt" >> .gitignore
# Commit the change
git commit -m "Stop tracking file.txt"
Removing Multiple Files
# Remove all files matching a pattern
git rm --cached *.log
# Remove entire directories
git rm --cached -r logs/
# Add patterns to .gitignore
echo "*.log" >> .gitignore
echo "logs/" >> .gitignore
Real-World Examples
Example 1: Environment Configuration
Scenario: You have a .env
file with sensitive information that should never be committed.
# Create .gitignore
echo ".env" >> .gitignore
echo ".env.local" >> .gitignore
echo ".env.*.local" >> .gitignore
# Create a template file for other developers
cp .env .env.example
git add .env.example
git commit -m "Add environment configuration template"
Example 2: IDE Settings
Scenario: You want to share some IDE settings but keep personal preferences private.
# Keep shared settings
git add .vscode/settings.json
# Ignore personal settings
echo ".vscode/settings.json" >> .gitignore
git update-index --assume-unchanged .vscode/settings.json
Example 3: Build Artifacts
Scenario: You have build outputs that shouldn't be tracked.
# Add build directories to .gitignore
echo "dist/" >> .gitignore
echo "build/" >> .gitignore
echo "*.o" >> .gitignore
echo "*.so" >> .gitignore
# Remove existing build files from tracking
git rm --cached -r dist/
git rm --cached -r build/
git commit -m "Stop tracking build artifacts"
Best Practices for File Ignoring
1. Use .gitignore
for Team-Wide Ignoring
# Good: Everyone ignores these files
*.log
node_modules/
.env
2. Use assume-unchanged
for Personal Preferences
# Good: Personal IDE settings
git update-index --assume-unchanged .vscode/settings.json
3. Create Template Files
# Create templates for configuration files
cp config.example config
echo "config" >> .gitignore
4. Document Your Ignoring Strategy
# Add comments to .gitignore
# Environment variables
.env
.env.local
# Build outputs
dist/
build/
# IDE files
.vscode/
.idea/
Common Pitfalls and Solutions
Pitfall 1: Adding Files to .gitignore
After Committing
Problem: Files are already tracked, so .gitignore
doesn't work
Solution: Use git rm --cached
to untrack the files first
Pitfall 2: Using assume-unchanged
for Security
Problem: Thinking assume-unchanged
prevents others from seeing changes
Solution: Use .gitignore
and proper security practices for sensitive files
Pitfall 3: Ignoring Too Much
Problem: Ignoring files that should be tracked Solution: Be selective and document your ignoring decisions
Pitfall 4: Not Updating .gitignore
for New File Types
Problem: New file types keep showing up in Git status
Solution: Regularly review and update your .gitignore
patterns
Advanced Techniques
Global .gitignore
Create a global .gitignore
file for files you never want to track:
# Create global .gitignore
git config --global core.excludesfile ~/.gitignore_global
# Add common patterns
echo ".DS_Store" >> ~/.gitignore_global
echo "*.swp" >> ~/.gitignore_global
echo "*.swo" >> ~/.gitignore_global
Conditional .gitignore
Use different .gitignore
files for different environments:
# .gitignore (base)
*.log
node_modules/
# .gitignore.local (personal)
.vscode/settings.json
.idea/
Using .gitattributes
For more advanced file handling:
# .gitattributes
*.log filter=lfs diff=lfs merge=lfs -text
*.env filter=lfs diff=lfs merge=lfs -text
Security Considerations
1. Never Commit Sensitive Information
# Bad: Committing sensitive data
git add .env
git commit -m "Add environment configuration"
# Good: Using .gitignore
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Add .gitignore for environment files"
2. Use Environment Variables
# Instead of hardcoding values
DATABASE_URL=postgres://user:password@localhost/db
# Use environment variables
DATABASE_URL=${DATABASE_URL}
3. Regular Security Audits
# Check for sensitive information in your repository
git log --all --full-history -- "*.env"
git log --all --full-history -- "*.key"
git log --all --full-history -- "*.pem"
Conclusion
Proper file ignoring is essential for maintaining a clean, secure, and efficient Git repository. By using .gitignore
for team-wide ignoring and assume-unchanged
for personal preferences, you can keep your repository organized while allowing each developer to have their own local configuration.
Remember:
- Use
.gitignore
for files that should never be tracked - Use
assume-unchanged
for personal preferences on tracked files - Remove files from tracking if they're already committed
- Create template files for configuration that others need
- Document your ignoring strategy for team clarity