Create a git alias that uses Claude to generate commit messages:
git config --global alias.cmsg '!f(){ \
set -euo pipefail; \
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}\n\nOutput ONLY the final commit message line(s), no quotes, no code fences."; \
prompt="${prompt/\{CONTEXT\}/$ctx}"; \
printf "%s" "$prompt" | claude >"$tmp"; \
git commit -e -F "$tmp" "$@"; \
}; f'
Usage
git add .
git cmsg
With additional context:
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
-
Stage related changes together — one logical change per commit helps Claude understand intent
-
Pass context for non-obvious changes —
git cmsg "performance optimization"gives Claude a hint -
Review before committing — the
-eflag opens your editor, so you can tweak the message -
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
- Runs faster (no agent startup)
- 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
- Conventional Commits — the format Claude follows
- Claude Code’s
/commitcommand — the built-in alternative