claude code中添加skills自动生成git commit信息

claude code中添加skills自动生成git commit信息

git-commit-helper

这个 git-commit-helper skill 的作用是:读取 staged changes,也就是已经 git add 的改动,然后根据 git diff --staged 生成规范的 commit message

这个 skill 会用 Bash 执行

bash 复制代码
git diff --staged --name-only
git diff --staged

它的分析流程里明确写了先检查 staged changes,再分析改动内容。 所以如果你的 .claude/settings.json 完全禁止 Bash,这个 skill 就无法自动读取 diff

1.1 git-helper这个skill适合解决什么问题

Claude 修改完代码后,我不知道 git commit message 应该怎么写

这个 skill 正好适合放在修改完成后使用:

Claude 修改代码

→ 你检查 git diff

→ 你 git add 需要提交的文件

→ 调用 /git-commit-helper

→ Claude 生成 commit message

→ 你手动 git commit

不要让 Claude 自动执行 git commit。让它只生成 message,你自己复制执行最稳。

1.2 安装方式:只给当前项目使用

把 skill 放到项目级 .claude/skills 目录

bash 复制代码
your-project/
├── CLAUDE.md
├── .claude/
│   ├── skills/
│   │   └── git-commit-helper/
│   │       └── SKILL.md
│   └── settings.json
├── main.py
├── utils/
└── configs/

Claude Code 官方文档说明,项目级 skill 路径是:

bash 复制代码
.claude/skills/<skill-name>/SKILL.md

只对当前项目生效;

个人级 skill 路径是:

bash 复制代码
~/.claude/skills/<skill-name>/SKILL.md

会对所有项目生效

1.3 skill.md内容

bash 复制代码
---
name: git-commit-helper
description: Generate clear Conventional Commit messages from staged git changes. Use after code modifications when the user asks what commit message to write. Analyze staged diff and suggest commit messages only. Do not execute git commit.
allowed-tools: Bash, Read
disable-model-invocation: true
---

# Git Commit Helper

你是当前项目的 Git commit message 助手。

你的任务是:根据 staged changes 生成清晰、准确、可复制的 commit message。

## 严格规则

- 只生成 commit message。
- 不要执行 `git commit`。
- 不要执行 `git push`。
- 不要执行 `git add`。
- 不要修改任何文件。
- 不要为了生成 message 改代码。
- 如果没有 staged changes,提醒用户先手动执行 `git add <file>`。
- 如果 staged changes 过多,先建议用户拆分提交。

## 允许读取的信息

优先读取 staged changes:

```bash
git status --short
git diff --staged --name-only
git diff --staged

1.4 git commit格式

使用 Conventional Commit 格式:

bash 复制代码
<type>(<scope>): <subject>

<body>

常用 type:

bash 复制代码
feat: 新增功能
fix: 修复 bug
refactor: 重构,不改变默认行为
perf: 性能优化
docs: 文档修改
test: 测试相关
style: 格式或排版,不改变逻辑
chore: 工程维护、配置维护、依赖或杂项
config: 配置参数调整
experiment: 实验流程或评估脚本调整

根据修改文件选择 scope:

例如我的项目

bash 复制代码
frontend: utils/slam_frontend.py
backend: utils/slam_backend.py
loop: utils/loop_closure.py

Subject 规则

使用英文

使用祈使句

小写开头

不超过 72 个字符

不以句号结尾

准确描述本次提交的核心变化

例子

bash 复制代码
fix(submap): preserve global seed pose when starting new submap
feat(config): add synchronized submap pruning parameters
refactor(loop): separate adjacent odometry from loop closure edges
docs(claude): add base config synchronization rule

Body 规则

Body 用英文 bullet points,说明 what 和 why,不要详细复述代码实现。

示例

bash 复制代码
- Keep submap initialization aligned with the tracked global pose
- Avoid resetting pose state during submap transitions
- Preserve compatibility with existing checkpoint fields

完整commit输出格式

Recommended commit message

<完整 commit message>
Alternative messages

type(scope): subject

type(scope): subject

type(scope): subject
Why this message

改动类型:

推荐 scope:

核心原因:

是否包含 breaking change:

是否建议拆分提交:

1.5 配置claude权限只能读git diff 不能执行git commit

bash 复制代码
.claude/settings.json
bash 复制代码
{
  "permissions": {
    "allow": [
      "Read(*)",
      "Bash(git status:*)",
      "Bash(git diff:*)"
    ],
    "deny": [
      "Bash(git commit:*)",
      "Bash(git push:*)",
      "Bash(git reset:*)",
      "Bash(rm -rf:*)"
    ]
  }
}

含义是:

bash 复制代码
允许:
- 读取文件
- 查看 git status
- 查看 git diff

禁止:
- git commit
- git push
- git reset
- 删除文件
- 编辑文件
- 写文件

1.6 如何使用

(1)让claude修改代码

(2)修改完成后,先让它总结:

bash 复制代码
请总结本次修改的 git diff,不要提交。

(3)自己在终端里手动运行检查diff

bash 复制代码
git status
git diff

确认claude没有乱改动要求范围外的代码

(4)自己 stage (将修改代码添加到本地暂存区)文件

bash 复制代码
git add utils/slam_frontend.py
git add utils/slam_backend.py
git add configs/rgbd/tum/base_config.yaml
git add configs/rgbd/replica/base_config.yaml
git add configs/rgbd/scannetpp/base_config.yaml

(5)调用 skill

在 Claude Code 中输入:

bash 复制代码
/git-commit-helper

或者更明确:

bash 复制代码
/git-commit-helper 请分析 staged changes,只生成 commit message,不要执行 git commit。

(6)复制 Claude 给出的 commit message后自己手动执行commit

bash 复制代码
git commit -m "fix(submap): preserve global pose during submap transition" -m "- Initialize new submaps from the tracked global camera pose
- Keep frontend and backend pose semantics aligned
- Avoid pose jumps when cutting submaps"
相关推荐
码哥字节2 天前
213000星的Superpowers,90%的人只用了它10%的功能
claude code·ai编程工具·claude code技巧
A_Lonely_Cat2 天前
记一次 GitHub 幽灵协作者大清洗:强制重写 Git 历史与穿透 CDN 缓存实践
git·github
lincats2 天前
Claude Code项目越写越乱?这套清理流程能救你
ai·ai agent·claude code
lincats3 天前
Claude Code再强,也有这7件事做不了
ai agent·deepseek·claude code
码哥字节4 天前
我用 Claude Code 做 Code Review 两个月,Bug 漏检率从 41% 降到 11%
code review·claude code·ai代码审查
和你看星星4 天前
Git rerere:让重复冲突只解决一次
git
码哥字节6 天前
GitHub 今日 +2299 Star,这个工具让 AI 读代码不再像翻字典
ast·claude code·代码知识图谱·understand-anything
嘻嘻仙人7 天前
Ubuntu中 git上传自己的项目和二次上传一般流程
git·github
Patrick_Wilson7 天前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员