{"slug": "i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python", "title": "I Fixed the \"AI Commit Messages\" Problem in 20 Lines of Python", "summary": "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.", "body_md": "You've probably seen that trending post — *\"I Asked AI to Write My Commit Messages and It Was Embarrassing.\"*\n\nSame. But instead of accepting embarrassing output, I fixed it.\n\nHere'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.\n\nNo new packages. No API key. If you have [Claude Code](https://claude.ai/code), you're already set.\n\n``` python\n#!/usr/bin/env python3\nimport subprocess\n\nSYSTEM = (\n    \"You are a git commit message generator. \"\n    \"Output ONLY the commit message — no explanation, no markdown, no quotes. \"\n    \"Follow Conventional Commits: type(scope): subject. \"\n    \"Types: feat, fix, docs, style, refactor, test, chore. \"\n    \"Subject: imperative, lowercase, max 72 chars.\"\n)\n\ndiff = subprocess.check_output([\"git\", \"diff\", \"--staged\"], text=True)\nif not diff.strip():\n    print(\"Nothing staged. Run `git add` first.\")\n    raise SystemExit(1)\n\nmsg = subprocess.check_output(\n    [\"claude\", \"-p\", SYSTEM + \"\\n\\n\" + diff],\n    text=True,\n).strip()\nprint(msg)\n```\n\nThat's it. 20 lines. Uses the `claude`\n\nCLI under the hood — no API key, no config, just your existing Claude Code OAuth session.\n\n**The system prompt does the heavy lifting.** Three constraints:\n\n`Output ONLY the commit message`\n\n— no preamble, no explanation`Follow Conventional Commits`\n\n— `feat`\n\n, `fix`\n\n, `chore`\n\n, etc.`max 72 chars`\n\n— keeps it readable in git log**The 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.\n\n```\n# No setup needed if you have Claude Code. Just:\ngit add .\npython /path/to/git_commit.py\n# → feat(server): add AI commit message generator via Claude CLI\n```\n\nOr wire it into a git alias:\n\n```\ngit config --global alias.ai '!python /path/to/git_commit.py'\n# git ai\n```\n\nBefore:\n\n```\nupdate stuff\nfix bug\nWIP\nadded the thing\n```\n\nAfter:\n\n```\nfeat(api): add generate_commit_message tool to MCP server\nfix(auth): handle expired token on refresh\nrefactor(db): extract query builder into separate module\n```\n\nI also wrapped it as an MCP tool so Claude Code can call it directly from any conversation:\n\n``` php\n@mcp.tool()\ndef generate_commit_message(diff: str) -> str:\n    \"\"\"Generate a Conventional Commits message from a git diff string.\"\"\"\n    full = SYSTEM + \"\\n\\n\" + diff\n    return subprocess.check_output([\"claude\", \"-p\", full], text=True).strip()\n```\n\nFull project: [github.com/enjoy-kumawat/my-git-manager](https://github.com/enjoy-kumawat/my-git-manager)\n\n20 lines. No new dependencies. No API key. Conventional Commits every time.\n\nThe embarrassing part was waiting this long to build it.", "url": "https://wpnews.pro/news/i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python", "canonical_source": "https://dev.to/enjoy_kumawat/i-fixed-the-ai-commit-messages-problem-in-50-lines-of-python-3a5a", "published_at": "2026-06-21 09:14:30+00:00", "updated_at": "2026-06-21 09:36:38.243734+00:00", "lang": "en", "topics": ["developer-tools", "large-language-models", "generative-ai"], "entities": ["Claude Code", "Conventional Commits", "MCP", "Git", "Python"], "alternates": {"html": "https://wpnews.pro/news/i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python", "markdown": "https://wpnews.pro/news/i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python.md", "text": "https://wpnews.pro/news/i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python.txt", "jsonld": "https://wpnews.pro/news/i-fixed-the-ai-commit-messages-problem-in-20-lines-of-python.jsonld"}}