cd /news/developer-tools/i-fixed-the-ai-commit-messages-probl… · home topics developer-tools article
[ARTICLE · art-35449] src=dev.to ↗ pub= topic=developer-tools verified=true sentiment=↑ positive

I Fixed the "AI Commit Messages" Problem in 20 Lines of Python

A developer created a 20-line Python script that generates high-quality git commit messages using Claude Code's CLI. The script combines a strict system prompt enforcing Conventional Commits format with the actual staged diff, eliminating the need for API keys or additional packages. The tool is available as a standalone script or an MCP tool for Claude Code.

read2 min views1 publishedJun 21, 2026

You've probably seen that trending post — "I Asked AI to Write My Commit Messages and It Was Embarrassing."

Same. But instead of accepting embarrassing output, I fixed it.

Here's the thing: the problem isn't AI writing commit messages. The problem is how you ask it. One clear system prompt + the actual diff = surprisingly good results.

No new packages. No API key. If you have Claude Code, you're already set.

#!/usr/bin/env python3
import subprocess

SYSTEM = (
    "You are a git commit message generator. "
    "Output ONLY the commit message — no explanation, no markdown, no quotes. "
    "Follow Conventional Commits: type(scope): subject. "
    "Types: feat, fix, docs, style, refactor, test, chore. "
    "Subject: imperative, lowercase, max 72 chars."
)

diff = subprocess.check_output(["git", "diff", "--staged"], text=True)
if not diff.strip():
    print("Nothing staged. Run `git add` first.")
    raise SystemExit(1)

msg = subprocess.check_output(
    ["claude", "-p", SYSTEM + "\n\n" + diff],
    text=True,
).strip()
print(msg)

That's it. 20 lines. Uses the claude

CLI under the hood — no API key, no config, just your existing Claude Code OAuth session.

The system prompt does the heavy lifting. Three constraints:

Output ONLY the commit message

— no preamble, no explanationFollow Conventional Commits

feat

, fix

, chore

, etc.max 72 chars

— keeps it readable in git logThe diff is the context. You're not asking "write a commit message". You're asking "given these exact changes, what happened?" That's a much more answerable question.

git add .
python /path/to/git_commit.py

Or wire it into a git alias:

git config --global alias.ai '!python /path/to/git_commit.py'

Before:

update stuff
fix bug
WIP
added the thing

After:

feat(api): add generate_commit_message tool to MCP server
fix(auth): handle expired token on refresh
refactor(db): extract query builder into separate module

I also wrapped it as an MCP tool so Claude Code can call it directly from any conversation:

@mcp.tool()
def generate_commit_message(diff: str) -> str:
    """Generate a Conventional Commits message from a git diff string."""
    full = SYSTEM + "\n\n" + diff
    return subprocess.check_output(["claude", "-p", full], text=True).strip()

Full project: github.com/enjoy-kumawat/my-git-manager

20 lines. No new dependencies. No API key. Conventional Commits every time.

The embarrassing part was waiting this long to build it.

── more in #developer-tools 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/i-fixed-the-ai-commi…] indexed:0 read:2min 2026-06-21 ·