Skip to content

Instantly share code, notes, and snippets.

@nthj
Created August 5, 2025 15:38
Show Gist options
  • Save nthj/6be0b19252a09c0e15867501a7aa919a to your computer and use it in GitHub Desktop.
Save nthj/6be0b19252a09c0e15867501a7aa919a to your computer and use it in GitHub Desktop.
claude-commit

Summarize staged changes into commit messages that follow house rules.

πŸ—οΈ Procedure

DEFAULT MODE: stage and commit

When we elect to only generate a mesage, disregard these steps:

  1. If no changes are staged, you may elect to stage some or all of the changes.
  2. You MUST actually run git commit with your proposed message.

pipe into git commit

When we ask to generate a message to pipe into git commit:

  • ❌ DO NOT stage changes.
  • ❌ DO NOT run git commit.
  • ❌ DO NOT output anything other than the commit message itself.

βœ… Format

This is loosely inspired by Rumelt's "Good Strategy, Bad Strategy."

  1. One-line summary

    • Prefer to use less than 80 characters
    • Present tense, imperative
    • Add user timezone support to delivery scheduler
    • ❌ Don't use past tense, vague titles, or chatty notes
  2. Optional body

    • Explain why this change happened
    • Keep this about the outcome
  3. Optional Appendix A

    • Explain course corrections or learnings while prompting
    • Explain how you would have one-shotted it if you had the original prompt to do over
  4. Optional Appendix B

    • Use # to link to issues or other PRs
    • Example: #1234 for an issue, #5678 for a PR
    • Include links to exceptions, tickets, or other material you were provided

🎨 Style

  1. Emojis are fun

    • Examples: πŸ› for bugs, ✨ for new features
  2. Use Markdown

    • Use headings, lists, and code blocks to organize content
    • Example: ## Changes for a section header
  3. Be concise

    • Keep messages short and to the point
    • Avoid unnecessary fluff or repetition

πŸ“ Example

Add user timezone support to delivery scheduler

## Changes

- Implemented timezone handling in delivery scheduling
- Updated database schema to store user timezones
- Added tests for timezone conversion logic

## Why

This change allows users to receive notifications at their local time, improving user experience and engagement.

## Appendix A
- Initially struggled with timezone conversion logic
- Would have researched timezone libraries earlier to avoid confusion

## Appendix B
- Related issue: #1234
- PR for review: #5678
#!/usr/bin/env bash
# ‼️ CHANGE THIS INSTALL PATH AS NEEDED:
CLAUDE="$HOME/.volta/bin/claude"
# Setup logging
LOG_DIR="$HOME/.logs/$(date +%Y-%m)"
LOG_FILE="$LOG_DIR/git-claude-commit.log"
mkdir -p "$LOG_DIR"
# Logging function (only to file, not stdout)
log() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" >> "$LOG_FILE"
}
# Check if we're in a git repository
if ! git rev-parse --git-dir > /dev/null 2>&1; then
log "Error: Not in a git repository"
echo "Error: Not in a git repository"
exit 1
fi
# Add all if -A flag is used
if [[ "$1" == "-A" ]]; then
log "Adding all changes to staging area"
git add -A
fi
# Check if there are any staged changes
if ! git diff --cached --quiet; then
log "Starting Claude commit generation..."
log "Repository: $(git rev-parse --show-toplevel)"
# Log staged files
STAGED_FILES=$(git diff --cached --name-only)
log "Staged files:"
echo "$STAGED_FILES" | sed 's/^/ /' >> "$LOG_FILE"
# Generate the prompt for Claude with instruction to only return message
PROMPT=$(echo -e "IMPORTANT: Output ONLY the commit message text. Do NOT run git commit. Do NOT include any explanation, markdown code blocks, or commentary. Just output the raw commit message that will be passed to git commit -F.\n\nIgnore the instruction in commit.md about running git commit - just generate the message.\n\n" && cat ~/.claude/commands/commit.md && echo -e "\nStaged files:" && git diff --cached --name-only)
# Create a temporary file for the prompt
TEMP_FILE=$(mktemp)
COMMIT_MSG_FILE=$(mktemp)
echo "$PROMPT" > "$TEMP_FILE"
# Run Claude to get commit message only
log "Running: claude < prompt"
gum spin --spinner dot --title "Claude is crafting your commit message..." -- bash -c "$CLAUDE < \"$TEMP_FILE\" > \"$COMMIT_MSG_FILE\" 2>> \"$LOG_FILE\""
# Log the generated message
log "Generated commit message:"
cat "$COMMIT_MSG_FILE" >> "$LOG_FILE"
# Use the generated message with git commit
git commit -F "$COMMIT_MSG_FILE"
# Clean up
rm -f "$TEMP_FILE" "$COMMIT_MSG_FILE"
# Log completion
log "Git commit completed"
else
log "No staged changes to commit"
echo "No staged changes to commit"
exit 1
fi

Setup

  • Add commit.md to ~/.claude/commands/commit.md
  • Run brew install gum (animated spinner, more broadly a neat tool to have around)
  • Place git-claude-commit somewhere in your $PATH
  • Change the install path for $CLAUDE in git-claude-commit
  • chmod +x ./git-claude-commit

Usage

  • git claude-commit: commit staged files
  • git claude-commit -A: commit all files
  • /commit within a Claude CLI session

Logs

If something goes wrong, check ~/.logs/<date>/git-claude-commit.log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment