iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✍️

Stop using "fix". Automate high-quality commit messages with gemini-cli and custom git commands

に公開

3-Point Summary

  1. Introduces a custom command that leverages gemini (gemini-cli) to automatically generate commit messages from staged changes (git diff --cached).
  2. Builds a safe workflow where the generated message is confirmed and edited by a human in an editor before committing.
  3. Enables boosting development productivity by calling it with a single git gcommit command (though it does involve waiting for the API response).

1. Introduction

Thinking of commit messages is a task that surprisingly consumes energy. Keeping records according to rules like Conventional Commits is important, but formatting them every single time when you are in a rush is a hassle. Therefore, I created a Git command (git alias) that uses the power of Gemini to generate a commit message draft in one go based on staged changes. This article introduces the script implementation and setup procedure.

Prerequisites

  • gemini (gemini-cli): A tool such as the following repository must be installed and in your PATH.
    • This article uses v0.18.4 (public version as of 2025-11-28) as an example. Newer previews or nightly builds may exist.
    • This article illustrates a simple workflow using -p|--prompt for headless calls and receiving via stdout. If JSON output is required, you can use --output-format json combined with jq.

https://github.com/google-gemini/gemini-cli

  • git: Version 2.x or higher.

2. Why generate commit messages automatically with gemini?

Conventional Commits (formats like feat: ..., fix: ...) are not just for keeping the history clean. They are also vital as a basis for automatic release note generation (creating CHANGELOG.md). For example, if you are using tools like release-please, the content of your commit messages is published to users as release notes exactly as is. In other words, writing careful commit messages is synonymous with creating careful release notes.
However, for a human to think from scratch about "text at a level suitable for release notes" every single time they commit is a very costly task. When in a hurry, it is easy to end up with lazy messages like fix.

By using AI here, you can automate the "summarization of changes" and "application of formatting," reducing the burden while maintaining quality. On the other hand, AI-generated messages are not always correct, and there is a risk of including unintended content. Therefore, the approach in this article adopts a workflow where "the AI writes a draft message, and a human makes the final decision (review) in an editor" rather than "letting the AI commit directly."

3. How to automatically generate commit messages with a git command

3.1 Implementation: Commit message auto-generation script

The script used is as follows:

#!/usr/bin/env bash

set -e
set -o pipefail

# 1. Check staging
# Throwing it to the AI when there are no changes is a waste, so we guard against it here
if git diff --cached --quiet; then
    echo "🚫 No staged changes."
    exit 1
fi

echo "🤖 Gemini is generating a commit message..."

# 2. Set prompt
PROMPT="Create a commit message following Conventional Commits based on the git diff below. Include a title and a body with bullet points. Do not include Markdown code blocks (backticks), and output only as plain text."

# 3. Execution. Pipe the staged diff to `gemini` and pass the standard output directly to `git commit -e -F -`.
git diff --cached | gemini -p "$PROMPT" | git commit -e -F -

Explanation of key points

  • git diff --cached: Targets only the staging area (content added with git add). It does not include unsaved files you are working on.
  • gemini -p "$PROMPT": Calling the AI tool in headless mode here.
  • git commit -e -F -:
    • -F -: Reads the AI output passed via the pipe as the message.

      • -F (--file): Option to specify a file containing the message.
      • -: A Unix/Linux convention representing standard input instead of a filename.
    • -e: Starts an editor (like Vim or Emacs) instead of committing automatically. This provides an opportunity for a human to review and correct the AI output. The editor can be specified via the GIT_EDITOR environment variable or git config --global core.editor. Since editors do not launch in CI or headless environments, you may need to consider treating -e as an optional flag in the script.

3.2 Setup procedure

Set this up so you can use the script as git gcommit.

Step 1: Place the script

Save the script in a directory in your PATH (e.g., ~/.local/bin).

# Create the directory if it does not exist
mkdir -p ~/.local/bin

# Create the file (open with your favorite editor and copy-paste the script above. A location in your PATH is preferred)
vi ~/.local/bin/git-gcommit.sh

# Grant execution permissions
chmod +x ~/.local/bin/git-gcommit.sh

Step 2: Register Git alias

Set up an alias so you can call it like a subcommand from Git.

# Option A (Recommended): If you placed the script in your PATH
git config --global alias.gcommit '!git-gcommit.sh'

# Option B: If using an absolute path (please replace the username)
git config --global alias.gcommit '!/Users/<yourname>/.local/bin/git-gcommit.sh'

Note: Aliases prefixed with ! are executed by git via /bin/sh -c. Since ~ expansion or shell-specific syntax might not behave as expected, using an absolute path or placing it in your PATH is recommended.

4. Basic usage

Usage steps are as follows:

  1. Stage the files you want to commit using git add.
git add .
  1. Run the git gcommit command to call your custom script. Gemini will analyze the diff and generate a message (expect a wait of several dozen seconds).
git gcommit

prompt

  1. The Git editor will automatically open, with the AI-written message already inserted (I am using vim in my example). Review the content, make corrections if necessary, and save/exit to complete the commit.

Message editing screen

5. Notes

  • Waiting time: AI responses can take anywhere from a few seconds to over ten seconds. In situations where you frequently commit small changes, it might be faster to write them manually. It is particularly powerful when committing a significant chunk of changes at once.

6. Summary

  • By combining gemini and Git options, we have enabled a safe and fast "AI suggestion -> human review" commit process.
  • With the single git gcommit command, you can lower the psychological hurdle of creating commit messages. Please try customizing the prompt to your own preferences and giving it a shot.

Discussion