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"
相关推荐
zttbee14 小时前
git命令
git
mister_guo1 天前
开发指南:Git日常维护与常用命令
git
鶴哥只手遮天2 天前
Git 2.56 源码深度剖析:为 Git 3.0 铺路的架构重构
git·开源
小此方2 天前
「插曲:Git」标签管理篇:理解Tag标签、为指定提交打标签、查看标签详情与附注标签
git
Madison-No72 天前
多语言聊天大模型--测试报告
linux·git·python·selenium·jmeter·自动化·postman
小此方2 天前
「插曲:Git」多人协作篇:远程分支创建与关联、多人协作冲突解决、Feature分支开发与合并、git remote prune清理失效分支
git
可乐ea2 天前
Git 基础设施重建:智能体规模开发下的读写解耦
大数据·git·elasticsearch·分布式存储·git基础设施·智能体规模开发·读写解耦
信可维2 天前
AI 说「改完了」怎么确认真改完了:git diff + 测试 + 一张验收表的 3 步复核
人工智能·git
Sun子矜2 天前
Git+Gitee 新手教程精简归纳版
git·gitee
亿道电子Emdoor2 天前
【Perforce】Klocwork-kwgcheck图形化界面无法打开如何解决
git·python·github