DEV Community

Cover image for Auto-generate Commit Messages with LLMs in Your Terminal
Hank Chiu
Hank Chiu

Posted on

Auto-generate Commit Messages with LLMs in Your Terminal

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.
Enter fullscreen mode Exit fullscreen mode

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.

# 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)"
Enter fullscreen mode Exit fullscreen mode

Now, instead of git commit, simply run:

git ca
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (0)

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay