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"
相关推荐
cen__y8 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
bukeyiwanshui11 小时前
20260518 Swift实验
git·swift
qziovv11 小时前
Git 回退场景
大数据·git·elasticsearch
Canicer14 小时前
【国内安装 Claude Code CLI版本纯净完整指南】
ai·agent·claude code
来自大山深处的Doge_14 小时前
解决Git提交更新更改时出错: detected dubious ownership in repository at ...
git
嵌入式爱好者hsw16 小时前
Git 部署本地仓库
git
C137的本贾尼17 小时前
Git基本操作(三):版本回退,坐上“时光机”
git
ylifs18 小时前
目的驱动式Git用法
git
来尔君19 小时前
Git Bash 提示符简化(就是每次敲命令时上面显示的那一行信息)
git·命令行
我叫张小白。19 小时前
PyCharm 集成 Git 与 Gitee
git·pycharm·gitee