Last active
May 6, 2026 11:23
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 [-f] [custom instruction] | |
| # | |
| # Original Gist: https://gist.github.com/coccoinomane/38f3fdf5f9793fa82090ff55c090eca2 | |
| # | |
| # Examples: | |
| # claude-commit # Generate a standard commit message | |
| # claude-commit "detailed description" # Add custom instructions for Claude | |
| # claude-commit -f # Commit directly without opening the editor | |
| # claude-commit -f "detailed desc" # Force commit with custom instructions | |
| # | |
| # 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 force=0 | |
| if [[ "$1" == "-f" || "$1" == "--force" ]]; then | |
| force=1 | |
| shift | |
| 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 --model haiku -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 | |
| if [[ $force -eq 1 ]]; then | |
| git commit --file="$msg_file" | |
| else | |
| git commit --edit --file="$msg_file" | |
| fi | |
| 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