• AI-Generated Git Commit Messages

    A person reviews a blank glowing card fed by branching copper rails in a warm workshop.

    Create a git alias that uses Claude to generate commit messages:

    bash
    git config --global alias.cmsg '!f(){ \
      set -euo pipefail; \
      if git diff --cached --quiet; then echo "Nothing staged. Run git add first." >&2; exit 1; fi; \
      tmp="$(mktemp -t gitmsg.XXXXXX)"; \
      trap "rm -f \"$tmp\"" EXIT; \
      ctx=""; \
      if [ $# -gt 0 ] && [ "${1#-}" = "$1" ]; then ctx="$1"; shift; fi; \
      prompt="Generate a clear, concise git commit message following conventional commits format. Include: type (feat/fix/docs/style/refactor/test/chore), scope, and description. Be specific about what changed and why. User passed the following info additionally to give you more understanding about the change: {CONTEXT}. Output ONLY the final commit message line(s), no quotes, no code fences."; \
      prompt="${prompt/\{CONTEXT\}/$ctx}"; \
      git diff --cached | claude -p "$prompt" >"$tmp"; \
      git commit -e -F "$tmp" "$@"; \
    }; f'

    Requires Bash or Zsh as your shell. The -p flag runs Claude non-interactively (its programmatic, headless mode), reading the staged diff from stdin.

    Usage

    bash
    git add .
    git cmsg

    With additional context:

    bash
    git cmsg "refactored auth module"

    The command opens the editor with a pre-generated message, allowing you to review and edit before committing.

    What You Get

    Instead of staring at a blank commit message, you get something like:

    feat(auth): add JWT token refresh mechanism
    
    - Implement automatic token refresh before expiration
    - Add refresh token rotation for security
    - Update auth middleware to handle refresh flow

    Claude analyzes your staged changes and generates a message that actually describes what happened.

    Tips for Better Results

    1. Stage related changes together — one logical change per commit helps Claude understand intent

    2. Pass context for non-obvious changesgit cmsg "performance optimization" gives Claude a hint

    3. Review before committing — the -e flag opens your editor, so you can tweak the message

    4. Smaller commits = better messages — 500-line commits confuse AI just like they confuse humans

    Why Not Just Use Claude Code Directly?

    You can! Claude Code has built-in commit support. But this alias:

    • Works outside Claude Code sessions
    • Pipes directly to your editor

    For quick commits in a terminal, the alias wins. For complex work where you’re already in Claude Code, use its native commit flow.

    See Also