跳到主内容
AIHO 2026 全新改版上线
gitcommit

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

让 AI 基于 git diff 写 commit message,不再是

适用:Claude CodeCursorAider

用法

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