DEV Community

Daniel Dong
Daniel Dong

Posted on

I Let AI Write My Commit Messages (And Never Looked Back)

Writing commit messages is the most boring part of development. Here's a 20-line script that does it for you — using git diff + AI.

Admit it. You've written this commit message before:

fix
update
asdfasdf
final final v2
Enter fullscreen mode Exit fullscreen mode

We've all been there. Writing good commit messages takes time. But bad commits make your git history unreadable.

Here's how I automated it.

The Script (20 Lines)

#!/usr/bin/env python3
import subprocess
from openai import OpenAI

client = OpenAI(
    api_key="mb-your-key",
    base_url="https://aibridge-api.com/v1"
)

# Get staged diff
diff = subprocess.run(
    ["git", "diff", "--cached"],
    capture_output=True, text=True
).stdout

if not diff:
    print("Nothing staged. Run: git add .")
    exit(1)

# Let AI write the commit message
response = client.chat.completions.create(
    model="deepseek-coder",
    messages=[{
        "role": "user",
        "content": f"Write a concise commit message for this diff. "
                   f"Use conventional commits format (feat/fix/docs/...):\n\n{diff}"
    }],
    max_tokens=100
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

Usage

# Stage your changes
git add .

# Generate a commit message
python auto-commit.py

# Output:
# feat(auth): add rate limiting to login endpoint
#
# - Add RateLimiter class with sliding window
# - Apply 5 req/min limit to /auth/login
# - Return 429 with Retry-After header
Enter fullscreen mode Exit fullscreen mode

Then just copy-paste:

git commit -m "$(python auto-commit.py)"
Enter fullscreen mode Exit fullscreen mode

Why This Works

  • Consistent format — Always conventional commits
  • Saves time — 30 seconds → 0 seconds
  • Better history — Readable git log
  • Free — DeepSeek-coder costs $0.14/1M tokens ##Make It a Git Alias Add to ~/.gitconfig:
[alias]
    ai-commit = "!f() { git commit -m \"$(python ~/auto-commit.py)\"; }; f"
Enter fullscreen mode Exit fullscreen mode

Now just run:

git ai-commit
Enter fullscreen mode Exit fullscreen mode

Try It

  1. Copy the script (20 lines above)
  2. Get a free API key → aibridge-api.com
  3. Stage changes → git add .
  4. Run → python auto-commit.py

Your future self (and your team) will thank you for the clean git history.

11

22

33

44

Top comments (0)