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.
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:
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:
This matches:
- '
tmp/app.log' - '
tmp/archive/old.log' - '
tmp/2026/03/system.log'
So the distinction is:
- '
tmp/*.logfor one directory level' - '
tmp/**/*.logfor 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:
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:
This means:
- match the
tmpdirectory at the repository root - do not match another
tmpdirectory 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.
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:
will not remove it from the index.
To stop tracking it while keeping the local file:
For a whole set of files:
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:
This shows:
- which
.gitignorefile 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
*.extwhen you only meant one directory. - Using
dir/*.extand expecting it to match subdirectories recursively. - Forgetting the leading slash when you intend a repository-root path.
- Assuming
.gitignorewill automatically remove already tracked files. - Debugging by guesswork instead of using
git check-ignore -v.
Summary
- Use
dir/*.extto ignore one extension in one specific directory level. - Use
dir/**/*.extto ignore recursively under that directory. - Use
/dir/*.extto anchor the pattern at the repository root. - '
.gitignoreaffects untracked files, not files already committed.' - Use
git check-ignore -vto verify which rule matched.
Related reading
- .gitignore all the .DS_Store files in every folder and subfolder
- .gitignore all the .DS_Store files in every folder and subfolder
- .gitignore and The following untracked working tree files would be overwritten by checkout
- .gitignore and The following untracked working tree files would be overwritten by checkout
- gitignore does not ignore .idea directory
- .gitignore exclude folder but include specific subfolder
- .gitignore exclude folder but include specific subfolder
- .gitignore files added inside Git submodules
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.