How to Write Better Git Commit Messages with AI — The AI Leverage Weekly
← All posts<br>How to Write Better Git Commit Messages with AI
2026-06-15
Bad commit messages are a tax you pay forever. You hit git log six months later and find a wall of "fix stuff", "WIP", "asdf", and "final final v2" — and now you're archaeologist instead of engineer. The good news: AI can eliminate this problem almost entirely, and in this walkthrough I'll show you exactly how to wire it into your workflow with copy-paste prompts you can use today.
Why Commit Messages Fail (And Why AI Actually Helps Here)
The root cause isn't laziness — it's timing. You write the commit message right after a long coding session, when your brain is fried and you just want to push. Context is all in your head, not on the screen.
AI flips this. You feed it the diff and your rough notes; it drafts a structured message. You edit for accuracy. Total time: 30 seconds. The output is consistently better than what most engineers write under pressure.
Step 1: Stage Your Changes and Generate a Diff
Before prompting, get a clean diff of what you're about to commit:
git diff --staged
Enter fullscreen mode
Exit fullscreen mode
Copy the output. If the diff is large (500+ lines), narrow it to the most meaningful files:
git diff --staged -- path/to/relevant/file.ts
Enter fullscreen mode
Exit fullscreen mode
You don't need to paste every line into the prompt — a representative slice plus a plain-English summary of your intent is enough.
Step 2: Use This Base Prompt
Paste this into your AI assistant of choice (ChatGPT, Claude, Copilot Chat, etc.):
You are a senior engineer helping me write a Git commit message.
Here is the staged diff:
My intent:
Write a commit message using the Conventional Commits format:<br>- First line: type(scope): short imperative summary (max 72 chars)<br>- Blank line<br>- Body: 2–4 bullet points explaining WHAT changed and WHY, not HOW<br>- If there's a breaking change, add a BREAKING CHANGE footer
Output only the commit message, no commentary.
Enter fullscreen mode
Exit fullscreen mode
A real example output for a token-refresh bug fix might look like this:
fix(auth): prevent silent token refresh on expired session
- Remove automatic refresh call when session TTL has already elapsed<br>- Add explicit expiry check before calling refreshToken()<br>- Prevents a race condition that caused duplicate refresh requests<br>under slow network conditions
Enter fullscreen mode
Exit fullscreen mode
That's immediately useful to the next engineer reading git log.
Step 3: Refine With a One-Line Follow-Up
If the first draft is close but not quite right, don't re-explain from scratch. Just correct the specific problem:
The scope should be "session" not "auth", and the first line<br>is too long — tighten it to under 60 characters.
Enter fullscreen mode
Exit fullscreen mode
AI handles surgical edits like this well. You're the reviewer; it's the first-draft writer.
Step 4: Build a Shell Alias for Speed
The friction of copy-pasting manually will kill this habit. Automate it. Add this to your .zshrc or .bashrc:
alias gcm='git diff --staged | pbcopy && echo "Diff copied. Paste into your AI prompt."'
Enter fullscreen mode
Exit fullscreen mode
On Linux, swap pbcopy for xclip -selection clipboard. Now your staged diff is on your clipboard in one command, ready to paste into any AI chat.
For teams using the GitHub CLI, you can go further and pipe directly into a script that calls an API — but the manual copy-paste habit alone will get you 80% of the value.
Step 5: Add a Linter to Enforce the Format
Writing good messages is only half the battle — the other half is making sure they don't regress. Add commitlint to your repo:
npm install --save-dev @commitlint/cli @commitlint/config-conventional<br>echo "module.exports = { extends: ['@commitlint/config-conventional'] };" > commitlint.config.js<br>npx husky add .husky/commit-msg 'npx --no -- commitlint --edit "$1"'
Enter fullscreen mode
Exit fullscreen mode
Now any commit that doesn't follow Conventional Commits format gets rejected before it ever touches your branch. Pair this with the AI prompt above and the format issues go away almost entirely.
This prompt pattern is one of the ones I've packaged into The AI Leverage Playbook: 50 Prompts & Workflows for Engineers — but the version above is enough to get real value on its own.
What Good Looks Like at Scale
Once this habit is set, your git log becomes actual documentation. You can:
Run git log --oneline to get a readable changelog for a release
Use git log --grep="fix(auth)" to find every auth-related fix without grepping source
Onboard new engineers by pointing them at commit history, not Confluence
The commit message becomes a first-class artifact, not an afterthought.
Quick Reference: The Prompt, One More Time
You are a senior engineer helping me write a Git commit message.
Diff:
Intent:
Format: Conventional Commits. First line ≤72...