Git ignore all except subfolder

Git ignore all except subfolder

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Git ignore all except subfolder

Solving Git Ignore Challenges: Excluding Everything Except Specific Subfolders

As a seasoned developer, I frequently encounter scenarios where we need to exclude most of our project files from version control while keeping certain critical folders intact. This is especially common in e-commerce platforms like Magento, which often have large file structures that don’t require versioning.

Understanding the Current Git Ignore Strategy

Let’s analyze your current .gitignore strategy:

*
!/app/
!/app/*

This rule tells Git to ignore everything (*) except for the app/ directory and its immediate contents. However, the subsequent rules are contradictory:

app/*
!/app/design/
!/app/design/*

The second set of rules tries to ignore everything inside app/ (including app/design/) unless it’s specifically excluded—essentially trying to do what the first rule already accomplished.

The Problem with Your Current Approach

Your current approach faces two primary issues:

  1. Lack of Recursive Exclusion: The ! symbol before /app/design/ only excludes that specific folder, not its subfolders. This is why your test file inside a subfolder of design was tracked.

  2. Conflicting Rules: Having multiple rules for the same directory (app/) can lead to confusing behavior and unexpected exclusions.

A More Efficient Git Ignore Strategy

To solve this, we need a more precise approach that clearly defines what to include and exclude:

  1. Use ! to explicitly include only the desired folders.
  2. Avoid using conflicting rules for the same directory.
  3. Ensure you use recursive exclusion when necessary.

Here’s an updated version of your .gitignore file that should work:

# Ignore everything except these two critical directories and their subfolders
!/app/design/
!/app/design/*
!/skin/
!/skin/*

How This Solution Works

  • The first two lines explicitly include the app/design/ directory and all its subdirectories (app/design/*) using the exclamation mark (!).
  • Similarly, the last two lines do the same for the skin/ directory.
  • By placing these specific exclusions at the top of your .gitignore file, you ensure they take precedence over any general “ignore everything” rules.

When to Use This Strategy

This approach is ideal when:

  1. You have a large project with many files that don’t require versioning (like theme files in Magento).
  2. You want to keep specific directories under version control while ignoring the rest.
  3. You need to ensure subdirectories are also excluded.

Conclusion: Effective Git Ignoring for Better Version Control

Remember, effective use of .gitignore is crucial for maintaining a clean and efficient repository. By carefully crafting your exclusion rules, you can ensure that only necessary files are tracked while keeping sensitive or unimportant data out of version control.

This strategy not only saves disk space but also prevents merging conflicts and makes it easier to manage your project history. For more in-depth guides on Git best practices, feel free to explore our extensive library at Laravel Company.

Themes Errors

Our network