A Git diff filter that uses Claude AI to annotate every changed line with a concise, one-sentence explanation β inline, right in your terminal.
- return user.age >= 18;
+ return user.age >= 21; <-- π€ Raised the legal age threshold from 18 to 21
Designed to work seamlessly with git add -p
(interactive staging), so you understand exactly what each hunk does before you stage it.
AI coding tools (Copilot, Cursor, Claude) are fast β but they generate code you didn't write and may not fully understand. When you run git add -p
to selectively stage that output, it's easy to rubber-stamp hunks you only half-read.
This filter puts a second AI in the loop at exactly that moment. Before you decide to stage a hunk, every changed line already has a plain-English explanation attached to it β what changed, and why it matters. You stay in control without having to context-switch to read the diff cold.
It's also useful outside AI workflows: reviewing a colleague's PR locally, digging into an unfamiliar codebase, or just double-checking your own changes before a commit.
The script reads a Git diff stream from stdin, strips ANSI color codes so Claude can parse the raw text, sends the numbered diff to Claude (via the claude
CLI), and weaves the AI-generated explanations back into the original colored output β appended inline to each changed line.
Claude Code CLIβ must be installed and authenticated (claude
available in your PATH)bash
(v3.2+)perl
(for ANSI stripping)awk
(for line weaving)
All three shell tools are pre-installed on macOS and most Linux distributions.
curl -fsSL https://raw.githubusercontent.com/kooskoos20/git-diff-ai-explainer-interactive-filter/main/install.sh | bash
git clone https://github.com/kooskoos20/git-diff-ai-explainer-interactive-filter.git
cd git-diff-ai-explainer-interactive-filter
bash install.sh
Or install just the script to your PATH:
chmod +x git-diff-ai-explainer-interactive-filter
sudo cp git-diff-ai-explainer-interactive-filter /usr/local/bin/
Configure Git to run the filter automatically whenever you use interactive staging:
git config --global interactive.diffFilter git-diff-ai-explainer-interactive-filter
Now every git add -p
session will show AI annotations inline as you step through hunks. Git passes the diff with colors already intact, so no extra flags are needed.
To apply it to a single repo only, omit --global
.
Git disables colors when stdout is a pipe, so pass --color=always
to preserve them:
git diff --color=always | git-diff-ai-explainer-interactive-filter
git diff --color=always HEAD~1 | git-diff-ai-explainer-interactive-filter
git show --color=always abc1234 | git-diff-ai-explainer-interactive-filter
Useful aliases to add to ~/.bashrc
or ~/.zshrc
:
alias gda='git diff --color=always | git-diff-ai-explainer-interactive-filter | less -R'
alias gdca='git diff --cached --color=always | git-diff-ai-explainer-interactive-filter | less -R'
The script uses Claude's haiku
model by default for speed and cost efficiency. To change the model, edit line 34 in the script:
CLAUDE_OUT=$(echo "$CLAUDE_PROMPT" | claude --model haiku)
CLAUDE_OUT=$(echo "$CLAUDE_PROMPT" | claude --model sonnet)
rm /usr/local/bin/git-diff-ai-explainer-interactive-filter
git config --global --unset interactive.diffFilter
Contributions are welcome! See CONTRIBUTING.md for guidelines.