Auto-generate Commit Messages with LLMs in Your Terminal

Auto-generate Commit Messages with LLMs in Your Terminal

posted Originally published at hankchiu.tw 2 min read

Original post: https://hankchiu.tw/writings/auto-generate-commit-messages-with-ll-ms-in-your-terminal/

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

  1. 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.
  2. Craft a system prompt: Instruct the LLM to generate a message in the Conventional Commits format.
  3. 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.

  • LLM CLI

    git diff --cached | llm -s '<your-system-prompt>'
    
  • Gemini CLI

    git diff --cached | gemini -p '<your-system-prompt>'
    
  • aichat

    git diff --cached | aichat --prompt '<your-system-prompt>'
    
  • Claude CLI

    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.

[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.

2 Comments

0 votes
0 votes

More Posts

I’m a Senior Dev and I’ve Forgotten How to Think Without a Prompt

Karol Modelskiverified - Mar 19

Your Tech Stack Isn’t Your Ceiling. Your Story Is

Karol Modelskiverified - Apr 9

Your AI Doesn't Just Write Tests. It Runs Them Too.

Kevin Martinez - May 12

TypeScript Complexity Has Finally Reached the Point of Total Absurdity

Karol Modelskiverified - Apr 23

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!