MW

How-To · Version Control

git diff --exclude: How to Exclude Files & Patterns from Git Diff (2026)

Lock files, build output, and node_modules dominate `git diff`. Hide them with one-shot pathspec excludes, repo-wide `.gitattributes` rules, or custom diff drivers.

Jan 24, 2025 4 min read Any platform intermediate
Advertisement

Excluding files from git diff is essential when reviewing changes — lock files, build artifacts, and node_modules drown out the actual source-code changes you care about.

Here’s the pathspec exclude pattern in action:

Terminal — exclude pattern (one-time)
$ git diff
diff --git a/package-lock.json b/package-lock.json ... 8,432 lines of lockfile noise ... diff --git a/src/App.tsx b/src/App.tsx ... your actual change ...
$ git diff -- '.' ':!package-lock.json' ':!node_modules/**'
diff --git a/src/App.tsx b/src/App.tsx + const greeting = "Hello, world" - const greeting = "Hi"

This guide covers all 3 ways to exclude files: pathspec (one-time), .gitattributes (permanent), and custom diff drivers (advanced).

Introduction

Why Exclude Files from Diff?

  • Focus on source code changes
  • Ignore generated files (build outputs, compiled code)
  • Skip dependency changes (node_modules, vendor)
  • Reduce diff noise
  • Improve readability

Common Files to Exclude:

  • node_modules/
  • vendor/
  • dist/, build/
  • .env files
  • Lock files (package-lock.json, yarn.lock)
  • Generated files

Using .gitattributes

The .gitattributes file can mark files to be excluded from diffs.

Basic Setup

Create .gitattributes in repository root:

# Exclude from diff
package-lock.json diff
yarn.lock diff
*.min.js diff
*.min.css diff

Syntax: pattern diff tells Git to use the “diff” driver, which by default shows nothing.

Exclude File Types

# Exclude all minified files
*.min.js diff
*.min.css diff

# Exclude lock files
package-lock.json diff
yarn.lock diff
composer.lock diff

# Exclude generated files
dist/* diff
build/* diff

Exclude Directories

# Exclude entire directories
node_modules/** diff
vendor/** diff

Custom Diff Driver

Define a custom driver that shows nothing:

In .gitattributes:

*.lock diff=lockfile
node_modules/** diff=exclude

In .git/config or ~/.gitconfig:

[diff "lockfile"]
    binary = true
[diff "exclude"]
    command = /bin/true

Git Diff Options

Exclude Patterns

Use -- . ':!pattern' syntax:

# Exclude node_modules
git diff -- . ':!node_modules'

# Exclude multiple patterns
git diff -- . ':!node_modules' ':!vendor' ':!dist'

# Exclude file types
git diff -- . ':!*.min.js' ':!*.min.css'

Exclude Paths

# Exclude specific files
git diff -- . ':!package-lock.json' ':!yarn.lock'

# Exclude directories
git diff -- . ':!node_modules' ':!vendor'

# Combine patterns
git diff -- . ':!node_modules/**' ':!*.lock'

Create Alias

Add to ~/.gitconfig:

[alias]
    diff-clean = diff -- . ':!node_modules' ':!vendor' ':!dist' ':!*.min.js'

Usage:

Terminal — using the diff-clean alias
$ git diff-clean
diff --git a/src/App.tsx b/src/App.tsx + const greeting = "Hello, world" - const greeting = "Hi"

Custom Diff Driver

Define Custom Driver

In .git/config:

[diff "exclude"]
    command = /bin/true

This driver always returns success (no diff shown).

Use in .gitattributes

node_modules/** diff=exclude
vendor/** diff=exclude
*.lock diff=exclude

Advanced Driver (Show Message)

Create script /usr/local/bin/git-diff-exclude:

#!/bin/bash
echo "File excluded from diff"
exit 0

Make executable:

chmod +x /usr/local/bin/git-diff-exclude

Configure:

[diff "exclude"]
    command = /usr/local/bin/git-diff-exclude

Ignore Patterns

Common Patterns

Lock Files:

package-lock.json diff
yarn.lock diff
composer.lock diff
Gemfile.lock diff

Dependencies:

node_modules/** diff
vendor/** diff
bower_components/** diff

Build Outputs:

dist/** diff
build/** diff
*.min.js diff
*.min.css diff

Environment Files:

.env diff
.env.local diff
.env.*.local diff

Generated Files:

*.log diff
*.cache diff
coverage/** diff

Common Use Cases

Exclude node_modules

Method 1: .gitattributes

node_modules/** diff

Method 2: Git diff option

git diff -- . ':!node_modules'

Method 3: Alias

[alias]
    diff-code = diff -- . ':!node_modules' ':!vendor'

Exclude Lock Files

.gitattributes:

package-lock.json diff
yarn.lock diff
composer.lock diff

Why: Lock files change frequently but aren’t source code.

Exclude Generated Files

dist/** diff
build/** diff
*.min.js diff
*.min.css diff
*.map diff

Exclude Test Coverage

coverage/** diff
.nyc_output/** diff

Best Practices

1. Use .gitattributes for Permanent Exclusions

For files that should always be excluded:

  • Add to .gitattributes
  • Commit to repository
  • Applies to all developers

2. Use Git Diff Options for Temporary Exclusions

For one-time exclusions:

  • Use -- . ':!pattern' syntax
  • No repository changes needed

3. Document Exclusions

Add comments in .gitattributes:

# Lock files - exclude from diff
package-lock.json diff
yarn.lock diff

# Dependencies - exclude from diff
node_modules/** diff
vendor/** diff

# Generated files - exclude from diff
dist/** diff
build/** diff
*.min.js diff

4. Team Consistency

Ensure all team members use same exclusions:

  • Commit .gitattributes
  • Document in README
  • Use shared aliases

5. Review Before Committing

Even if excluded from diff, review important files:

# View excluded files separately
git diff package-lock.json

Example .gitattributes

# Lock files
package-lock.json diff
yarn.lock diff
composer.lock diff

# Dependencies
node_modules/** diff
vendor/** diff

# Build outputs
dist/** diff
build/** diff
*.min.js diff
*.min.css diff

# Generated files
*.log diff
coverage/** diff

Conclusion

Excluding files from git diff:

  1. .gitattributes - Permanent exclusions (recommended)
  2. Git diff options - Temporary exclusions
  3. Custom drivers - Advanced control
  4. Aliases - Convenience shortcuts

Key Points:

  • Use .gitattributes for team-wide exclusions
  • Use diff options for one-time needs
  • Document exclusions
  • Keep exclusions minimal and justified

This improves diff readability and focuses on meaningful changes.

Git Diff Version Control Development Command Line
Advertisement

Get weekly notes in your inbox

Practical tips, tutorials and resources. No spam.