.gitignore exclude folder but include specific subfolder
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Git, it is common to encounter scenarios where you want to exclude an entire directory but still include certain subdirectories or files within it. This is achievable using a .gitignore file in your repository. In this article, we'll explore how to effectively use .gitignore to exclude folders while including specific subfolders, backed by technical explanations and examples.
Understanding .gitignore
The .gitignore file is utilized to specify the files and directories that Git should ignore in a repository. This is particularly useful for excluding auto-generated files, logs, build artifacts, or any other files you don't want to track.
Basic Syntax of .gitignore
- Blank Lines - Ignored lines used as separators.
- Comments - Lines starting with
#used for comments. - Wildcards - Use
*for wildcard matches. - Negation - Use
!to negate a pattern, meaning that matched files will be included despite previous patterns suggesting otherwise.
Example of .gitignore Syntax
Suppose your project structure is as follows:
If you want to ignore the entire assets directory but include the docs subdirectory, your .gitignore file should be structured like this:
Explanation
- The line
/assets/*instructs Git to ignore everything within theassetsdirectory. - The negation line
!/assets/docs/overrides the previous rule for thedocssubdirectory, thus including it in the version control.
Advanced Techniques
Combining Patterns
You can create more complex .gitignore rules using multiple patterns:
Hierarchical Patterns
Patterns work hierarchically. If you exclude a top-level directory, all its contents are also excluded unless specified otherwise. This makes it possible to exclude entire directories but only include specific files or subdirectories:
Key Considerations
- Order Matters: Patterns are applied in sequence. Ensure your negations appear after the patterns they are meant to override.
- Directory-Specific Negate Patterns: Only directories have a trailing slash in their pattern (e.g.,
/dirname/). - Global Git Ignore: Consider global ignores set in
~/.config/git/ignore, as these can influence multiple repositories across your user profile.
Summary Table
| Feature | Description | Example |
| Exclude Whole Folder | Use /* after the folder name to exclude all contents | /assets/* |
| Include Specific Subfolder | Use ! followed by the subfolder path | !/assets/docs/ |
| Wildcard Usage | Use * for matching any number of characters | *.log for all .log files |
| Negation (Excluding Exceptions) | Use ! to include files previously ignored | !main.log |
| Order Requirement | Negation patterns must come after the exclusion pattern they override | - |
| Directory Specificity | Use trailing slash for directories | /dirname/ |
Additional Tips
- Check Ignored Files: Use
$ git check-ignore -v <filename>to diagnose why a file is being ignored. - Global Exclusions: To apply ignore rules across all your projects, you can add patterns to a global
.gitignorefile using$ git config --global core.excludesFile '~/.config/git/ignore'. - Tracking Changes: Keep
.gitignorepatterns version-controlled so all team members see and enforce consistent ignore patterns.
By strategically using .gitignore, you can cleanly manage which files are tracked in your project, optimizing both your version control practices and overall project management.

