git
gitignore
file extension
directory
version control

gitignore all files of extension in directory

Interview Questions practice on Codemia

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

Browse interview questions

Introduction

To ignore all files with a given extension inside a specific directory, use a .gitignore pattern that includes the directory path. The key is to decide whether you want to ignore files only in that exact directory, in nested subdirectories too, or everywhere in the repository.

Ignore by Extension in One Specific Directory

If you want to ignore all .log files only in a directory named tmp, write:

gitignore
tmp/*.log

This matches files such as:

  • 'tmp/app.log'
  • 'tmp/errors.log'

It does not match:

  • 'tmp/archive/old.log'
  • 'other/tmp/app.log'

That is the right pattern when the scope is one known directory level.

Ignore Recursively Under a Directory

If the directory contains subdirectories and you want to ignore matching files there too, use a recursive pattern:

gitignore
tmp/**/*.log

This matches:

  • 'tmp/app.log'
  • 'tmp/archive/old.log'
  • 'tmp/2026/03/system.log'

So the distinction is:

  • 'tmp/*.log for one directory level'
  • 'tmp/**/*.log for recursive matching under that path'

Be explicit about which behavior you need.

Ignore Everywhere by Extension

If the goal is to ignore all files of that extension anywhere in the repository, use:

gitignore
*.log

That is broader than a directory-specific rule. It is useful for editor swap files, logs, or generated artifacts that should never be tracked anywhere.

Anchor the Pattern to the Repository Root

Patterns without a leading slash can match more broadly than people expect. If you want the path relative to the repository root, anchor it:

gitignore
/tmp/*.log

This means:

  • match the tmp directory at the repository root
  • do not match another tmp directory nested elsewhere

That distinction matters in monorepos and large repositories with repeated folder names.

Ignore a Directory but Re-Allow Specific Files

Sometimes you want to ignore most files of an extension but keep one example or template file tracked.

gitignore
/tmp/*.log
!/tmp/keep-this.log

Git processes .gitignore from top to bottom, so later negation rules can re-include paths.

This is useful for:

  • sample config files
  • placeholder directories
  • one tracked baseline artifact among many generated ones

Already Tracked Files Are Different

.gitignore affects untracked files. If files were already committed, adding a pattern does not untrack them automatically.

For example, if tmp/app.log is already in Git history, this:

gitignore
/tmp/*.log

will not remove it from the index.

To stop tracking it while keeping the local file:

bash
git rm --cached tmp/app.log

For a whole set of files:

bash
git rm --cached tmp/*.log

Then commit the removal along with the .gitignore change.

Test Your Pattern

The fastest way to confirm whether Git is ignoring a file for the reason you expect is:

bash
git check-ignore -v tmp/app.log

This shows:

  • which .gitignore file matched
  • which line provided the rule

That is much faster than guessing, especially when multiple ignore files are involved.

Common Mistakes

People often mix up these cases:

  • exact directory path versus any directory with that name
  • one-level matching versus recursive matching
  • untracked files versus already tracked files

Those are separate behaviors. The pattern syntax is small, but the semantics matter.

Common Pitfalls

  • Writing *.ext when you only meant one directory.
  • Using dir/*.ext and expecting it to match subdirectories recursively.
  • Forgetting the leading slash when you intend a repository-root path.
  • Assuming .gitignore will automatically remove already tracked files.
  • Debugging by guesswork instead of using git check-ignore -v.

Summary

  • Use dir/*.ext to ignore one extension in one specific directory level.
  • Use dir/**/*.ext to ignore recursively under that directory.
  • Use /dir/*.ext to anchor the pattern at the repository root.
  • '.gitignore affects untracked files, not files already committed.'
  • Use git check-ignore -v to verify which rule matched.

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.