Create a git alias that uses Claude to generate commit messages:
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
git add .
git cmsgWith 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 flowClaude 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
- 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 CLI reference — documents the
-pheadless mode this alias uses






