What is .gitignore?
A complete guide to understanding, using, and troubleshooting .gitignore files in Git version control.
Introduction to .gitignore
A .gitignore file is a
text file that tells Git which files or directories to ignore in a project. It's a crucial
tool
for keeping your repository clean and preventing sensitive or unnecessary files from being
tracked.
When you add a file to .gitignore, Git will completely ignore it—it won't be tracked, staged, or committed. This is essential for excluding build artifacts, local configuration files, dependencies, and other files that shouldn't be part of your version control.
✅ Why Use .gitignore?
- Keep repository size small
- Prevent sensitive data exposure
- Avoid conflicts in team projects
- Improve build performance
- Maintain clean commit history
📁 Common Files to Ignore
- Build artifacts (
dist/,build/) - Dependencies (
node_modules/,vendor/) - Local configs (
.env,.env.local) - IDE files (
.vscode/,.idea/) - OS files (
.DS_Store,Thumbs.db)
How to Use .gitignore
Creating a .gitignore File
Create a file named .gitignore
in the root of your Git repository:
touch .gitignore
Or create it directly in your code editor (VS Code, Sublime Text, etc.)
Basic Syntax and Patterns
.gitignore uses simple pattern matching. Here are the most common patterns:
Pattern Examples
- *.log Ignore all .log files
- node_modules/ Ignore entire directory
- /.env Ignore .env in root only
- **/temp/ Ignore temp dirs anywhere
- !important.log Exception: don't ignore
- # comment Comments start with #
Pattern Cheatsheet
*- Matches any characters?- Matches single character**/- Matches zero or more directories/at start - Root directory/at end - Directory only!- Negation (don't ignore)[]- Character ranges
Common Patterns to Include
Here are common entries for different project types. Click to copy:
# Dependencies
node_modules/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Environment variables
.env
.env.local
.env.*.local
# Build outputs
dist/
build/
*.exe
*.dll
# IDE files
.vscode/
.idea/
*.swp
*.swo
# OS files
.DS_Store
Thumbs.db
Applying .gitignore
After creating or updating .gitignore, you need to apply it:
Important Warning
This command removes all files from cache and re-adds them. Make sure you have no uncommitted changes first!
git rm -r --cached . && git add . && git commit -m "Apply
.gitignore"
Alternative: Apply to specific files
If you only want to apply .gitignore to specific already-tracked files:
git rm --cached filename
Troubleshooting: .gitignore Not Working
If your .gitignore file isn't working as expected, here are common issues and solutions:
Files Already Tracked
Git is already tracking files you want to ignore
Solution:
Remove them from Git tracking:
git rm --cached filename
git rm -r --cached directory/
Incorrect File Location
.gitignore file is not in the correct location
Solution:
Ensure .gitignore is in the root of your Git repository. Check with:
find . -name ".gitignore" -type f
Pattern Syntax Errors
Incorrect pattern syntax in .gitignore
Solution:
Check your patterns:
node_modules/
node_modules
*.log
.log
Case Sensitivity
Case-sensitive file systems causing issues
Solution:
Git is case-sensitive. Ensure your patterns match the exact case:
# On macOS/Linux: these are different
.DS_Store
.ds_store
Global .gitignore Conflicts
Global .gitignore file overriding local rules
Solution:
Check global gitignore configuration:
git config --global core.excludesfile
~/.gitignore_global
Best Practices
✅ Do
- Ignore build artifacts and dependencies - Keep your repo clean of generated files
- Ignore local configuration files - Never commit
.envor local settings - Use specific patterns over broad ones -
*.logis better than* - Comment your .gitignore file - Add context for team members
- Test your patterns - Use
git check-ignore -v filename - Use global .gitignore for personal files - IDE files, OS files, etc.
- Review before committing - Use
git status --ignored - Keep it organized - Group related patterns with comments
❌ Don't
- Ignore source code files - Only ignore generated/transient files
- Commit sensitive data then try to ignore it - Once committed, it's in history
- Use overly broad patterns - Avoid
*or**/*without good reason - Forget to update .gitignore - Add new dependencies as you add them
- Ignore files that should be tracked - Configuration templates should be tracked
- Use trailing spaces - They can cause pattern matching issues
- Mix OS-specific patterns - Keep Windows, macOS, Linux patterns separate
- Ignore without understanding - Know why you're ignoring each pattern
Advanced Tips
📁 Organization Example
# ================
# Dependencies
# ================
node_modules/
vendor/
*.jar
*.war
# ================
# Build Outputs
# ================
dist/
build/
*.exe
*.dll
# ================
# Environment
# ================
.env
.env.local
.env.*.local
# ================
# IDE Files
# ================
.vscode/
.idea/
*.swp
*.swo
# ================
# OS Files
# ================
.DS_Store
Thumbs.db
desktop.ini
🔧 Helpful Commands
git check-ignore -v path/to/file
git ls-files --others --ignored --exclude-standard
git clean -n
Frequently Asked Questions
Why isn't my .gitignore file working?
The most common reasons are:
- The file is already tracked by Git (use
git rm --cached filename) - The .gitignore file is not in the root directory
- Pattern syntax errors (missing trailing slash for directories)
- Case sensitivity issues on macOS/Linux
- Global .gitignore file overriding local rules
git check-ignore -v path/to/file
Can I have multiple .gitignore files?
Yes! You can have:
- Global .gitignore: Applies to all repositories on your system
- Local .gitignore: In the root of your repository
- Nested .gitignore files: In subdirectories for specific rules
git config --global core.excludesfile
~/.gitignore_global
git config --global core.excludesfile
How do I remove a file from Git after committing it?
Once a file is committed, it's in your Git history. To remove it:
Important Warning
This rewrites Git history. Don't do this on shared branches!
git filter-branch --force --index-filter 'git rm --cached
--ignore-unmatch filename' --prune-empty --tag-name-filter cat --
--all
What's the difference between *.log
and **/*.log?
Pattern matching differences:
*.log
- Matches files in current directory only
- Example:
app.log,error.log - Does not match:
logs/app.log
**/*.log
- Matches files in any directory
- Example:
logs/app.log,var/log/error.log - Also matches:
app.log(current directory)
Should I commit my .gitignore file?
Yes, absolutely! Your .gitignore file should be committed because:
- It's part of your project configuration
- Team members need the same ignore rules
- CI/CD systems need to know what to ignore
- It documents what files should be excluded
Best Practice
Commit a .gitignore template and let
team members customize their local copies if needed.
Tools and Resources
Helpful Git Commands
git check-ignore -v filename- Check why a file is ignoredgit ls-files --others --ignored --exclude-standard- List ignored filesgit status --ignored- Show ignored files in statusgit clean -n- Show what would be removed (dry run)git clean -f- Remove untracked files
Online Resources
Need a .gitignore Template?
Browse our curated collection of .gitignore templates for popular frameworks, languages, and tools.
Browse Templates