Exclude a directory from 'git diff'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want git diff to ignore a directory, the right tool is an exclude pathspec, not .gitignore. Ignore rules affect untracked files, while git diff needs to be told which tracked paths to include or exclude for that specific comparison.
Use an Exclude Pathspec
The basic pattern is:
This command means:
- diff everything under the current directory
- but skip the
dist/directory
The -- matters because it tells Git that the remaining arguments are paths, not revisions or options.
If the directory is nested, give the repository-relative path:
Git applies the exclusion before rendering the diff, which is cleaner and more reliable than filtering the output afterward.
Use the Same Trick for Commit Comparisons
Pathspec exclusions also work when you compare revisions:
That is useful during review when you want to compare two branches but ignore generated output, vendored files, or large build artifacts.
You can exclude more than one directory by repeating the pathspec:
This keeps the diff focused on the files a reviewer can actually reason about.
Why .gitignore Does Not Help Here
.gitignore only affects untracked file discovery. Once a file is tracked by Git, ignore rules do not suppress it from git diff.
That distinction is the source of a lot of confusion:
- '
.gitignoresays whether new untracked files should be noticed' - '
git diffpathspecs say which existing tracked paths participate in a diff command'
If the directory should never have been tracked in the first place, then the repository may need a broader cleanup. But that is a separate decision from “show me a cleaner diff right now.”
Prefer Pathspecs Over Output Filtering
Some people try to hide a directory by piping diff output through grep -v or similar shell filters. That is fragile for several reasons:
- it can remove context lines rather than whole files,
- it does not understand Git’s diff structure,
- it breaks more easily when filenames or content change.
Using Git’s own path filtering keeps the command honest:
This asks Git to omit those paths at the source, which is the safer approach.
When .gitattributes Is the Better Tool
If you do not want a one-off command but a persistent policy for generated files, .gitattributes can disable diffs for a path pattern across the repository.
Example:
That is not the same as an exclude pathspec. It changes repository behavior for everyone who uses the repo. Use it when the team agrees that a certain path should not produce meaningful diffs, not when you only want a temporary filtered view.
Practical Examples
Hide a generated folder while reviewing your working tree:
Compare staged changes but skip build output:
Compare your feature branch to main while excluding vendored code:
Once you start using exclude pathspecs, most “exclude from diff” problems become straightforward.
Common Pitfalls
- Expecting
.gitignoreto hide tracked directory changes fromgit diff. - Forgetting the
--separator and making Git misread a path as a revision. - Writing the exclude path with the wrong repository-relative location.
- Filtering diff output with shell tools instead of excluding the path inside Git.
- Using
.gitattributesfor a one-off local need that should really stay at the command level.
Summary
- Exclude directories from
git diffwith pathspecs such asgit diff -- . ':(exclude)dist/'. - The same technique works for working tree diffs, staged diffs, and commit comparisons.
- '
.gitignoredoes not suppress tracked files from diff output.' - Pathspec filtering is more reliable than post-processing diff text with shell commands.
- Use
.gitattributesonly when the repository wants a persistent diff policy.

