Claude Code + git: auto-generate commit messages, PR descriptions, and pre-commit reviews
I've been running Claude Code for a few months now. The .claude/ folder setup articles got a lot of attention — settings.json, CLAUDE.md, custom /commands.
But the thing that saved me the most time isn't any of those. It's wiring Claude Code into my git workflow.
Here's everything I've set up, with the exact commands.
1. Auto-generate commit messages
The most basic one. Instead of git commit -m "fix stuff", let Claude write the message from the diff:
# Add this to your .bashrc or .zshrc
alias gcm='git diff --cached | claude -p "Write a conventional commit message for this diff. Format: type(scope): description. Types: feat/fix/docs/refactor/test/chore. Be specific, max 72 chars." | git commit -F -'
Usage:
git add -p # stage what you want
gcm # Claude writes the commit message
Output looks like:
feat(auth): add rate limiting to login endpoint
Prevents brute force attacks by limiting to 5 attempts per IP per minute.
Returns 429 with Retry-After header on breach.
This is a real commit message. Not fix login bug.
2. Pre-commit code review hook
This is the one that catches things before they're committed:
# .git/hooks/pre-commit
#!/bin/bash
DIFF=$(git diff --cached --diff-filter=ACM)
if [ -z "$DIFF" ]; then
exit 0
fi
REVIEW=$(echo "$DIFF" | claude -p "You are a senior code reviewer. Review this diff for: security issues, obvious bugs, missing error handling, performance problems. If nothing critical, say LGTM. If issues found, list them briefly.")
echo "\n🤖 Claude pre-commit review:"
echo "$REVIEW"
# Block commit if Claude flags security issues
if echo "$REVIEW" | grep -qi "security\|vulnerability\|injection\|XSS"; then
echo "\n❌ Commit blocked: security issue detected. Fix and re-stage."
exit 1
fi
echo "\n✅ Proceeding with commit..."
exit 0
Make it executable:
chmod +x .git/hooks/pre-commit
This runs on every git commit. If Claude finds a security issue, the commit is blocked. Otherwise, you see the review and it proceeds.
3. PR description generator
Every PR I open now has a real description. Claude reads the branch diff against main:
# Add to .bashrc
alias pr-desc='git diff main...HEAD | claude -p "Write a GitHub PR description for this diff. Include: Summary (2-3 sentences), Changes Made (bullet list), Testing Done, and any Breaking Changes. Use markdown."'
Usage:
pr-desc | pbcopy # pipe to clipboard
# paste into GitHub PR description
Or with GitHub CLI:
pr-desc | gh pr create --title "$(git log --format=%s -1)" --body-file -
4. Wire it into your CLAUDE.md
So Claude Code knows your git conventions when you're working interactively:
# CLAUDE.md
## Git conventions
- Commit style: conventional commits (feat/fix/docs/refactor/test/chore)
- Branch naming: type/ticket-description (e.g. feat/PROJ-123-add-auth)
- PR descriptions: always include summary, changes, testing
- Never commit directly to main
- Always run tests before committing
## When I ask you to commit changes:
1. Review what's staged with `git diff --cached`
2. Write a conventional commit message
3. Check if tests should be run first
4. Confirm before committing
Now when you say "commit this" inside Claude Code, it follows your team's conventions automatically.
5. The full git alias setup
Here's my complete git + Claude alias block:
# ~/.bashrc or ~/.zshrc
# Commit with Claude message
alias gcm='git diff --cached | claude -p "Write a conventional commit message for this diff. Be specific, max 72 chars, format: type(scope): description" | git commit -F -'
# Review staged changes before committing
alias greview='git diff --cached | claude -p "Review this diff for bugs, security issues, and missing error handling. Be brief."'
# Summarize what changed in the last N commits
alias gsummary='git log --oneline -${1:-10} | claude -p "Summarize these commits as a brief changelog entry for a user-facing CHANGELOG.md"'
# Explain any commit
alias gexplain='git show $1 | claude -p "Explain what this commit does in plain English, as if explaining to a non-technical stakeholder"'
The API cost question
Every time you run claude -p "...", you're hitting the Claude API.
With the default Claude.ai setup, you're paying Anthropic rates. With a proxy API like SimplyLouie, you pay $2/month flat — unlimited calls.
For git workflows that run on every commit, the flat rate model makes more sense than per-token billing. A 20-file PR review could easily cost $0.05-$0.10 in API calls. If you're committing 10-20 times a day, that adds up.
The curl equivalent of everything above works with any OpenAI-compatible base URL:
export ANTHROPIC_BASE_URL=https://simplylouie.com/api
export ANTHROPIC_API_KEY=your_key_here
# Claude Code picks this up automatically
claude
What's next
This is part of an ongoing series on the .claude/ ecosystem:
- Claude Code agents: the .claude/ settings that actually make them faster
- CLAUDE.md: the project memory file that makes Claude Code 10x more useful
- Claude Code custom slash commands: the /commands directory you're probably not using
Next up: Claude Code + CI/CD (GitHub Actions, running Claude in your pipeline).
What's your git + AI setup? Drop it in the comments.
Top comments (0)