Claude Code中英文系列教程:在同一个项目里面并行使用多个Claude Code

Run parallel Claude Code sessions with Git worktrees

使用 Git 工作树 并行 运行 Claude Code 会话

Suppose you need to work on multiple tasks simultaneously with complete code isolation between Claude Code instances.

假设你需要在 Claude Code 实例之间 完全隔离代码的情况下 同时处理多个任务。

1,Understand Git worktrees 理解 Git 工作树

Git worktrees allow you to check out multiple branches from the same repository into separate directories.

Each worktree has its own working directory with isolated files, while sharing the same Git history.

Git 工作树允许你从同一代码仓库中检出多个分支到不同的目录中。

每个工作树都有自己的工作目录和隔离的文件,同时共享相同的 Git 历史。

git worktree 能让你在同一个仓库里同时检出(checkout)多个不同的分支,每个分支都有自己独立的文件夹,里面的文件、修改、甚至构建产物都互不干扰。

编译/构建很慢的项目,stash 后切分支要重新 build。worktree 每个目录独立,构建缓存可以各自保留,切来切去不用反复重编译。

假设你现在在项目目录 my-project/ 里,当前在 feature/login 分支写代码。

语法:git worktree add <新文件夹路径> <分支名>

场景1:紧急修 bug(推荐写在项目同级目录,清晰)

git worktree add ../hotfix-urgent release/v15_3_0

会输出类似

Preparing worktree (new branch 'release/v15_3_0')

branch 'release/v15_3_0' set up to track 'origin/release/v15_3_0'.

场景2:快速新建分支做实验(不写分支名会自动创建同名分支)

git worktree add ../temp-experiment

场景3:review PR(别人提的 pr-456 分支)

git worktree add ../review-pr-456 pr-456

查看当前有哪些 worktree

git worktree list

用完后删除 worktree

git worktree remove ../hotfix-urgent

然后手动删掉文件夹

建 worktree 的路径最好放在项目外面(../xxx),不要建在项目里面(容易混乱)

2,Create a new worktree 创建新的工作树

Create a new worktree with a new branch

创建一个带有新分支的新工作树

git worktree add ../project-feature-a -b feature-a //从当前所在分支新建分支

Or create a worktree with an existing branch

创建一个现有分支的工作树

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

This creates a new directory with a separate working copy of your repository.

这会创建一个新目录,其中包含你仓库的独立工作副本。

3,Run Claude Code in each worktree

在每个工作区中运行 Claude 代码

Navigate to your worktree

cd ../project-feature-a

Run Claude Code in this isolated environment

claude

4,Run Claude in another worktree

在另一个工作区运行 Claude

cd ../project-bugfix

claude

5,Manage your worktrees 管理你的工作树

List all worktrees 列出所有工作树

git worktree list

Remove a worktree when done 完成后删除工作树

git worktree remove ../project-feature-a

Each worktree has its own independent file state, making it perfect for parallel Claude Code sessions

每个工作树都有自己独立的文件状态,非常适合并行 Claude Code 会话

Changes made in one worktree won't affect others, preventing Claude instances from interfering with each other

在一个工作树中做出的更改不会影响其他工作树,防止 Claude 实例相互干扰

All worktrees share the same Git history and remote connections

所有工作树共享相同的 Git 历史记录和远程连接

For long-running tasks, you can have Claude working in one worktree while you continue development in another

对于长时间运行的任务,你可以让 Claude 在一个工作区中运行,而你则在另一个工作区继续开发

Use descriptive directory names to easily identify which task each worktree is for

使用描述性的目录名称,以便轻松识别每个工作区是用于哪个任务

Remember to initialize your development environment in each new worktree according to your project's setup. Depending on your stack, this might include:

记得在每个新的工作区中根据你的项目设置初始化开发环境。根据你的技术栈,这可能包括:

JavaScript projects: Running dependency installation (npm install, yarn)

JavaScript 项目:运行依赖安装 ( npm install , yarn )

注意 yarn 是目前 JavaScript 项目中最常用的两种包管理器之一(另一个是 npm)

Python projects: Setting up virtual environments or installing with package managers

Python 项目:设置虚拟环境或使用包管理器安装

Other languages: Following your project's standard setup process

其他语言:遵循项目的标准设置流程

相关推荐
JMchen1231 天前
AI编程范式转移:深度解析人机协同编码的实战进阶与未来架构
人工智能·经验分享·python·深度学习·架构·pycharm·ai编程
HetFrame1 天前
大模型驱动的禅道任务自动化规划与创建
python·ai·自动化·大模型·ai编程·任务·禅道
Ashley_Amanda2 天前
主流AI编程工具深度解析
ai编程
GISer_Jing2 天前
构建高性能Markdown引擎开发计划
前端·aigc·ai编程
Horizon_Ruan2 天前
从零开始掌握AI:LLM、RAG到Agent的完整学习路线图
人工智能·学习·ai编程
Testopia2 天前
AI编程实例 -- 数据可视化实战教程
人工智能·信息可视化·ai编程
PorkCanteen2 天前
Cursor使用-从问题到解决方案(以及一些通用rules)
前端·ai·ai编程
yangminlei2 天前
从零构建一个基于 DeepSeek 的 AI 对话系统:Spring Boot + 前端实战指南
spring boot·ai编程
差不多程序员2 天前
AI应用-关于Function-Call和MCP的理解与应用
java·spring boot·ai编程