ENAPIUZ - BLOG

Vadim Shashin

[Vah-déem Sháh-shin]

AI Commit Messages in Lazygit

By Vadim Shashin 2 min read

Install and configure Codex CLI first; the helper assumes the codex command works in your shell.

Install the helper script

Make the helper executable at ~/.local/bin/codex-git-commit.sh so it can run from the Lazygit custom command.

#!/usr/bin/env bash
set -euo pipefail

# Go to repo root
repo_root="$(git rev-parse --show-toplevel)"
cd "$repo_root"

# Ensure there are staged changes
if git diff --cached --quiet; then
  echo "No staged changes. Stage something first."
  exit 1
fi

# Grab staged diff & staged file list
staged_diff="$(git diff --cached)"
staged_files="$(git diff --cached --name-only)"

# Build the prompt for Codex
prompt_header=$'You are a tool that writes Git commit messages.\n\nTask: Write a concise Git commit message based ONLY on the staged changes.\n\nConstraints:\n- First line: <= 50 characters, imperative mood, no trailing period.\n- Then a blank line.\n- Then 1–3 bullet points explaining key changes (each starting with "- ").\n- No code fences.\n- Do not mention "staged" or "diff".\n\nHere is the output of `git diff --cached`:\n\n'

prompt="$(printf '%s%s\n' "$prompt_header" "$staged_diff")"

# Ask Codex, with low reasoning effort
commit_msg="$(codex exec -m gpt-5.1-codex-mini "$prompt")"

# Sanity check: non-empty after stripping whitespace
if [ -z "$(printf '%s' "$commit_msg" | tr -d ' \t\r\n')" ]; then
  echo "Codex returned an empty commit message." >&2
  exit 1
fi

# Compose final buffer: header with staged files + AI message
tmpfile="$(mktemp)"
trap 'rm -f "$tmpfile"' EXIT

{
  printf '%s\n' "$commit_msg"
  printf '\n'
  printf '# Staged files:\n'
  printf '#   %s\n' $staged_files
} > "$tmpfile"

# Decide editor
editor="${GIT_EDITOR:-${VISUAL:-${EDITOR:-nvim}}}"

# Let you edit the full buffer
nvim -c "set ft=gitcommit" "$tmpfile"

# Strip comment lines starting with '#'
final_msg="$(grep -v '^#' "$tmpfile")"

# Abort if message is empty
if [ -z "$(printf '%s' "$final_msg" | tr -d ' \t\r\n')" ]; then
  echo "Aborting commit: commit message is empty." >&2
  exit 1
fi

# Commit using the cleaned content
printf '%s\n' "$final_msg" > "$tmpfile"
git commit -F "$tmpfile"

The script refuses to run without staged changes, builds a short prompt from your diff, lets you edit the AI draft with your git editor, strips comments, and completes the commit.

Bind it inside Lazygit

Add a custom command so pressing Shift+C in the files context fires the helper.

customCommands:
  - key: 'C'
    context: 'files'
    command: '~/.local/bin/codex-git-commit.sh'
    description: 'AI commit (Codex, staged files)'
    output: 'terminal'
    after:
      checkForConflicts: true

Save the config where Lazygit reads it by default:

Linux: ~/.config/lazygit/config.yml
MacOS: ~/Library/Application\ Support/lazygit/config.yml
Windows: %LOCALAPPDATA%\lazygit\config.yml (default location, but it will also be found in %APPDATA%\lazygit\config.yml

Use it now

Stage your changes, open Lazygit, focus the files panel, and hit Shift+C. Review or tweak the Codex draft in your editor, save, and let the script commit with the cleaned message.