Writing commit messages can be a drag. While IDEs like Cursor can automate this, what if you live in your terminal and want a fast, controllable way to generate conventional commits?
This guide is for you. It's as simple as piping git diff
to a command-line LLM client to create well-formatted commit messages without leaving the terminal.
Quick Summary
- Use a non-interactive LLM client: We need a tool that takes input from a pipe, sends it to the model, and prints the result to standard output.
- Craft a system prompt: Instruct the LLM to generate a message in the Conventional Commits format.
- Create a Git alias: Make the entire process accessible through a simple command like
git ca
.
The System Prompt
First, let's define our instructions for the LLM. This prompt ensures the output is consistently formatted as a Conventional Commit:
Write a commit message in the Conventional Commits format. Use the structure:
<type>(<optional scope>): <short description>
<optional body>
<optional footer>
Example types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
Optionally, include a body for more details in bullet points.
Optionally, in the footer, use BREAKING CHANGE: followed by a detailed explanation of the breaking change.
Just return the commit message, do not include any other text.
LLM Clients for the Terminal
Several command-line tools can handle this task. Here are a few examples of how to use them, piping your staged changes directly to the model.
-
git diff --cached | llm -s '<your-system-prompt>'
-
git diff --cached | gemini -p '<your-system-prompt>'
-
git diff --cached | aichat --prompt '<your-system-prompt>'
-
git diff --cached | claude -p '<your-system-prompt>'
Create a Git Alias
To make this truly seamless, add an alias to your .gitconfig
. This example uses llm
to commit all staged changes and then displays the latest log entry.
Place your full system prompt directly into the alias or save it to a file and reference it.
# In your ~/.gitconfig file
[alias]
ca = "!(git commit -m \"$(git diff --cached | llm -s 'Write a commit message in the Conventional Commits format...')\" && git log --stat -1)"
Now, instead of git commit
, simply run:
git ca
Your staged changes will be sent to the LLM, the generated message will be used for the commit, and you'll see the result instantly. It's a quick, powerful way to keep your workflow moving, all from the comfort of your terminal.