Chapter 8 · Git Integration and Code Review
Chapter goals: Master Claude Code's deep integration with Git — auto-generating commit messages, branch operations, creating PRs via GitHub CLI, and leveraging Claude to assist code reviews.
8.1 Auto Commit Messages
One of Claude Code's most commonly used Git features is auto-generating meaningful commit messages based on staged changes:
# In interactive mode
> Help me commit these changes
# Claude will:
# 1. Run git diff --cached to view staged内容
# 2. Analyze the change semantics (new feature/bug fix/refactor)
# 3. Generate a message conforming to Conventional Commits
# 4. Execute git commitTypical output:
Claude detected modifications to 3 files:
src/auth/login.ts | 42 +++++---
src/auth/types.ts | 12 ++++
tests/auth.test.ts | 28 +++++++
Suggested commit message:
feat(auth): add OAuth2 login flow with refresh token
- Implement OAuth2 authorization code flow
- Add token refresh mechanism with auto-retry
- Update TypeScript interfaces for auth types
- Include unit tests for login and refresh paths
Confirm commit? (y/n)Conventional Commits Convention
Convent the commit format in CLAUDE.md; Claude will自动 follow:
## Git Conventions
- Use Conventional Commits: feat/fix/docs/refactor/test/chore
- scope uses module name: auth, api, ui
- description in English, first line under 72 chars8.2 Branch Operations
# Create a feature branch and switch to it
> Create a feature/user-profile branch
# Merge and handle conflicts
> Merge the latest main code into the current branch; help me resolve any conflicts
# How Claude handles conflicts:
# 1. git merge main → detects conflicts
# 2. Reads each conflicted file one by one
# 3. Analyzes the modification intent on both sides
# 4. Gives merge suggestions or resolves directly
# 5. Marks resolved and completes the merge commitRebase Operations
> Rebase my branch onto main
# Claude checks for unpushed commits,
# executes git pull --rebase origin main,
# and handles possible conflicts.Proceed with Caution on Dangerous Operations
Operations like git reset --hard, git push --force, and git rebase irreversibly修改 history. Claude Code默认 requires you to confirm these operations. It's recommended to add additional protection in settings.json:
{
"permissions": {
"deny": [
"Bash(git push --force:*)",
"Bash(git reset --hard:*)"
]
}
}8.3 Creating PRs with GitHub CLI
After installing the gh CLI, Claude can完成 a full PR workflow:
# Prerequisite: install and authenticate gh CLI
brew install gh
gh auth login
# Have Claude create a PR
> Push the current branch and create a PR to mainClaude's execution流程:
1. git push -u origin feature/user-profile
2. gh pr create --title "..." --body "..."
├── Title extracted from commit messages
└── Body auto-includes:
- Change summary
- Test status
- Related issue (if mentioned)
3. Returns the PR linkCustom PR Templates
<!-- Convent in CLAUDE.md -->
## PR Conventions
Use the following template structure when creating PRs:
### Changes / ### Tests / ### Screenshots (if UI changes) / ### Checklist8.4 Code Review Assistance
Claude Code is a powerful review assistant — it can读 context, not just diffs.
Reviewing Uncommitted Code
> Review my current changes, focusing on security issues and performance hazards
# Claude will:
# 1. git diff to view changes
# 2. Read relevant files for full context
# 3. Analyze from security, performance, and maintainability dimensions
# 4. Output findings sorted by severityOutput example:
📋 Code Review Results
🔴 High Risk
├── src/auth/login.ts:45 — Plaintext password stored in session
│ Suggestion: hash with bcrypt before storing
🟡 Medium
├── src/api/users.ts:78 — N+1 query problem
│ Suggestion: use join or eager loading
🟢 Suggestion
├── src/utils/format.ts:12 — Can be提取 as a shared functionReviewing Others' PRs
> Review the code quality of PR #42
# Claude获取s PR info via gh commands
gh pr view 42
gh pr diff 42
# Then analyzes file by file and gives review comments8.5 Diff Reading Tips
When Claude展示s大量 changes, learn to quickly locate关键信息:
# Only view statistics
> How many files did this branch modify?
# Focus on特定 type of changes
> List all newly added API endpoints
# Compare two versions
> Compare the差异 between v1.0 and v2.0 tags; summarize the main changesFor大型 diffs, having Claude先 give an overview before diving into details is more efficient than reading everything at once:
You: What did this PR change?
Claude: This PR包含 3 major changes:
1. New user preferences API (4 files)
2. Refactored database connection pool (2 files)
3. Fixed a timezone bug (1 file)
Which part should I dive deeper into?
You: Focus on the database connection pool refactor
Claude: [深入分析...]Chapter Summary
- Claude can auto-generate规范的 commit messages based on diffs;
- Branch operations (create/merge/rebase) and conflict resolution can be delegated to Claude;
- Paired with the
ghCLI, you can one-click push + create a PR; - Code reviews output in 🔴🟡🟢 three-tier format, covering security/performance/maintainability;
- For large diffs, getting an overview first then diving into specific areas is more efficient.
🧪 随堂测验
点击你认为正确的选项。答错时会展示正确答案与原因解析。
1. Before having Claude generate a commit message, what is the most critical prerequisite step?
2. Why is it recommended to deny git push --force in settings.json?
3. What prerequisites are needed to have Claude create a PR?
4. Facing a large diff touching 50 files, what is the most efficient review strategy?
🛠️ Hands-on Practice
- Make a小 change in your project and have Claude auto-generate a commit message; assess its quality.
- Install and authenticate the
ghCLI, then have Claude create a test PR for your project. - Find an open PR in a public repo and have Claude help review the code, outputting structured review comments.
After completing the exercises, proceed to Chapter 9: Custom Instructions and Configuration.