Use Claude Code for git operations: generate commit messages, review diffs, write PR descriptions, resolve merge conflicts, and automate git workflows.
Claude Code integrates deeply with git. It reads your working tree state, understands diffs, and can drive the entire commit-to-PR workflow. Here are the patterns that work best.
# Stage your changes first, then ask Claude:
git add -p # interactive staging
# Then in Claude Code:
"Write a commit message for these staged changes"
# Claude runs git diff --cached, reads the diff,
# and produces a conventional commit message:
# feat(auth): add rate limiting to login endpoint
#
# Adds a sliding window rate limiter (10 req/min per IP) to POST /auth/login.
# Uses Redis for the counter store. Existing tests updated.
# To commit directly:
"Commit the staged changes with an appropriate message"
# Built-in review command:
/review
# Or ask specifically:
"Review the changes I'm about to commit — any bugs or issues?"
# Claude reads git diff HEAD (or --staged) and reports:
# - Logic errors
# - Missing error handling
# - Tests that should be updated
# - Security issues (hardcoded secrets, injection risks)
# - Style inconsistencies vs the rest of the file
# Prerequisites: gh CLI installed and authenticated (gh auth login)
# Ask Claude Code:
"Create a GitHub pull request for this branch"
# Claude will:
# 1. Run git log main..HEAD to understand the full set of commits
# 2. Read the changed files for context
# 3. Run gh pr create with a generated title, description, and test checklist
# 4. Return the PR URL
# To target a specific base branch:
"Create a PR against the staging branch"
# Summarize recent changes for a changelog:
git log --oneline v1.2.0..HEAD | claude --print "Summarize these commits as a changelog entry"
# Review a specific commit:
git show abc1234 | claude --print "Explain what this commit does and flag any risks"
# Generate release notes from all changes since last tag:
git diff $(git describe --tags --abbrev=0)..HEAD | claude --print "Write release notes for these changes. Group by: Features, Bug Fixes, Breaking Changes"
# Summarize a teammate's PR before reviewing it:
gh pr diff 142 | claude --print "Summarize this PR. What changed and why? Any concerns?"
# After a conflict:
git merge feature/new-auth
# CONFLICT (content): Merge conflict in src/auth/middleware.py
# In Claude Code:
"Resolve the merge conflict in src/auth/middleware.py"
# Claude reads the file with conflict markers, understands both sides,
# and writes the correct merged version. It explains the resolution:
# "Kept HEAD's rate limiting logic, merged in feature branch's
# JWT refresh handling — both changes are independent and compatible."
# For all conflicts at once:
"Resolve all merge conflicts in this repo"
# "Why does this line exist?" — git blame with explanation:
"Why was line 47 of src/cache.py written this way? Check git history."
# Claude runs git log -p src/cache.py, finds the relevant commit,
# reads the commit message and diff context, and explains the decision.
# Find when a bug was introduced:
"When was the bug introduced where the cache key uses user_id instead of session_id?"
# Claude uses git log -S to search for when that string was added.
// .claude/settings.json — auto-review before any git commit
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "if echo "$CLAUDE_TOOL_INPUT" | grep -q '"git commit"\|"git commit'; then git diff --cached --stat >&2; echo 'Staged changes shown above. Proceeding with commit.' >&2; fi"
}
]
}
]
}
}
For a complete reference of Claude Code's slash commands (including /review), see Claude Code slash commands. For automating git lint hooks, see Claude Code hooks tutorial.