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"
相关推荐
FserSuN3 小时前
Git Worktree 使用学习
git·学习
Z文的博客3 小时前
嵌入式LINUX QT 开发 .gitignore 文件编写指南
linux·git·qt·elasticsearch·嵌入式
前端双越老师3 小时前
3 个命令 7 个步骤,学会 git worktree 并行开发
git·ai编程·全栈
小黑要努力18 小时前
智能音箱遇到的问题(一)
linux·运维·git
RePeaT18 小时前
【git】指令场景实战:单分支与多分支协作流程
git
前端Hardy20 小时前
杀疯了!Git 2.54 正式发布,3个封神新特性,效率直接翻倍!
git
Uncertainty!!20 小时前
GitNexus安装过程记录:Code Graph RAG 并联动 Claude Code
mcp·claude code·gitnexus
Eloudy21 小时前
迁移带有 git lfs 功能的 github 仓库
git·github
xlq223221 天前
1.git
git