Your git log looks like this:
fix bug
update stuff
wip
asdf
more changes
final fix
final fix 2
Every developer has been there. Writing good commit messages takes effort you'd rather spend coding. But bad commit messages make debugging, reverting, and onboarding painful.
The fix: let AI write your commit messages from your actual diff.
The prepare-commit-msg Hook
Git has a hook called prepare-commit-msg that runs before the commit message editor opens. We'll use it to auto-generate messages.
Create .git/hooks/prepare-commit-msg:
#!/bin/bash
# Only run for regular commits (not merges, amends, etc.)
if [ "$2" != "" ]; then
exit 0
fi
COMMIT_MSG_FILE=$1
# Get staged diff
DIFF=$(git diff --cached --stat)
DIFF_CONTENT=$(git diff --cached | head -c 5000)
if [ -z "$DIFF" ]; then
exit 0
fi
# Generate commit message
MESSAGE=$(curl -s https://api.anthropic.com/v1/messages \
-H "content-type: application/json" \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d "{
\"model\": \"claude-sonnet-4-20250514\",
\"max_tokens\": 100,
\"messages\": [{
\"role\": \"user\",
\"content\": \"Write a conventional commit message for this diff. Format: type(scope): description. One line, max 72 chars. Types: feat, fix, refactor, docs, test, chore. No quotes around the message.\\n\\nFiles:\\n$DIFF\\n\\nDiff:\\n$DIFF_CONTENT\"
}]
}" | jq -r '.content[0].text')
# Write to commit message file
echo "$MESSAGE" > "$COMMIT_MSG_FILE"
Make it executable:
chmod +x .git/hooks/prepare-commit-msg
What You Get
Now when you run git commit, the hook reads your staged changes and generates a message:
git add src/auth/login.ts src/auth/middleware.ts
git commit
# Auto-generated message: feat(auth): add JWT login with token refresh
git add src/utils/format.ts
git commit
# Auto-generated message: fix(utils): handle null values in date formatter
git add tests/auth.test.ts
git commit
# Auto-generated message: test(auth): add integration tests for login flow
The message appears in your editor. Keep it, edit it, or rewrite it. You're always in control.
Team-Wide Setup with a Shared Hook
Git hooks live in .git/hooks/ which isn't committed. To share with your team, use a hooks directory:
mkdir .githooks
cp .git/hooks/prepare-commit-msg .githooks/
git config core.hooksPath .githooks
Commit .githooks/ to your repo. Add setup instructions to your README:
git config core.hooksPath .githooks
export ANTHROPIC_API_KEY=your-key-here
A Faster Alternative: Local Models
Don't want to hit an API on every commit? Use a local model:
# Using ollama
MESSAGE=$(echo "$DIFF_CONTENT" | ollama run codellama \
"Write a conventional commit message for this diff. One line, max 72 chars.")
Local models are faster (no network latency) and free. The quality is slightly lower but still better than "fixed stuff."
Parsing Commit Messages for Changelogs
Once your commit messages follow a consistent format, you can automatically generate changelogs. Extract structured data from your git log using StructureAI:
COMMITS=$(git log --oneline -20)
curl -X POST https://api-service-wine.vercel.app/api/extract \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_KEY" \
-d "{
\"text\": \"$COMMITS\",
\"schema\": \"custom\",
\"custom_fields\": [\"features\", \"bug_fixes\", \"breaking_changes\", \"version_bump\"]
}"
Returns:
{
"features": ["JWT login with token refresh", "user profile page"],
"bug_fixes": ["null values in date formatter", "race condition in auth middleware"],
"breaking_changes": [],
"version_bump": "minor"
}
Feed that into your CHANGELOG.md generator and you have fully automated releases.
Conventional Commits Cheat Sheet
The AI uses these types by default:
| Type | When to use |
|---|---|
feat |
New feature |
fix |
Bug fix |
refactor |
Code change that doesn't fix a bug or add a feature |
docs |
Documentation only |
test |
Adding or fixing tests |
chore |
Build, CI, dependencies |
perf |
Performance improvement |
Cost
Claude Sonnet costs about $0.003 per 1K input tokens. A typical diff is 200-500 tokens. That's roughly $0.001 per commit. At 20 commits per day, that's $0.02/day or $0.60/month.
You spend more on coffee.
Setup in 2 Minutes
- Create the hook file (copy the script above)
chmod +x .git/hooks/prepare-commit-msg- Set
ANTHROPIC_API_KEYin your shell profile - Run
git commitand watch the magic
Your git log will finally tell a coherent story. Future you will be grateful.
Built by Avatrix LLC. Turn messy text into clean JSON with StructureAI.
Top comments (0)