LLM-Driven Commit Messages
Writing good commit messages is a chore that most developers hate, yet everyone agrees is critical. When you’re in the zone, summarizing the last hour of refactoring feels like a distraction. So, we end up with “fix bugs” or “wip”.
This is where LLMs shine. Instead of writing messages from scratch, you can automate the process using Git hooks and the Gemini API to generate structured, Conventional Commit compliant messages based on your actual diffs.
The Problem with Manual Commits
A good commit message explains why a change was made, not just what changed. The diff shows the “what”. The problem is context switching. You’ve just solved a complex state management bug, and now you have to switch from coding to technical writing.
By automating this, we enforce a standard without the cognitive overhead.
Building the Git Hook
We can use a prepare-commit-msg Git hook to intercept the commit process.
This script reads the staged diff, sends it to Gemini, and populates the commit message template automatically.
import { GoogleGenerativeAI } from '@google/generative-ai';
import { execSync } from 'child_process';
import fs from 'fs';
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const diff = execSync('git diff --cached').toString();
const prompt = `
Analyze the following git diff and generate a Conventional Commit message.
Use the format: <type>(<scope>): <subject>
Followed by a blank line and a brief body explaining the *why*.
Diff:
${diff}
`;
// Fetch and write the message...
This simple script uses the staged changes as context. Because Gemini has a large context window, it can easily handle massive refactors, summarizing the core intent accurately.
Refining the Output
The key to getting good results is the system prompt. You must explicitly define the output format and ban filler words. Instruct the model to avoid generic phrases like “This commit updates…” and instead start directly with the action: “Refactor state management to…”
If the output isn’t quite right, you can always edit it before finalizing the commit, but starting with a solid draft saves significant time.
Takeaway
Automating your commit messages with an LLM eliminates context switching and ensures a pristine, readable project history.