以下の内容はhttps://en.bioerrorlog.work/entry/git-commit-with-claude-code-custom-slash-commandより取得しました。


Auto-Commit with a Custom Slash Command in Claude Code

This post introduces a convenient way to automate git commit using a custom slash command in Claude Code.

Introduction

Writing a good git commit message is important, but it can also be a task that takes a bit of mental effort.

By using Claude Code's custom slash command, you can make this task easier. Here's one way to do it.

Note: This article was translated from my original post.

Auto-Commit with a Custom Slash Command in Claude Code

How to Do It

The basics of creating a custom slash command are covered in another article.
Please check it here:

en.bioerrorlog.work

Now, let’s take a look at an example slash command for auto-committing.

Create a file at ~/.claude/commands/commit.md and write the following prompt. This will allow you to trigger an automatic commit with the slash command /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

The task here is simple:

  • Understand the current changes
  • Generate 3 commit message candidates and select the best one with a reason
  • Run git commit

The key point is asking for multiple commit message suggestions. Even if you don't like Claude's message, seeing the selection process can help inspire your own.

By default, Claude Code adds the following footer to commit messages:

    🤖 Generated with [Claude Code](https://claude.ai/code)

    Co-Authored-By: Claude <noreply@anthropic.com>

Since I don't need that, I instruct it not to add the footer as a constraint.

Example

As an example, let's recreate the latest commit in the MCP Python SDK using the /commit slash command.

At the time of writing, the latest commit is:

$ 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)

The official commit message is:

Fix context injection for resources and prompts (#1336)

Now we undo this commit with git reset --soft HEAD~1 (keeping the changes staged), and let Claude Code auto-commit using the /commit command.

Claude Code log:

> /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.

Here's the actual committed result:

$ 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

Commit message:

feat: implement unified context parameter detection and injection system

While it doesn’t mention that this was a fix for an issue, the message is clear and effectively describes the change.

Even if you don’t use the auto-generated commit message as-is, Claude Code’s reasoning log is a valuable reference for writing your own.

Conclusion

That’s how to automate commits using a custom slash command in Claude Code.

Even small tasks can be standardized and supported by AI to reduce human workload.

I hope this is helpful to someone!

[Related Articles]

en.bioerrorlog.work

en.bioerrorlog.work

References




以上の内容はhttps://en.bioerrorlog.work/entry/git-commit-with-claude-code-custom-slash-commandより取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14