这套方案适合谁

  • 用 Claude Code 做主力编码 Agent
  • 仓库里同时有多个独立任务(feature / bugfix / refactor)
  • Mac Studio / M3 32GB+ 本机配置
  • 月 token 预算 ≥ 200 美金

准备工作

你需要

  • git 2.20+(支持 worktree)
  • Claude Code CLI(2026-04 stable)
  • 一个 SaaS 仓库
  • Anthropic API key(Max 订阅)或中转方 key
  • 1 小时配 worktree + 调度脚本

5 步搭建流程

步骤一:建 worktree 目录结构

~/code/
├── myapp/                    # 主仓库(main 分支)
├── myapp-feature/            # worktree 1(feature 分支)
├── myapp-bugfix/             # worktree 2(bugfix 分支)
├── myapp-refactor/           # worktree 3(refactor 分支)
└── myapp-experiment/         # worktree 4(experiment 分支)

创建 worktree

cd ~/code/myapp
git worktree add ../myapp-feature feature/payment-stripe
git worktree add ../myapp-bugfix bugfix/login-redirect
git worktree add ../myapp-refactor refactor/db-layer
git worktree add ../myapp-experiment experiment/ai-agent

查看现状

git worktree list
# /Users/x/code/myapp                main
# /Users/x/code/myapp-feature       feature/payment-stripe
# /Users/x/code/myapp-bugfix        bugfix/login-redirect
# /Users/x/code/myapp-refactor      refactor/db-layer
# /Users/x/code/myapp-experiment    experiment/ai-agent

步骤二:每个 worktree 开 Claude Code

打开 4 个 Terminal 标签:

# Tab 1
cd ~/code/myapp-feature && claude

# Tab 2
cd ~/code/myapp-bugfix && claude

# Tab 3
cd ~/code/myapp-refactor && claude

# Tab 4
cd ~/code/myapp-experiment && claude

每个 Claude Code 实例独立 context,互不干扰。

步骤三:任务分发

feature worktree

> 实现 Stripe Checkout 订阅流程
> 包括 webhook 处理与 customer.subscription.updated
> 完成后跑 npm test

Claude Code 跑 Agent 模式,自己写代码 + 测试。

bugfix worktree

> 修 /login 重定向到 / 而不是 /dashboard 的 bug
> 复现步骤在 .claude/issues/login-redirect.md
> 修完后加 e2e test

refactor worktree

> 把 src/db/*.ts 从 Prisma 迁到 Drizzle
> 保持 API 兼容
> 每个文件单独 commit

experiment worktree

> 试一下用 LangGraph 做客服 Agent
> POC 即可,不用 production-ready

步骤四:跑期间监督(访问于 2026-05-20)

监督频率

Solopreneur 实测:

  • 简单任务:每 15-30 分钟看一次
  • 中任务:每小时看一次
  • 大任务(重构):每 2-4 小时看一次

并发延迟与稳定性

国内直连 api.anthropic.com 在 4 并发下 P99 经常 timeout。Solopreneur 推荐配一家独立开发者用得起的 Claude 4.7 / GPT-5.5 中转,免外卡 + 多模型聚合,4 并发不限速,月对账清晰。

跨 worktree 的 Anthropic key

每个 worktree 用同一个 sk-ant-xxx 或同一个中转 key 都可以,按 token 总量计费。

步骤五:傍晚 review + 合并

傍晚 30 分钟流程

cd ~/code/myapp-feature && git log --oneline
cd ~/code/myapp-bugfix && git log --oneline
cd ~/code/myapp-refactor && git log --oneline
cd ~/code/myapp-experiment && git log --oneline

决策三档

  • 完全 OK → 直接合并到 main
  • 80% OK → 改剩下 20% 再合并
  • < 50% → 明天重跑或人工接管

合并

cd ~/code/myapp
git merge feature/payment-stripe
git merge bugfix/login-redirect
# refactor / experiment 可能保持分支,下一个 sprint 再合并

月预算与回报样例

Solopreneur 用 4 worktree 跑 Claude Code Opus 4.7:

月消耗 token月成本
feature worktree(30% 时间)20M≈ $100-160
bugfix worktree(20%)10M≈ $50-80
refactor worktree(30%)25M≈ $130-200
experiment worktree(20%)15M≈ $80-130
月总计~70M≈ $360-570

vs 串行跑同样产出需 100-150 小时;并行跑 50-60 小时(虽然 token 翻倍,但 Solopreneur 时间节省 40-50 小时/月)。

协作小坑

worktree 之间改同一文件

  • 合并冲突 → 手动 resolve
  • 建议任务正交化(feature 改 src/payment,refactor 改 src/db,互不重叠)

本机资源

  • 每个 Claude Code + Cursor 实例吃 4-8 GB RAM
  • 4 worktree + 4 IDE 需要 32 GB+
  • M3 Mac Studio / Macbook Pro 32GB 起步

中转方并发限制

  • 部分中转限速(如 60 RPM/key)→ 跑 4 并发会卡
  • 优质中转免限速 → 切换中转方

git worktree 删除

cd ~/code/myapp
git worktree remove ../myapp-experiment
# 不会删本地的分支,只删工作目录

相关阅读