Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Claude Code Git Workflow

Claude Code Git Workflow

Claude Code is deeply integrated with Git, allowing you to perform almost all Git operations in natural language, including creating commits, managing branches, resolving merge conflicts, and using Git Worktrees for parallel workflows. This chapter explains in detail how to use Git efficiently in Claude Code.


Basic Git Operations

1. View changes

Just ask in natural language to get the Git status:

我改了哪些文件?

这次的改动有哪些内容?

最近 10 次提交都改了什么?

检查一下当前的 git 状态

Claude will automatically run the appropriate Git commands and show the results.

2. Commit code

Simple commit:

提交这次的改动

把这次的修改提交

Commit with a description:

把这次的修改提交,commit 信息说明修复了登录验证的 bug

Claude will automatically generate a compliant commit message based on the actual changes.

3. Branch management

新建一个 feature/user-profile 分支

切换到 develop 分支

把 main 分支的最新改动合并进来

查看所有分支

删除已合并的分支


Handling Merge Conflicts

1. Automatically Resolve Simple Conflicts

帮我解决合并冲突

这个文件有冲突,帮我看看哪个版本是正确的

Claude will analyze the conflicts and choose the appropriate version or propose a solution based on the project context.

2. Manually Resolve Complex Conflicts

When a conflict is too complex, Claude will explain the pros and cons of each option:

这个冲突涉及架构设计,你来决定保留哪个方案:

A. 保留我们的实现...

B. 保留引入的优化...

Before making any file modifications, Claude Code will show the changes and ask for your confirmation. For high-risk operations like merge conflicts, review each change even more carefully.


Git Worktree Parallel Workflow

Git Worktree lets you work on different branches in different directories at the same time, without switching branches or cloning the repository. This is one of the most powerful parallel working methods in Claude Code.

1. Create a Worktree with Claude

Specify a worktree when starting Claude:

claude --worktree feature-auth

This automatically:

Open multiple worktrees at the same time:

claude --worktree bugfix-123

claude --worktree

If no name is specified, Claude will automatically generate a fun name (e.g. bright-running-fox)。

2. Worktree Properties

property Value
Position /.claude/worktrees/
Branch Naming worktree-
Base Branch Remote origin/HEAD Branch Pointed To

3. Worktree Lifecycle

Scenario Behavior
No changes were made The worktree and branch are automatically deleted
Changes or commits exist Claude prompts you to choose whether to keep or delete
An orphaned worktree caused by a Claude crash exceeds the cleanupPeriodDays configured number of days, it is automatically deleted

4. Copying the .gitignore File to the Worktree

Create in the project root directory .worktreeinclude file, specify the gitignore file to copy to the worktree:



.env

.env.local

config/secrets.json

Only files matching a certain pattern and that are ignored by gitignore will be copied.

5. Manually Managing Worktrees

If you need more control, you can manage them manually:



# Create worktree and specify branch

git worktree add ../project-feature-a -b feature-a



# Create a worktree from an existing branch

git worktree add ../project-bugfix bugfix-123



# Start Claude in worktree

cd ../project-feature-a && claude



# Clean up after completion

git worktree list

git worktree remove ../project-feature-a

It is recommended to include .claude/worktrees/ Add to .gitignore, to avoid worktree contents showing up as untracked files in the main repository.


Subagent Worktree Isolation

Subagents can use worktree isolation to achieve fully independent parallel work:

Method 1: Let Claude handle it automatically

让子代理使用 worktree 来并行处理这些任务

Method 2: Specify it in the subagent configuration

Example


name

:

experimental-refactor

description

:

Try refactoring approaches in an isolated worktree

isolation

:

worktree    

Run in a temporary worktree

tools

:

Read, Write, Edit, Bash


You can modify freely in the isolated environment without affecting the main branch.

After completion, summarize the changes and whether the approach succeeded.

Features of Subagent Worktrees


Pull Request Workflow

1. Steps to Create a PR

Step 1: Have Claude summarize the changes

总结一下我对认证模块做的改动

Step 2: Generate the PR

创建一个 PR

Step 3: Refine the PR description

在 PR 描述中补充更多关于安全改进的内容

2. PR Session Association

Use gh pr create When a PR is created, the Claude session is automatically linked to that PR:

3. Using the GitHub CLI

Claude knows how to use the gh CLI tool. If it is not installed gh, Claude can read and write the GitHub API, but with limited functionality.



# Operations Claude can perform

gh pr create --title "Fix login bug"

gh pr view --comments

gh pr diff

gh issue create --title "Bug report"


Session Management and Git Integration

1. Resuming a Previous Session

Command Function
claude --continue Resume the most recent conversation in the current directory
claude --resume Open the session picker or resume by name
claude --from-pr 123 Resume a session associated with a specific PR

2. Git Features of the Session Picker

Keyboard shortcut Function
B Filter sessions by the current git branch

3. Session Metadata Display

The session picker displays the following information:

4. Branching Sessions

Use /branch、/rewind or --fork-session Created sessions are grouped under the root session for easier management.


Plan Mode and Safe Analysis

Plan Mode uses read-only operations to safely analyze the codebase and does not perform any write operations.

Start Plan Mode

claude --permission-mode plan

Or run a one-time query in the session:

claude --permission-mode plan -p "分析认证系统并提出改进建议"

Configure the default mode in settings.json

Example

{

 

"permissions"

:

{

   

"defaultMode"

:

"plan"

 

}

}


Typical Workflow Examples

Workflow 1: Parallel Development of Multiple Tasks



# Scenario: Developing three features simultaneously, but not wanting to switch branches



> 启动三个 worktree,分别处理登录重构、支付集成和性能优化

> 在每个 worktree 中独立工作,完成后合并到主分支

Workflow 2: Bug Fixing Process



# Step 1: Describe the problem

> 用户反馈:用户登出后刷新页面仍然显示已登录



# Step 2: Create a fix branch

> 创建一个 bugfix/session-cookie 分支来修复这个问题



# Step 3: Analysis and Fix

> 先分析可能的原因,在 bugfix 分支中修复



# Step 4: Submit and create PR

> 提交修复并创建一个 PR

Workflow 3: Code Review



# Review specific file

> 帮我审查 src/payment/processor.ts,重点关注错误处理



# Review git changes

> 审查我这次的所有改动,看看有没有明显的问题



# Review in an independent branch

> 创建一个 worktree 来审查这个重构方案

Workflow 4: Feature Development and Verification



# Main session: implement new feature

> 实现用户资料编辑功能



# Sub-agent: Run tests in parallel

> 在子代理中运行所有测试,只返回失败的测试和根因



# Sub-agent: Check code style

> 使用子代理审查代码是否符合项目规范



# After completion: create PR

> 创建一个 PR 并添加详细的描述


Non-Git Version Control Systems

For SVN, Perforce, or Mercurial users, Claude Code supports extension via hooks:

Configure custom hooks

in .claude/settings.json Configure in :

Example

{

 

"hooks"

:

{

   

"WorktreeCreate"

:

"./scripts/create-worktree.sh"

,

   

"WorktreeRemove"

:

"./scripts/remove-worktree.sh"

 

}

}

These hooks replace the default git behavior. When using hook scripts, copy the local configuration files inside the script instead of using .worktreeinclude。


Anthropic Internal Team Best Practices


Common questions

Q: What is the difference between a worktree and a branch?

A worktree is an independent directory that lets you work in different directories at the same time; a branch is a different commit history in the same directory. Worktrees are more suitable for scenarios where you need to handle multiple complex tasks at once.

Q: Will a worktree created by Claude affect the main repository?

No. A worktree has an independent directory, and its contents do not affect the main repository's uncommitted changes.

Q: How do I make Claude work on a specific branch?

Switch to the branch first and then start Claude, or use claude --worktree to create a branch automatically.

Q: Will too many worktrees take up too much space?

Git worktrees share the repository history, and the new directory only contains the branch differences, so they take up very little space.

Q: When are subagent worktrees cleaned up?

On normal completion, Claude prompts you to choose whether to keep or delete. An orphaned worktree from a crash will, after exceeding the cleanupPeriodDays configured number of days, be automatically deleted.