BioErrorLog Tech Blog

試行錯誤の記録

カスタムスラッシュコマンドで自動コミットする | Claude Code

Claude Codeカスタムスラッシュコマンドで自動git commitすると便利だよという紹介です。

はじめに

git commitメッセージは重要であると同時に、考えるのにワンテンポ要する作業でもあります。

Claude Codeカスタムスラッシュコマンドを活用することでこの作業を補佐できるので、やり方の一例を紹介します。

The English translation of this post is here.

Claude Codeカスタムスラッシュコマンドで自動git commitする

やり方

まず、カスタムスラッシュコマンドの基本的な作り方については別途記事にまとめました。 こちらをご覧ください。

www.bioerrorlog.work

ではさっそく、自動コミットするスラッシュコマンドの一例を見ていきます。

~/.claude/commands/commit.mdを作成し、下記のようにプロンプトを記載しておけば、スラッシュコマンド/commitで自動コミットさせることが可能です。

---
allowed-tools: Bash(git add:*), Bash(git status:*), Bash(git commit:*)
description: Create a git commit
---

## Context

- Current git status: !`git status`
- Current git diff (staged and unstaged changes): !`git diff HEAD`
- Current branch: !`git branch --show-current`
- Recent commits: !`git log --oneline -10`

## Your task

1. Analyze the diff content to understand the nature and purpose of the changes
2. Generate 3 commit message candidates based on the changes
   - Each candidate should be concise, clear, and capture the essence of the changes
   - Prefer Conventional Commits format (feat:, fix:, docs:, refactor:, etc.)
3. Select the most appropriate commit message from the 3 candidates and explain the reasoning for your choice
4. Stage changes if necessary using git add
5. Execute git commit using the selected commit message

## Constraints

- DO NOT add Claude co-authorship footer to commits

やらせているタスクはシンプルです。

  • 現時点の変更内容を理解する
  • 3つのコミットメッセージ候補を立案し、最適なものを理由とともに選定
  • git commitする

ポイントは、コミットメッセージ候補を複数出させているところです。 もしclaudeによるコミットメッセージが気に入らなくても、その選定過程を見ることで別途自分で考えるときの参考にできます。

なお、特に指示しない場合とClaude Codeはコミットメッセージに下記のようなフッターを付与してきます。

    🤖 Generated with [Claude Code](https://claude.ai/code)
    
    Co-Authored-By: Claude <noreply@anthropic.com>

今回これは不要なので、制約としてこのようなフッターを追記しないよう指示しました。

具体例

ではこの自動コミットコマンド/commitの具体例として、MCP Python SDKの最新コミットを再現してみます。

本記事執筆時点での最新コミットはこちら:

$ git log -1  
commit c3717e7ad333a234c11d084c047804999a88706e (HEAD -> main, tag: v1.14.0, origin/main, origin/HEAD)
Author: David Soria Parra <167242713+dsp-ant@users.noreply.github.com>
Date:   Thu Sep 11 14:30:06 2025 +0100

    Fix context injection for resources and prompts (#1336)

公式のコミットメッセージは

Fix context injection for resources and prompts (#1336)

となっていますね。

ではこのコミットをgit reset --soft HEAD~1で取り消して(変更内容はステージングで保持)、先述の/commitでClaude Codeに自動コミットさせてみます。

Claude Codeのログ:

> /commit is running… 
  ⎿  Allowed 3 tools for this command

⏺ I'll analyze the diff content and create appropriate commit message candidates for these changes.

⏺ Looking at the diff, I can see the main changes are:

  1. New context injection system: Added src/mcp/server/fastmcp/utilities/context_injection.py with utilities to find and inject Context parameters into functions
  2. Context support in prompts: Modified prompt system to detect and inject Context parameters automatically
  3. Context support in resources: Updated resource templates to handle Context parameters
  4. Context detection improvements: Enhanced tool system's context parameter detection using the new utilities
  5. Test coverage: Added comprehensive tests for context injection in tools, resources, and prompts
  6. Configuration files: Added .gitattribute and updated .gitignore

⏺ 3 Commit Message Candidates:

  1. feat: add automatic context injection for prompts and resources
    - Focuses on the main new feature: automatic context detection and injection
    - Covers the core functionality across prompts and resources
  2. feat: implement unified context parameter detection and injection system
    - Emphasizes the systematic approach and new utilities created
    - Highlights the unified approach across tools, prompts, and resources
  3. feat: extend context injection to prompts and resource templates
    - Focuses on extending existing context injection (from tools) to new areas
    - More specific about what components were enhanced

⏺ Selected Commit Message:

  feat: implement unified context parameter detection and injection system

  Reasoning:

  This message is the most appropriate because:

  1. Accuracy: It accurately reflects the core change - creating a unified system (context_injection.py) that works across all FastMCP components
  2. Scope: Captures the systematic nature of the changes - not just adding context to prompts/resources, but creating reusable utilities
  3. Technical precision: "parameter detection and injection" is more precise than just "context injection" since it involves both finding Context parameters and injecting them
  4. Clarity: Makes it clear this is a new feature that establishes a pattern for the codebase

⏺ Bash(git commit -m "feat: implement unified context parameter detection and injection system")
  ⎿  [main 6944a3e] feat: implement unified context parameter detection and injection system
      12 files changed, 741 insertions(+), 349 deletions(-)
      create mode 100644 .gitattribute
      create mode 100644 src/mcp/server/fastmcp/utilities/context_injection.py

⏺ ✅ Commit created successfully!

  Commit hash: 6944a3eMessage: feat: implement unified context parameter detection and injection system

  The commit includes 12 modified files with 741 insertions and 349 deletions, establishing a unified system for context parameter detection and injection across FastMCP's tools, prompts, and
  resources.

実際に行われたコミットはこちら:

$ git log -1
commit 6944a3e89268b98d5b0d1c316bbdfb5422dc1bdd (HEAD -> main)
Author: bioerrorlog <bioerrorlog.contact@gmail.com>
Date:   Wed Sep 17 19:48:29 2025 +0900

    feat: implement unified context parameter detection and injection system

コミットメッセージ:

feat: implement unified context parameter detection and injection system

もちろん、Issueに対するFixであるというコンテキストは抜けてしまいますが、変更内容の言語化という意味ではわかりやすいメッセージなのではないでしょうか。

もし自動コミットメッセージが気に入らない場合も、Claude Codeが残した思考ログが、コミットメッセージを考える良い材料になると思います。

おわりに

以上、Claude Codeカスタムスラッシュコマンドで自動コミットする方法をまとめました。

ちょっとした作業でも、定型化してAIのサポートを得るようにするだけで人間の負荷を軽減することができますね。

どなたかの参考になれば幸いです。

[関連記事]

www.bioerrorlog.work

www.bioerrorlog.work

参考