Coding Agent Git Worktree 隔离
Coding agent 使用 Git worktree 隔离多条修改线,让并行任务、多方案对比、大规模迁移互不污染。
#tech / ai
#type / concept
#status / growing
[!info] related notes
- 所属 MOC: Coding Agent MOC
- 并列概念: Coding Agent Subagents
- 关系笔记: Agent 工程纪律
Coding Agent Git Worktree 隔离
一句话定义
Git Worktree 隔离是 coding agent 用 Git worktree 为并行任务创建独立工作区的模式,每个 worktree 有自己的分支和文件状态,互不污染,适合同时试多个方案、大规模迁移和 subagent 并行修改。
为什么需要 Worktree
当 coding agent 同时做多件事时:
- 两个修复方案互相污染
- 多个 subagent 改同一个工作区会冲突
- 大规模迁移需要隔离每个子任务
- PR 分支修改不能影响主工作区
Git worktree 让每个任务有自己的独立文件系统副本。
各产品的实现
Codex
- Codex App 内置 worktree 支持
- 适合同时试多个方案,不污染主工作区
- Cloud 任务自动使用隔离环境
Claude Code
- CLI 参数:
claude -w feature-auth /batch:把大规模改动拆成 5–30 个 worktree subagents- Workflow 中
isolation: 'worktree'让 subagent 在独立 worktree 中工作 - Agent View 支持多个并行 session,每个可以有自己的 worktree
- worktree 在
.claude/worktrees/目录下
使用场景
| 场景 | 为什么用 worktree |
|---|---|
| 同时试两个修复方案 | 不互相污染 |
| 大规模迁移 | 每个子任务独立 PR |
| PR review 修复 | 在 PR 分支隔离工作 |
| Claude 并行 agent | 避免多个 agent 改同一工作区 |
/batch 大改 | 每个 worktree 一个子任务 |
最短记忆方式
Worktree = 用 Git worktree 为并行任务创建独立工作区,互不污染,适合多方案对比和大规模迁移。