Nested Gitignores
What
A .gitignore file does not have to reside exclusively at the root of your repository. You can place separate, nested .gitignore files inside any subdirectory of your project folder.
How
A nested .gitignore file applies its ignoring rules exclusively to the directory where it resides and all of its subdirectories:
# Project directory structure:
project/
├── .gitignore (root)
└── src/
├── assets/
│ ├── .gitignore (nested)
│ ├── cover_art.jpg
│ └── onlydevs.png
└── main.py
If src/assets/.gitignore contains:
onlydevs.png
main.py
src/assets/onlydevs.png: Ignored (matches pattern insidesrc/assets/.gitignore).src/main.py: Not ignored (the patternmain.pyinsidesrc/assets/.gitignoreonly scopes files located insidesrc/assets/or below).
Why
Using nested .gitignore files keeps ignoring patterns highly modular. Instead of maintaining a single, massive root-level .gitignore containing hundreds of complex path strings for different sub-projects, teams can place local .gitignore files inside specific frontend, backend, or assets folders to manage their respective exclusions independently.
Summary
Nested .gitignore files scope their exclusion rules strictly to their parent directory and its children, allowing modular control over project file tracking.