I Built An AI Git Commit Message Generator

I used to write bad commit messages like "fixed bug" or "updated stuff." This made my pull requests messy. I decided to automate it using AI. I wanted to generate clean, conventional commit messages from my code diffs.

It was harder than I thought. A simple prompt did not work.

The early attempts failed for many reasons:

  • The AI wrote long paragraphs instead of short lines.
  • It ignored the required "feat:" or "fix:" prefixes.
  • It hallucinated features that did not exist in the code.
  • Local models were too slow and wrote in a poetic, useless style.

I eventually built a hybrid system that works. Here is the structure I use:

  • Type Classification: I ask the model to pick a type like feat, fix, or chore before writing the message.
  • Context Truncation: I only send the first 250 lines of the diff. This saves money and keeps the focus sharp.
  • Validation: I use a regex to check the output. If the message is wrong, the script tries again.
  • Low Temperature: I set the temperature to 0.2. This makes the output consistent and boring. Boring is good for logs.

I do not use automatic commit hooks. AI makes mistakes. I run the script as a git alias. It suggests a message, and I review it before I commit. Manual review is the best insurance against a bad git history.

Three lessons I learned:

  • Token limits are your enemy. Large diffs will break your budget or your prompt.
  • Validation is mandatory. Without it, you will get nonsense.
  • Speed matters. I use GPT-4o-mini because it is fast and cheap.

If you work on sensitive code, do not send your diffs to an external API. Use a local model instead.

How do you handle your commit messages? Do you use AI or write them by hand?

Source: https://dev.to/__c1b9e06dc90a7e0a676b/i-built-a-git-commit-message-generator-with-ai-heres-what-i-learned-2534