实战配置 CLAUDE.md:禁止 AI 自动添加 Git Commit 签名并规范格式
前言:为什么要给 AI 立规矩?
在日常开发中,我经常使用 Claude 来辅助编写代码和生成 Git 提交信息(Commit Message)。通常情况下,它的表现非常出色,但有一个令我头疼的小毛病:它太'礼貌'了。
经常出现的情况是,当我要求它写一个 commit 时,它会在末尾画蛇添足地加上 Co-authored-by: Claude 或者 Signed-off-by: AI Assistant,甚至会在输出前加一句'Here is your commit message:'。这导致我每次复制粘贴时,还得手动删除这些多余的元数据和废话,非常影响效率。
这篇文章将分享如何在项目根目录(或家目录下全局)配置 CLAUDE.md 规则文件,通过**否定提示(Negative Prompting)和约定式提交(Conventional Commits)**规范,强制 Claude 输出纯净、标准化的提交信息。
一、痛点复现:那些多余的'尾巴'
在没有配置规则之前,你可能遇到过这样的回复:
User: 帮我给这次修改写个 commit,修复了登录页的 bug。
Claude: 好的,这是为您准备的提交信息:
这种结果有三个问题:
- 废话多:开头的'好的…'需要手动过滤。
- 乱签名:Git 提交记录应该是纯净的,不应该包含 AI 的签名(除非特定开源项目要求)。
- 格式不可控:有时它会忘记使用
fix:或feat:等前缀。
二、解决方案:编写 CLAUDE.md 规则
我们可以在项目根目录下(或家目录下全局)创建一个名为 CLAUDE.md的文件。这相当于给 Claude 下达了一份'岗位说明书'。
以下是经过验证的严格规范版配置代码,建议直接复制使用:
# Git Commit Rules
## Behavior When generating git commit messages:
- **Strictly Forbidden**: Never include text indicating the message was AI-generated (e.g., "Written by Claude", "AI-generated").
- **No Footers**: Do not append "Signed-off-by" or "Co-authored-by" lines unless explicitly told to do so for a specific human user.
- **Direct Output**: Output the commit message immediately without introductory text (e.g., skip "Sure, here is the commit...").
## Format Standard
- Use the Conventional Commits format: `<type>(<scope>): <subject>`
- Allowed types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert.
- Keep the first line under 72 characters.
三、规则解析:为什么这样写有效?
这段规则主要运用了 Prompt Engineering(提示词工程)中的几个关键技巧:
1. 明确的否定指令 (Negative Constraints)
大模型有时会'产生幻觉'或者'过度表现'。在规则中,我们使用了 'Strictly Forbidden' (严禁) 和 'No Footers' (无页脚) 这样的强语气词汇。
Never include text indicating...:直接切断了它自我声明的冲动。Do not append...:明确列举了Signed-off-by等具体关键词,相当于给模型打了一个'补丁'。

