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
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)
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
Then just copy-paste:
git commit -m "$(python auto-commit.py)"
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"
Now just run:
git ai-commit
Try It
- Copy the script (20 lines above)
- Get a free API key → aibridge-api.com
- Stage changes → git add .
- Run → python auto-commit.py
Your future self (and your team) will thank you for the clean git history.




Top comments (0)