{"slug": "how-to-write-better-git-commit-messages-with-ai", "title": "How to Write Better Git Commit Messages with AI", "summary": "A developer has created an AI-powered workflow to generate better Git commit messages using tools like ChatGPT, Claude, or Copilot Chat. The method involves feeding a staged diff and intent into a prompt that outputs a Conventional Commits-formatted message, reducing the time to 30 seconds. The developer also recommends automating the diff copy with a shell alias and enforcing format compliance with commitlint.", "body_md": "Bad commit messages are a tax you pay forever. You hit `git log`\n\nsix 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.\n\nThe 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.\n\nAI 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.\n\nBefore prompting, get a clean diff of what you're about to commit:\n\n```\ngit diff --staged\n```\n\nCopy the output. If the diff is large (500+ lines), narrow it to the most meaningful files:\n\n```\ngit diff --staged -- path/to/relevant/file.ts\n```\n\nYou don't need to paste every line into the prompt — a representative slice plus a plain-English summary of your intent is enough.\n\nPaste this into your AI assistant of choice (ChatGPT, Claude, Copilot Chat, etc.):\n\n```\nYou are a senior engineer helping me write a Git commit message.\n\nHere is the staged diff:\n<paste diff here>\n\nMy intent: <one sentence about what this change accomplishes and why>\n\nWrite a commit message using the Conventional Commits format:\n- First line: type(scope): short imperative summary (max 72 chars)\n- Blank line\n- Body: 2–4 bullet points explaining WHAT changed and WHY, not HOW\n- If there's a breaking change, add a BREAKING CHANGE footer\n\nOutput only the commit message, no commentary.\n```\n\nA real example output for a token-refresh bug fix might look like this:\n\n```\nfix(auth): prevent silent token refresh on expired session\n\n- Remove automatic refresh call when session TTL has already elapsed\n- Add explicit expiry check before calling refreshToken()\n- Prevents a race condition that caused duplicate refresh requests\n  under slow network conditions\n```\n\nThat's immediately useful to the next engineer reading `git log`\n\n.\n\nIf the first draft is close but not quite right, don't re-explain from scratch. Just correct the specific problem:\n\n```\nThe scope should be \"session\" not \"auth\", and the first line\nis too long — tighten it to under 60 characters.\n```\n\nAI handles surgical edits like this well. You're the reviewer; it's the first-draft writer.\n\nThe friction of copy-pasting manually will kill this habit. Automate it. Add this to your `.zshrc`\n\nor `.bashrc`\n\n:\n\n```\nalias gcm='git diff --staged | pbcopy && echo \"Diff copied. Paste into your AI prompt.\"'\n```\n\nOn Linux, swap `pbcopy`\n\nfor `xclip -selection clipboard`\n\n. Now your staged diff is on your clipboard in one command, ready to paste into any AI chat.\n\nFor 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.\n\nWriting good messages is only half the battle — the other half is making sure they don't regress. Add `commitlint`\n\nto your repo:\n\n```\nnpm install --save-dev @commitlint/cli @commitlint/config-conventional\necho \"module.exports = { extends: ['@commitlint/config-conventional'] };\" > commitlint.config.js\nnpx husky add .husky/commit-msg 'npx --no -- commitlint --edit \"$1\"'\n```\n\nNow 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.\n\nThis 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.\n\nOnce this habit is set, your `git log`\n\nbecomes actual documentation. You can:\n\n`git log --oneline`\n\nto get a readable changelog for a release`git log --grep=\"fix(auth)\"`\n\nto find every auth-related fix without grepping sourceThe commit message becomes a first-class artifact, not an afterthought.\n\n```\nYou are a senior engineer helping me write a Git commit message.\n\nDiff:\n<paste>\n\nIntent: <one sentence>\n\nFormat: Conventional Commits. First line ≤72 chars, imperative mood.\nBody: 2–4 bullets on what changed and why. BREAKING CHANGE footer if needed.\nOutput the commit message only.\n```\n\nSave that. Reach for it every time you're about to type \"fix stuff.\"\n\nI break down one workflow like this every week in The AI Leverage Weekly — practical, no fluff, free. Subscribe: [https://theaileverageweekly.beehiiv.com/subscribe?utm_source=devto&utm_medium=article&utm_campaign=long_w6](https://theaileverageweekly.beehiiv.com/subscribe?utm_source=devto&utm_medium=article&utm_campaign=long_w6)", "url": "https://wpnews.pro/news/how-to-write-better-git-commit-messages-with-ai", "canonical_source": "https://dev.to/leveragenotes/how-to-write-better-git-commit-messages-with-ai-1j00", "published_at": "2026-06-15 09:00:58+00:00", "updated_at": "2026-06-15 09:10:42.469024+00:00", "lang": "en", "topics": ["developer-tools", "artificial-intelligence", "large-language-models"], "entities": ["ChatGPT", "Claude", "Copilot Chat", "GitHub CLI", "commitlint", "Husky", "Conventional Commits"], "alternates": {"html": "https://wpnews.pro/news/how-to-write-better-git-commit-messages-with-ai", "markdown": "https://wpnews.pro/news/how-to-write-better-git-commit-messages-with-ai.md", "text": "https://wpnews.pro/news/how-to-write-better-git-commit-messages-with-ai.txt", "jsonld": "https://wpnews.pro/news/how-to-write-better-git-commit-messages-with-ai.jsonld"}}