Claude CodeのAgent Skillでgit commitを自動化する

に公開

git diffの内容を踏まえて自動的にコミットメッセージを作りgit commitするClaude Codeのスキル(Agent Skill)を作りました。いくらか試していますが、まずまず期待通りに動いています。

この記事では作成したSKILL.mdの紹介をします。何番煎じかよくわかりませんが、きっとこの知識が欲しい人もいることでしょう。

※ 筆者はスキル名を git-cm としていますが、もっと良い名前もあるかもしれないなと思っています。

スキルとは

解説記事は既にたくさんあるので仔細には触れません。ざっくり言うなら、事前に準備したプロンプトのスニペットをClaudeの判断で読み込ませ、コンテキストを増強する仕組みです。

繰り返し発生するような操作をClaudeに指示することもできるので、今回はgit commitの自動化に用いました。

使い方

以下の実行例はClaude Opus 4.5 (thinking on) によります。筆者の環境では ~/.claude/skills/git-cm/SKILL.md を作成して登録してあります。

あとはClaude Code内でスキルを起動してもらうだけです。例えば:

run /git-cm

git diffの内容にもよりますが、適当なプロジェクトでREADME.mdをgit addしてから実行してみました。すると次のようなgit commitが自動で実行されました。コミットメッセージは Conventional Commits風の形式を採用しています。

git commit -m "docs: add README with project overview and setup instructions"

言語やbodyを含める指定もできます。Claudeの推理能力はかなり高いので、相当に省略しても期待通りに動いてくれます。

run /git-cm w detail JP

この部分のthinkingログを見てみると、ちゃんと日本語かつ詳細をメッセージに書くという意図が伝わっているのが分かります。

The user wants me to run a git commit with a detailed message in Japanese (JP). Let me follow the workflow:

1. First, check staged changes with git diff --cached --stat
2. Analyze the diff
3. Generate and execute the commit

Let me start by checking the staged changes.

スキル設計の工夫

基本的には SKILL.md 自体もClaudeに生成してもらったものなのですが、いくつか工夫を入れています。

ステージ済み変更のみを対象とする

プログラマとClaudeの役割分担をこのように想定しました:

  • コミットに何を含めるか決めるのは、依然としてプログラマの責務
    • Claudeはgit addしない
  • コミットメッセージを考えてgit commitするのはClaudeの役割

もし期待外れのメッセージがgit commitされてしまったとしても、スキル実行後にプログラマがcommit --amend等すればいいので、作業フローとしてはスムーズです。

コミットメッセージの言語を選べる

日本語でコミットしてほしい時は、そう書くと反応するようにしています。

If user specifies language (e.g., "日本語で", "in English"), write commit message in that language

bodyを書くかは自動だが強制もできる

bodyを省略した簡潔なコミットにしてもよいと指示しています。LLMの性質上、毎回同じ判断になるとは限らず、いくらかの不安定さを感じますが、おおむね期待通りです。

The Body is optional: skip if the diff is very small to describe details

誤動作の抑制

スキルが暗黙のうちに呼び出されづらいように、descriptionで念押ししています。コミットは明確に指示した時にのみ実施してほしいからです。

ONLY use when user explicitly invokes /git-cm slash command. Do NOT trigger on general git questions.

SKILL.md

---
name: git-cm
description: "Generate commit message and automatically commit staged changes. ONLY use when user explicitly invokes /git-cm slash command. Do NOT trigger on general git questions."
---

# Git Auto-Commit

Generate commit message and execute commit automatically.

## General Rules
- If user specifies language (e.g., "日本語で", "in English"), write commit message in that language
- If user wants to add details (e.g., "with details", "bodyあり"), ALWAYS write commit message with Body segment

## Workflow

### 1. Check Staged Changes

```bash
git diff --cached --stat
```

If empty, stop and tell user to `git add` first.

### 2. Analyze Diff

```bash
git diff --cached
```

For large diffs (>500 lines), check `--stat` first, then view key files selectively.

If needed for context:
- Read full file for new files
- Check `git log --oneline -5`

### 3. Generate & Execute Commit

Conventional Commits format:

```
<type>[(<scope>)]: <subject>

<body>
```

**Types:** feat, fix, docs, style, refactor, perf, test, chore

**Scope:** optional

**Rules:**
- Subject: imperative, lowercase, no period, ≤50 chars
- Body: what/why not how, wrap 72 chars, keep simple if possible
  - The Body is optional: skip if the diff is very small to describe details

**Execute directly:**

```bash
git commit -m "type(scope): subject" -m "body paragraph"
```

For multi-line body, use multiple `-m` flags.

## Examples

**Simple:**
```bash
git commit -m "fix(auth): correct token expiration check"
```

**With body:**
```bash
git commit -m "feat(api): add user search endpoint" -m "Implement fuzzy search across username and email fields."
```

Discussion