跳到主内容
gitcommit

Git Commit Message Prompt:写出团队能读懂的 commit

让 AI 基于 git diff 自动生成 commit message,遵循 Conventional Commits 规范,不再是「Update file.ts」这种废话。支持 Claude Code / Cursor / Aider,生成带 scope 和 type 的规范提交信息。

用法

git add -p          # stage 你想 commit 的部分
git diff --cached   # 看 staged diff

把 diff 粘进 AI 配合 prompt:

Prompt

请基于下面的 git diff 生成 commit message,要求:

**格式**:Conventional Commits

```
<type>(<scope>): <subject>

<body>

<footer>
```

**type**:feat / fix / refactor / docs / test / chore / perf / style

**铁律**:
1. **subject ≤ 60 字符**,祈使句("add X" 不是 "added X")
2. **不要写"update XX 文件"**。要写**改了什么行为**。"update userService.ts" ❌;"add retry logic to userService.fetch" ✅
3. **body 解释 why,不是 what**——what 看 diff 就知道,why 看不出来
4. **如果是 fix,body 必须说**:(a) 现象、(b) 根本原因、(c) 修复方式
5. **scope 取自 diff 涉及的目录/模块**,不要瞎编
6. **如果 diff 包含多个不相关改动**,告诉我"建议拆成 N 个 commit",**不要硬拼成一条 message**

diff:
```
<paste here>
```

为什么有效

  • "解释 why 不是 what"是核心——好 commit message 让 6 个月后 git blame 的人能立刻理解决策
  • "建议拆 commit"防止 AI 把混乱的 diff 强行总结成一条空泛的 message
  • 对 fix 强制"现象 / 根因 / 修复"三段式 = 直接可读的事故记录

进阶(自动化)

把这个 prompt 存成 .git/hooks/prepare-commit-msgclaude-code 的别名脚本:

# ~/.local/bin/aicommit
git diff --cached | claude-code --prompt-file=~/.config/claude/commit-prompt.md \
  | tee /tmp/msg && git commit -F /tmp/msg

aicommit 就 = 自动 staged diff → AI 写 message → commit。

反例(AI 默认会写的烂 message)

Update auth.ts and config.tsVarious improvementsFix bug (fix 什么 bug?) feat: add new feature(什么 feature?)

我们要的好例子:

fix(auth): handle 401 from token refresh endpointrefactor(api): extract pagination logic into useCursor hookfeat(rankings): add real-time tool ranking from db click counts

延伸阅读

相关对比

Aider vs Claude Code:终端 AI 编程双雄怎么选

Aider vs Claude Code 2026 选型对比:开源 BYOK 多模型 vs Anthropic 订阅长任务 Agent,从编程能力、多模型支持、价格、Git 集成、国内可用性和适合人群判断,帮你选对终端 AI 编程工具。

Aider vs Continue:终端会话派还是 IDE 自建派?2026 对比

Aider 与 Continue 2026 选型对比:两者都是开源、模型可换的路线,但 Aider 是终端里的 Git-native 会话工具,每次改动自动生成一个 commit;Continue 是 IDE 内的可配置助手,强调自定义与自托管。从 6 个维度给出明确取舍。

Aider vs Crush:CLI pair programmer vs 终端 TUI agent(2026 实测选型)

Aider vs Crush 2026 选型对比:Git 原生 AI pair programmer vs Charmbracelet 终端 TUI agent。从工作哲学、多模型、Git 集成、LSP/MCP、价格和国内可用性帮你选对终端 AI 编程工具。

Aider vs OpenCode:两个开源终端 Agent,选哪个?2026 对比

Aider 与 OpenCode 2026 选型对比:两者都是开源终端 agent、都支持换模型,但 Aider 以 Git-native 工作流与成熟的多语言支持见长,OpenCode 主打模型无关、本地优先与终端交互体验。从 6 个维度给出明确取舍建议。

Augment Code vs Cursor:企业 AI 编程怎么选?Context Engine vs AI IDE 对比

Augment Code vs Cursor 2026 选型对比:Context Engine 全仓索引的企业 AI 平台 vs SpaceX 收购的 AI IDE 天花板,从形态、Context 覆盖、长任务、价格、合规、中文支持和适合人群 8 个维度判断,帮你选对企业 AI 编程工具。

Claude Code vs Cline:CLI AI Agent 怎么选?2026 对比

Claude Code vs Cline 2026 选型对比:Anthropic 官方闭源 CLI Agent vs Apache-2.0 开源 VS Code 插件。从模型绑定、工作流、MCP 支持、价格、隐私和适合人群 6 个维度帮你选对 CLI AI 编程工具。

相关评测