How do I configure a .gitignore file to ignore everything except specific files?
I understand that a .gitignore file cloaks specified files from Git’s version control. How do I tell .gitignore to ignore everything except the files I’m tracking with Git? Something like:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
To configure a .gitignore file to ignore everything except specific files, you need to use negative patterns with the ! prefix. While your approach of using * to ignore everything is correct, Git doesn’t provide a direct way to “unignore” specific files after a wildcard. Instead, you must use the ! prefix to explicitly include specific files that would otherwise be ignored.
- Understanding .gitignore Syntax
- Ignoring Everything Except Specific Files
- Advanced Techniques and Best Practices
- Common Pitfalls and Solutions
- Alternative Approaches
- Practical Examples
Understanding .gitignore Syntax
The `.gitignore file uses specific patterns to determine which files and directories should be excluded from Git tracking. Understanding the basic syntax is crucial for effective configuration.
Basic Pattern Rules
*matches any sequence of characters (except directory separators)**matches any sequence of characters including directory separators?matches any single character[abc]matches any character in the set[a-z]matches any character in the range!prefix negates a pattern (makes it an exception)
Wildcard Behavior
Wildcards in .gitignore work differently than in many other systems. When you use * at the beginning of a pattern, it matches any file name, but once a file is ignored, you cannot easily “unignore” it without using negative patterns.
Ignoring Everything Except Specific Files
The correct approach to ignore everything except specific files requires a combination of wildcard patterns and negative exceptions.
Basic Method
# Ignore everything by default
*
# But include these specific files
!.gitignore
!script.pl
!template.latex
Important Considerations
File Order Matters: Git processes .gitignore files in order. Files that match earlier patterns take precedence over later ones.
Directory-Specific Rules: The above approach works for files in the root directory. For files in subdirectories, you need to be more specific:
# Ignore everything
*
# Include specific files in root directory
!.gitignore
!script.pl
!template.latex
# Include files in subdirectories
!src/important.js
!docs/readme.md
Pattern Limitations
The * wildcard does not ignore directories - it only ignores files. To ignore directories as well, you need additional patterns:
# Ignore all files
*
# But keep these files
!.gitignore
!script.pl
!template.latex
# Ignore all directories explicitly
*/
# But keep specific directories
!src/
!docs/
Advanced Techniques and Best Practices
Using Negative Patterns Effectively
Negative patterns (prefixed with !) are powerful but have limitations:
- Cannot Negate Directory-wide Wildcards: You cannot do
!*/to include all files in a directory - Must Appear After Corresponding Wildcard: The negative pattern must come after the general pattern it overrides
Alternative: Git Attributes
For more complex scenarios, consider using .gitattributes file:
# Set the export-ignore attribute for all files
* export-ignore
# But unset it for specific files
!.gitignore export-ignore
!script.pl export-ignore
!template.latex export-ignore
Pattern Combination Strategies
# Comprehensive approach
# Ignore all files
*
# Ignore all directories
*/
# Include root-level specific files
!.gitignore
!script.pl
!template.latex
# Include specific directories and their contents
!src/
!src/*
!docs/
!docs/*
Common Pitfalls and Solutions
Issue: Already Tracked Files
Files that are already tracked by Git cannot be ignored by adding them to .gitignore. You must first untrack them:
git rm --cached filename
Issue: Directory Contents
When you ignore a directory with *, Git still tracks the directory structure. To ignore entire directories:
# Ignore directories and their contents
node_modules/
build/
dist/
Issue: Case Sensitivity
Git’s pattern matching can be case-sensitive on some systems. Be consistent with file naming.
Issue: Git Status Shows Ignored Files
If Git status shows ignored files when you don’t want them to:
git status --ignored # See ignored files
git status --no-ignored # Hide ignored files
Alternative Approaches
Method 1: Start Fresh with New Repository
For new repositories, start with a comprehensive .gitignore:
# Ignore everything
*
# Include essential files
!.gitignore
!README.md
!.gitattributes
!LICENSE
Method 2: Use Includes Instead of Excludes
Sometimes it’s better to explicitly include what you want:
# Only include specific file types
!.gitignore
!*.md
!*.txt
!*.js
!*.py
!script.pl
!template.latex
# Ignore everything else
Method 3: Directory-Specific .gitignore Files
Create .gitignore files in specific directories:
/
├── .gitignore # Root level
├── src/
│ └── .gitignore # Source-specific
└── docs/
└── .gitignore # Documentation-specific
Practical Examples
Example 1: Simple Project Structure
# Ignore everything
*
# Keep project configuration and source files
!.gitignore
!package.json
!README.md
!script.pl
!template.latex
# Keep source directory
!src/
!src/*
Example 2: Web Project with Build Output
# Ignore all files
*
# Keep configuration and source
!.gitignore
!package.json
!webpack.config.js
!src/
!public/
!dist/
# But ignore build output within directories
src/node_modules/
src/build/
public/build/
Example 3: Research Project with Data Files
# Ignore everything
*
# Keep project files
!.gitignore
!README.md
!data/
!data/processed/
!script.pl
!template.latex
# Ignore raw data and temporary files
data/raw/
data/temp/
*.tmp
Sources
- Official Git Documentation - gitignore
- Pro Git Book - Ignoring Files
- GitHub Guide - Ignoring Files
- Stack Overflow - How to ignore all files except specific ones
- Atlassian Git Tutorial - .gitignore best practices
Conclusion
To effectively ignore everything except specific files in Git:
- Start with
*to ignore all files, then use!prefix to include exceptions - Remember that order matters in .gitignore files - patterns are processed sequentially
- For directories, combine
*with*/patterns - Files already tracked by Git must be untracked with
git rm --cachedbefore being ignored - Consider alternative approaches like starting with an empty repository or using directory-specific .gitignore files
The key insight is that Git’s .gitignore works by exclusion, so you must explicitly “unignore” files using the ! prefix rather than trying to create an “include list.” This approach provides the most control and flexibility for managing your Git repository contents.