.gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.
Overview
A.gitignore file tells Git which files (or patterns) to ignore. This is useful for:
- Build artifacts and generated files
- Dependencies and packages
- IDE and editor configuration
- Operating system files
- Temporary files and caches
- Sensitive data like credentials
File Locations
Git checks for ignore patterns in multiple locations:Repository .gitignore
Location: Root or any directory in the repository
Use for: Files that all developers should ignore (build output, dependencies)
Per-Repository Exclude
Location:.git/info/exclude
Use for: Files specific to your workflow that shouldn’t be shared
Global Ignore
Location: Configured viacore.excludesFile
Use for: Files to ignore across all repositories (OS files, editor config)
Pattern Format
Basic Patterns
Pattern Rules
Blank Lines and Comments
Blank Lines and Comments
- Blank lines are ignored (can separate groups for readability)
- Lines starting with
#are comments - Use
\#to match files starting with#
Wildcards
Wildcards
*matches zero or more characters (except/)?matches exactly one character[abc]matches any character in the set[0-9]matches any character in the range
Directory Separator
Directory Separator
/at the start matches from repository root/at the end matches only directories/in the middle anchors the pattern to that level
Negation
Negation
!negates a pattern (re-includes files)- Cannot re-include a file if parent directory is excluded
- Prefix with
\!to match files starting with!
Double Asterisk
Double Asterisk
**/at start matches in all directories/**at end matches everything inside/**/in middle matches zero or more directories
Common Patterns
Node.js Project
Python Project
Java Project
macOS
Windows
IDE and Editors
Examples
Ignore Everything Except Specific Files
Ignore Everything in a Directory Except One Subdirectory
Ignore Files Only at Root
Pattern Matching Examples
Working with .gitignore
Check if a File is Ignored
Check Multiple Files
Debug Ignore Patterns
Stop Tracking Already Tracked Files
If you add a pattern to.gitignore but the file is already tracked:
Remove All Ignored Files
Global .gitignore Setup
Create Global Ignore File
Recommended Global Patterns
Best Practices
Commit .gitignore
Always commit
.gitignore to share ignore patterns with your teamIgnore Early
Add patterns before creating files to avoid accidentally committing them
Use Global for Personal Files
Keep editor and OS-specific ignores in global
.gitignoreUse Comments
Document why files are ignored, especially non-obvious patterns
Don’t Ignore
.gitignoreitself- Configuration templates (e.g.,
config.example.js) - Empty directory placeholders (
.gitkeep) - Lock files (
package-lock.json,Gemfile.lock)
Always Ignore
- Secrets and credentials
- API keys and tokens
- Database files
- Build artifacts
- Dependencies (
node_modules,vendor) - Generated files
Templates
GitHub maintains a collection of useful.gitignore templates:
Visit github/gitignore for more templates.
Troubleshooting
File Still Being Tracked
Problem: Added pattern to.gitignore but file still appears in git status
Solution: The file was already tracked. Remove it from the index:
Pattern Not Working
Problem: Pattern doesn’t match expected files Solution: Usegit check-ignore to debug:
Cannot Re-include File in Ignored Directory
Problem: Trying to track a file inside an ignored directory Solution: Exclude the directory contents, then re-include specific files:See Also
- Configuration Overview - Git configuration system
- .gitattributes - Path-specific Git attributes
- .gitconfig Reference - Git configuration variables
