Claude Code Git Workflow

Use Claude Code for git operations: generate commit messages, review diffs, write PR descriptions, resolve merge conflicts, and automate git workflows.

💥 50p impulse-buy: Power Prompts PDF (first 10 buyers) 30 battle-tested Claude Code prompts · 8-page PDF · paste into CLAUDE.md and never re-type a prompt again · 50p impulse-buy, no commitment

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.

Generate commit messages

# 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"

Review before committing

# 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

Create a pull request

# 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"

Pipe git output for summaries

# 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?"

Resolve merge conflicts

# 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"

Git history exploration

# "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.

Pre-commit hook via Claude Code hooks

// .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.

Frequently asked questions

How do I use Claude Code to write commit messages?
Ask Claude Code to 'write a commit message for these changes' or simply run /review before committing. Claude reads git diff and produces a concise, conventional-commit-style message. You can then copy it or ask Claude to run git commit directly.
Can Claude Code create pull requests?
Yes. Ask 'create a PR for this branch' — Claude Code runs gh pr create (GitHub CLI) with a generated title, description, and test plan. Make sure gh is authenticated first.
How do I have Claude Code resolve merge conflicts?
After git merge or git rebase produces conflicts, ask Claude Code 'resolve the merge conflicts in X file' or 'resolve all conflicts'. Claude reads both sides plus the base, then writes the merged version.
Can I pipe git diff to Claude Code?
Yes: git diff HEAD~3 | claude --print 'Summarize these changes for a changelog'. The --print flag lets you pipe stdin and get a non-interactive response.
How do I stop Claude Code from committing without asking?
Add Bash(git commit*) to the deny list in .claude/settings.json, or add a PreToolUse hook that prompts for confirmation before git commit runs.

Free tools

Cost Calculator → API Cookbook → Diff Summarizer → Skills Browser →

More examples

Claude API Python QuickstartClaude API Node.js / TypeScript QuickstartClaude API Streaming in PythonClaude API Streaming in Node.js / TypeScriptClaude API Tool Use in PythonClaude API Tool Use in Node.js / TypeScript