git
git diff
directory
exclude
tutorial

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:

bash
git diff -- . ':(exclude)dist/'

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:

bash
git diff -- . ':(exclude)src/generated/'

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:

bash
git diff HEAD~1 HEAD -- . ':(exclude)dist/'
git diff main...feature -- . ':(exclude)vendor/'

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:

bash
1git diff origin/main...HEAD -- . \
2  ':(exclude)public/build/' \
3  ':(exclude)src/generated/' \
4  ':(exclude)coverage/'

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:

  • '.gitignore says whether new untracked files should be noticed'
  • 'git diff pathspecs 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:

bash
git diff --cached -- . ':(exclude)docs/build/'

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:

gitattributes
docs/build/* -diff

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:

bash
git diff -- . ':(exclude)generated/'

Compare staged changes but skip build output:

bash
git diff --cached -- . ':(exclude)dist/'

Compare your feature branch to main while excluding vendored code:

bash
git diff main...HEAD -- . ':(exclude)vendor/'

Once you start using exclude pathspecs, most “exclude from diff” problems become straightforward.

Common Pitfalls

  • Expecting .gitignore to hide tracked directory changes from git 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 .gitattributes for a one-off local need that should really stay at the command level.

Summary

  • Exclude directories from git diff with pathspecs such as git diff -- . ':(exclude)dist/'.
  • The same technique works for working tree diffs, staged diffs, and commit comparisons.
  • '.gitignore does not suppress tracked files from diff output.'
  • Pathspec filtering is more reliable than post-processing diff text with shell commands.
  • Use .gitattributes only when the repository wants a persistent diff policy.

Course illustration
Course illustration

All Rights Reserved.