Skip to content

Instantly share code, notes, and snippets.

@coccoinomane
Created February 23, 2026 13:06
Show Gist options
  • Select an option

  • Save coccoinomane/38f3fdf5f9793fa82090ff55c090eca2 to your computer and use it in GitHub Desktop.

Select an option

Save coccoinomane/38f3fdf5f9793fa82090ff55c090eca2 to your computer and use it in GitHub Desktop.
claude-commit: Use Claude Code to generate git commit messages, then review in your editor before committing
#!/usr/bin/env bash
#
# claude-commit: Use Claude Code to generate a commit message for staged changes,
# then open it in the editor for review before committing.
#
# Usage: claude-commit [custom instruction]
#
# Examples:
# claude-commit # Generate a standard commit message
# claude-commit "detailed description" # Add custom instructions for Claude
#
# Prerequisites: Claude Code CLI (https://claude.com/product/claude-code)
claude-commit() {
if ! command -v claude &> /dev/null; then
echo "Claude Code CLI is not installed. Get it at https://claude.com/product/claude-code"
return 1
fi
if git diff --cached --quiet; then
echo "No staged changes to commit."
return 1
fi
local max_subject_length=72
local user_instruction=""
if [[ -n "$*" ]]; then
user_instruction="Additional instruction from the user: $*"
fi
local msg_file
msg_file=$(mktemp "${TMPDIR:-/tmp}/claude-commit.XXXXXX")
echo "Generating commit message with Claude..."
if ! git diff --cached | claude -p \
"Generate a git commit message for these staged changes. Output ONLY the commit message, nothing else. Rules: use conventional commit format (e.g. feat:, fix:, refactor:). The first line MUST be at most ${max_subject_length} characters including the prefix — this is a hard limit, truncate or rephrase if needed. Optionally follow with a blank line and a longer description. ${user_instruction}" \
> "$msg_file"; then
echo "Failed to generate commit message."
rm -f "$msg_file"
return 2
fi
git commit --edit --file="$msg_file"
local exit_code=$?
rm -f "$msg_file"
return $exit_code
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment