gitignore
git
exclude-folder
include-subfolder
version-control

.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

  1. Blank Lines - Ignored lines used as separators.
  2. Comments - Lines starting with # used for comments.
  3. Wildcards - Use * for wildcard matches.
  4. 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:

 
1project/
23├── assets/
4│   ├── images/
5│   ├── videos/
6│   └── docs/
7|
8└── source/
9    ├── main.py
10    ├── config/
11    └── utils/

If you want to ignore the entire assets directory but include the docs subdirectory, your .gitignore file should be structured like this:

plaintext
1# Ignore everything in the assets directory
2/assets/*
3
4# Exclude (i.e., do not ignore) the docs subdirectory
5!/assets/docs/

Explanation

  • The line /assets/* instructs Git to ignore everything within the assets directory.
  • The negation line !/assets/docs/ overrides the previous rule for the docs subdirectory, thus including it in the version control.

Advanced Techniques

Combining Patterns

You can create more complex .gitignore rules using multiple patterns:

plaintext
# Ignore all `.log` files in the `logs` directory, except critical logs
logs/*.log
!logs/critical.log

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:

plaintext
1# Ignore all node modules, but include specific utility
2/node_modules/*
3!/node_modules/utilities
4!/node_modules/utilities/helper.js

Key Considerations

  1. Order Matters: Patterns are applied in sequence. Ensure your negations appear after the patterns they are meant to override.
  2. Directory-Specific Negate Patterns: Only directories have a trailing slash in their pattern (e.g., /dirname/).
  3. Global Git Ignore: Consider global ignores set in ~/.config/git/ignore, as these can influence multiple repositories across your user profile.

Summary Table

FeatureDescriptionExample
Exclude Whole FolderUse /* after the folder name to exclude all contents/assets/*
Include Specific SubfolderUse ! followed by the subfolder path!/assets/docs/
Wildcard UsageUse * for matching any number of characters*.log for all .log files
Negation (Excluding Exceptions)Use ! to include files previously ignored!main.log
Order RequirementNegation patterns must come after the exclusion pattern they override-
Directory SpecificityUse 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 .gitignore file using $ git config --global core.excludesFile '&#126;/.config/git/ignore'.
  • Tracking Changes: Keep .gitignore patterns 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.


Course illustration
Course illustration

All Rights Reserved.