How to Write Better Git Commit Messages with AI
You've been there: it's 6 PM, you've finally fixed that gnarly bug, and you type git commit -m "fix stuff". Six months later, you're digging through git log trying to understand why a specific change was made, and all you see is a wall of "fix stuff", "updates", and "wip".
Bad commit messages are a tax you pay forever. Every time you run git blame, bisect a regression, or onboard a new teammate, unclear commits slow you down. The good news? AI can help you write better ones—without adding friction to your workflow.
Why Commit Messages Actually Matter
Before we talk about AI, let's establish why this matters. A good commit message should answer three questions:
- What changed? (the diff already shows this, so don't just repeat it)
- Why did it change? (the business reason, not the technical implementation)
- What's the impact? (what breaks if this is reverted?)
Consider these two examples:
Bad:
fix bug
Good:
fix: prevent duplicate webhook processing in payment handler
Webhook providers like Stripe can send the same event multiple times.
Without idempotency checks, users were being charged twice.
Adds a processed_events table to track webhook IDs we've already handled.
The second message tells a story. It explains the user impact, the root cause, and the solution. When you're debugging a payment issue at 2 AM, this context is gold.
The problem? Writing messages like this takes time and mental energy—commodities that are in short supply when you're deep in flow state.
How AI Can Help (Without Getting in the Way)
Modern AI tools can analyze your staged changes and generate commit messages that are:
- Contextually aware: They read your diff and understand what actually changed
- Conventionally formatted: They follow patterns like Conventional Commits
- Customizable: You can tune them to match your team's style
Here are three approaches, from lightweight to integrated:
1. CLI Tools: Generate on Demand
Tools like aicommits (powered by OpenAI) or CommitAssist let you generate messages on the fly:
git add .
aicommits
The tool reads your staged diff, sends it to an LLM, and proposes a message. You can accept it, edit it, or regenerate. This works well if you want control but hate staring at a blank commit message prompt.
Pro tip: Configure it to use Conventional Commits format by default:
aicommits config set type=conventional
2. Git Hooks: Automatic Suggestions
For a more integrated experience, use a prepare-commit-msg hook that pre-fills your editor with an AI-generated draft:
#!/bin/sh
# .git/hooks/prepare-commit-msg
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
# Only run for regular commits (not merges, rebases, etc.)
if [ -z "$COMMIT_SOURCE" ]; then
# Generate message from staged diff
DIFF=$(git diff --cached)
AI_MSG=$(echo "$DIFF" | your-ai-tool-here)
# Prepend to commit message file
echo "$AI_MSG" > "$COMMIT_MSG_FILE.new"
cat "$COMMIT_MSG_FILE" >> "$COMMIT_MSG_FILE.new"
mv "$COMMIT_MSG_FILE.new" "$COMMIT_MSG_FILE"
fi
Now every time you run git commit, your editor opens with a suggested message already populated. Edit it as needed, then save.
3. IDE Extensions: In-Editor Assistance
If you commit from VS Code or JetBrains, extensions like Copilot for Git or GitLens AI can suggest messages directly in the commit UI. This is the lowest-friction option—no context switching, no CLI commands.
In VS Code with GitHub Copilot, for example:
- Stage your changes
- Open the Source Control panel
- Click the sparkle icon next to the commit message box
- Copilot analyzes your diff and suggests a message
A Practical Workflow That Actually Works
Here's how to integrate AI commit messages without slowing down:
-
Make small, focused commits: AI works best when each commit does one thing. If your diff touches 15 files across 3 features, even the best AI will produce a vague message.
-
Stage incrementally: Use
git add -pto stage logical chunks, not entire files. This gives the AI clearer context. -
Review and edit: AI-generated messages are starting points, not gospel. Always read the suggestion and adjust for:
- Context the AI can't see (related to a specific ticket, breaking change, etc.)
- Tone and style preferences
- Sensitive information that shouldn't be in the log
-
Teach your team the pattern: If you're on a team, document your AI tool choice and message format in your CONTRIBUTING.md. Consistency across commits is more valuable than perfect individual messages.
The Takeaway
You don't need to choose between fast commits and good commit messages. AI tools give you both—as long as you:
- Pick a tool that fits your workflow (CLI, hook, or IDE)
- Commit small, focused changes
- Treat AI suggestions as drafts, not final copy
Start simple: try aicommits on your next feature branch. If you find yourself editing the suggestions less and less, you'll know the approach is working. And six months from now, when you're digging through git log looking for that payment bug fix, you'll thank yourself.
Try it yourself: Install aicommits with npm install -g aicommits, configure your OpenAI API key, and run it on your next commit. See how close the AI gets to what you would have written—and adjust from there.