gitignore
subfolders
git
version control
software development

Multiple .gitignore in subfolders

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Git is a powerful version control system that helps developers manage changes to codebases. One of its essential features is the `.gitignore` file, which specifies intentionally untracked files to be ignored by Git. While many are familiar with a single `.gitignore` at the repository's root, it is possible and sometimes beneficial to have multiple `.gitignore` files across different subfolders. This article will delve into the mechanics and use cases of multiple `.gitignore` files within a Git repository.

Understanding `.gitignore`

The purpose of the `.gitignore` file is to prevent specific files and directories from being tracked by Git. This is useful for excluding build artifacts, sensitive information, or temporary files that do not belong in a versioned project. The `.gitignore` file uses pattern matching, allowing for simple lines that specify the file patterns to be ignored.

Multiple `.gitignore` Files

In Git, each directory can have its own `.gitignore` file. These files are cumulative. If a `.gitignore` rule in a subdirectory overlaps with one defined in a parent directory, both rules will be applied. This can allow for more granular control and organization.

How It Works

When Git traverses the directory tree, it checks for a `.gitignore` file in the current directory and applies those ignore rules. Here’s how it typically functions:

  1. Root-Level .gitignore: Applies globally across the entire repository.
  2. Subfolder .gitignore: Applies only within its directory hierarchy, including any subdirectories.

Example Structure

Let's consider a project with the following directory structure:

  • Root-Level `.gitignore` might contain:
  • `src/.gitignore` might contain:
  • `tests/unit/.gitignore` might contain:
  • `*.log` - Ignoring all files with a `.log` extension.
  • `/node_modules/` - Ignoring the `node_modules` directory.
  • `!important.txt` - Specifying an exception, ensuring `important.txt` is tracked.
  • `#` - Lines starting with `#` are comments.
  • Documentation: Always document the intent of complex ignore rules with comments.
  • Consistency: While multiple files provide flexibility, strive for consistency to avoid confusion.
  • Hierarchy Awareness: Be mindful of the repository structure and understand that subdirectory `.gitignore` files only take effect within their scope.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.