Claude Code git diff too large -- (2026)
The Problem
Running git diff or git diff --staged in Claude Code returns the full diff, which for large changesets can be 10,000-50,000+ tokens. This entire output enters the context window and persists for the rest of the session. A single large diff can double the session’s token cost. At Opus 4.6 rates ($15/MTok), a 30K-token diff that persists across 10 turns costs $4.50 in re-sent input alone.
Quick Fix (2 Minutes)
- Always use
--statfirst – shows changed files and line counts in approximately 200-500 tokens. - Diff specific files –
git diff -- src/auth/middleware.tsinstead of the full repository. - Add to CLAUDE.md – enforce
--statfirst pattern for all git diff operations.
# Instead of this (potentially 30K+ tokens):
git diff
# Do this first (~200 tokens):
git diff --stat
# Then target specific files (~500-2,000 tokens each):
git diff -- src/auth/middleware.ts
git diff -- src/routes/users.ts
Why This Happens
Claude Code runs git diff (or the user requests “show me the changes”) and the command returns every modified line across every changed file. Common scenarios where diffs are unexpectedly large:
- Generated file changes – lock files (pnpm-lock.yaml), compiled output, or auto-generated types can add 5,000-20,000 tokens to a diff.
- Formatting changes – running a formatter that touches many files creates large diffs with no semantic content.
- Multi-feature branches – branches with 20+ changed files produce diffs that exceed 30K tokens.
- Whitespace or line ending changes – can inflate diffs dramatically with no meaningful content.
The token cost mechanism: git diff output enters the conversation context at full size. Claude Code has no automatic truncation for tool outputs. A 40K-token diff persisted across 10 turns = 400K tokens of re-sent input = $6.00 at Opus rates.
The Full Fix
Step 1: Diagnose
# Check how large the diff would be before running it:
git diff --stat
# Output: 15 files changed, 847 insertions(+), 234 deletions(-)
# This tells you the diff would be large -- do not run full diff
# Check for generated files in the diff:
git diff --stat | grep -E "(lock|generated|compiled|dist)"
# If these appear, exclude them from the diff
Step 2: Fix
# Strategy 1: Targeted diffs (read only relevant files)
git diff --stat # See all changed files (~200 tokens)
git diff -- src/auth/middleware.ts # Read one specific diff (~800 tokens)
# Strategy 2: Exclude noisy files
git diff -- . ':!pnpm-lock.yaml' ':!dist/' ':!*.generated.ts'
# Strategy 3: Summary diff with context control
git diff --stat --compact-summary # Even more compact summary
# Strategy 4: For very large diffs, use --name-only
git diff --name-only # Just the file paths (~100 tokens)
# Then read specific files of interest
Step 3: Prevent
# CLAUDE.md -- git diff cost control
## Git Diff Rules
- ALWAYS run `git diff --stat` before `git diff` (full diff)
- Never run full `git diff` on more than 5 changed files
- Exclude from diffs: pnpm-lock.yaml, yarn.lock, dist/, *.generated.*, node_modules/
- For PR reviews: use `git diff --stat` then `git diff -- <specific-file>` for each file of interest
- Maximum diff output: pipe through `head -100` if full diff is needed for a large file
- For merge conflicts: diff only the conflicted files, not the entire branch
# Example of bounded diff reading:
# Step 1: Overview
git diff --stat
# 8 files changed
# Step 2: Identify important files
# Skip: pnpm-lock.yaml, dist/ files
# Read: src/auth/*.ts, src/routes/users.ts
# Step 3: Targeted diffs
git diff -- src/auth/middleware.ts # ~800 tokens
git diff -- src/auth/token.ts # ~600 tokens
git diff -- src/routes/users.ts # ~500 tokens
# Total: ~1,900 tokens vs ~25,000 for full diff
Cost Recovery
Large diff already in context:
Diff size: 30K tokens
Remaining turns: 10
Re-send cost: 30K * 10 / 1,000,000 * $15 = $4.50 (Opus)
Recovery: /compact to remove the diff content
Post-compact diff tokens: ~500 (summary)
Remaining re-send cost: 500 * 10 / 1,000,000 * $15 = $0.08
Savings: $4.42
Prevention: --stat first pattern
Stat output: ~200 tokens
Targeted diffs: ~2,000 tokens total
Re-send cost: 2,200 * 10 / 1,000,000 * $15 = $0.33
Savings vs full diff: $4.17 per session
Prevention Rules for CLAUDE.md
# CLAUDE.md -- copy-paste this section
## Git Diff Cost Control
- NEVER run bare `git diff` -- always `git diff --stat` first
- Full diffs: only on specific files (`git diff -- <file>`)
- Exclude from all diffs: `':!pnpm-lock.yaml' ':!*.lock' ':!dist/' ':!*.generated.*'`
- Maximum files to diff in full: 5 per operation
- For large files (>200 lines changed): `git diff -- <file> | head -100`
- For PR review: `git diff --stat origin/main...HEAD` first, then targeted file diffs
Estimate usage → Calculate your token consumption with our Token Estimator.
Related Guides
Configure permissions → Build your settings with our Permission Configurator.
Try it: Estimate your monthly spend with our Cost Calculator.
- Claude Code re-reading entire codebase every message – reducing redundant file reads
- CLAUDE.md as Cost Control – comprehensive cost prevention rules
- The Compaction Strategy – recovering from large context pollution
See Also
- Knowledge Base Exceeds 512KB Maximum — Fix (2026)
- Tool Result Exceeds 100KB Truncating — Fix (2026)
- Git Submodule Not Initialized Error Fix
Frequently Asked Questions
Do I need a paid Anthropic plan to use this?
Claude Code works with any Anthropic API plan, including the free tier. However, the free tier has lower rate limits (requests per minute and tokens per minute) that may slow down multi-step workflows. For professional use, the Build or Scale plan provides higher limits and priority access during peak hours.
How does this affect token usage and cost?
The token cost depends on the size of your prompts and Claude’s responses. Typical development tasks consume 10K-50K tokens per interaction. Using a CLAUDE.md file and skills reduces exploration tokens by 50-80%, which directly lowers costs. Monitor your usage at console.anthropic.com/settings/billing.
Can I customize this for my specific project?
Yes. All Claude Code behavior can be customized through CLAUDE.md (project rules), .claude/settings.json (permissions), and .claude/skills/ (domain knowledge). The most impactful customization is adding your project’s specific patterns, conventions, and common commands to CLAUDE.md so Claude Code follows your standards from the start.
What happens when Claude Code makes a mistake?
Claude Code creates files and edits through standard filesystem operations, so all changes are visible in git diff. If a change is wrong, revert it with git checkout -- <file> for a single file or git stash for all changes. Claude Code does not make irreversible changes unless you explicitly allow destructive commands in settings.json.