关于 diff-preview
diff-preview 是面向开发者的技能,用于检查 Git 变更并准备代码审查。它结合 Git 差异比较方式与 Gemini 提示词,可解释变更、分析影响、审查暂存代码并建议提交消息。
主要功能
- 01比较工作区、暂存区、提交和分支差异
- 02调整差异输出并忽略空白
- 03使用 Gemini 解释变更
- 04分析影响并审查暂存代码
- 05建议 Conventional Commit 消息
适用场景
- 01提交前审查暂存变更
- 02准备分支差异以供拉取请求审查
- 03调查不同提交间的变更
- 04根据差异起草提交消息
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ johnlindquist/ claude --skill diff-preview安装后在 Claude Code 中输入/diff-preview确认已加载。
详细介绍
Diff 预览与分析
借助 AI 辅助预览、解释和分析 git 变更。
前置要求
# Git
git --version
# Gemini for AI explanations
pip install google-generativeai
export GEMINI_API_KEY=your_api_key
命令行参考
基本 Diff 命令
# Working directory changes
git diff
# Staged changes
git diff --staged
git diff --cached
# Specific file
git diff path/to/file.ts
# Between commits
git diff abc123 def456
# Between branches
git diff main feature-branch
git diff main...feature-branch # Since branch diverged
# Show stat summary
git diff --stat
git diff --shortstat
# Name only
git diff --name-only
git diff --name-status # With status (M/A/D)
Diff 输出选项
# Word diff (better for prose)
git diff --word-diff
# Color words
git diff --color-words
# Ignore whitespace
git diff -w
git diff --ignore-all-space
# Context lines
git diff -U10 # 10 lines of context (default 3)
# Show function names
git diff --function-context
提交比较
# Last commit
git diff HEAD~1
# Last N commits
git diff HEAD~5
# Specific commit range
git diff abc123..def456
# What changed in a specific commit
git show abc123
AI 驱动的分析
解释变更
# Get diff and explain
DIFF=$(git diff --staged)
gemini -m pro -o text -e "" "Explain these code changes in plain English:
$DIFF
Summarize:
1. What was changed
2. Why it might have been changed
3. Any potential issues"
影响分析
DIFF=$(git diff main...feature-branch)
gemini -m pro -o text -e "" "Analyze the impact of these changes:
$DIFF
Consider:
1. Breaking changes
2. Performance implications
3. Security considerations
4. Files/systems affected
5. Testing recommendations"
提交前审查
# Review staged changes before commit
git diff --staged | gemini -m pro -o text -e "" "Review this code diff for:
1. Bugs or issues
2. Code style problems
3. Missing error handling
4. Security concerns
Provide specific line references if issues found."
生成提交信息
DIFF=$(git diff --staged)
gemini -m pro -o text -e "" "Based on this diff, suggest a commit message:
$DIFF
Use conventional commit format (feat/fix/chore/etc).
First line under 50 chars, then details."
比较模式
比较分支
# Summary
git diff main feature-branch --stat
# Full diff
git diff main feature-branch
# Since branch point
git diff main...feature-branch
# Commits unique to feature branch
git log main..feature-branch --oneline
查找变更内容
# What files changed between commits
git diff --name-only abc123 def456
# What changed in a file
git log -p -- path/to/file.ts
# When was a line added
git blame path/to/file.ts
合并预览
# Preview merge conflicts
git merge --no-commit --no-ff feature-branch
git diff --staged
git merge --abort # Clean up
工作流模式
提交前检查
#!/bin/bash
# Check staged changes before commit
echo "=== Changes to commit ==="
git diff --staged --stat
echo ""
echo "=== Detailed diff ==="
git diff --staged
echo ""
read -p "Proceed with commit? (y/n) " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]; then
git commit
fi
PR 审查准备
# Get full PR diff for review
BASE=${1:-main}
BRANCH=$(git branch --show-current)
echo "=== Files changed ==="
git diff $BASE...$BRANCH --name-status
echo ""
echo "=== Stats ==="
git diff $BASE...$BRANCH --shortstat
echo ""
echo "=== AI Summary ==="
git diff $BASE...$BRANCH | gemini -m pro -o text -e "" "Summarize this PR's changes for a code reviewer. Focus on the intent and key changes."
跟踪特定变更
# Find all changes to a function
git log -p -S "functionName" -- "*.ts"
# Find changes mentioning something
git log -p --grep="JIRA-123"
最佳实践
- 提交前审查 - 始终检查
git diff --staged - 先使用 stat - 在完整 diff 前获取概览
- 与基线比较 - 使用
main...branch获取 PR 上下文 - 忽略空白字符 - 使用
-w关注有意义的变更 - 请求 AI 审查 - 针对复杂变更
- 保存重要 diff - 重定向到文件以供参考
评论
支持 Markdown