Git Worktree 工作流
用
git worktree為每個 Claude Code session 開獨立工作目錄——同 repo、不同分支、不同檔案狀態互不干擾。是 Boris Cherny 列為「最大生產力提升」的具體實作。
為什麼不直接開多個 checkout
傳統做法是 git clone 多次或在同一目錄 git checkout 切分支:
| 做法 | 問題 |
|---|---|
多次 git clone | 浪費磁碟、每個都要重新 install dependency |
| 同目錄切 branch | 互相覆蓋、Claude 看到的檔案會在 session 之間被改 |
git worktree | 同 repo(共享 .git/物件)、不同 working dir、互不干擾 |
Boris 自己的話:
“Most of the Claude Code team prefers worktrees—it’s the reason we built native support into the Claude Desktop app.”
Boris 的 5-tab 設定
iTerm2 開 5 個 tab,每個對應一個 worktree,編號 1–5:
~/repo-1 $ # Tab 1: Working on feature
~/repo-2 $ # Tab 2: Running tests
~/repo-3 $ # Tab 3: Code review
~/repo-4 $ # Tab 4: Debugging
~/repo-5 $ # Tab 5: Documentation
加上 iTerm2 系統通知,當任一 Claude 需要輸入時自動提醒,避免「不停切窗檢查 → 反而比單實例慢」的反模式。
啟動方式(v2.1.29+)
CLI
# 基本用法
$ claude --worktree my_worktree
# 同時開 tmux session
$ claude --worktree my_worktree --tmux
# 命名讓 Claude 自己取
$ claude --worktreeDesktop App
Code tab 勾選 worktree checkbox 即可。
手動建立 worktree
$ git worktree add .claude/worktrees/my-worktree origin/main
$ cd .claude/worktrees/my-worktree && claude進階:命名 + shell alias
團隊有人用單字母 alias 一鍵跳:
alias za="cd ~/repo/.claude/worktrees/a"
alias zb="cd ~/repo/.claude/worktrees/b"
alias zc="cd ~/repo/.claude/worktrees/c"也有人開專用 worktree只做某類事——例如 “analysis” worktree 只用來讀 log 和跑 BigQuery,不寫 code。
跟 Subagent 的疊乘
v2.1.29+ 起,subagent 也能用 worktree 隔離:
# .claude/agents/worktree-worker.md
---
name: worktree-worker
model: haiku
isolation: worktree
---或在 prompt 直接要求:
> Migrate all sync IO to async. Batch up the changes,
and launch 10 parallel agents with worktree isolation.
→ 主 session 跑 5 個 worktree,每個 worktree 內派 10 個 worktree-isolated subagent = 50 個並行單元互不干擾。這是 batch 大規模遷移 的基礎。
非 Git 的版本控制怎麼辦
Mercurial / Perforce / SVN 用 WorktreeCreate / WorktreeRemove hooks:
{
"hooks": {
"WorktreeCreate": [
{ "command": "jj workspace add \"$(cat /dev/stdin | jq -r '.name')\"" }
],
"WorktreeRemove": [
{ "command": "jj workspace forget \"$(cat /dev/stdin | jq -r '.worktree_path')\"" }
]
}
}對 PAM 的應用
Tab 1 — feature/grading-formula-v2 ← 改 GradingService 公式
Tab 2 — feature/dotnet-tests ← 跑 xUnit / 改測試
Tab 3 — feature/ui-polish ← 改 React 前端
Tab 4 — hotfix/migration ← 處理 EF migration
Tab 5 — docs/release-notes ← 寫文件
5 個 worktree 互不干擾,Claude 在 Tab 1 改 .cs 不會影響 Tab 3 的前端 hot reload。
跟 並行 Claude 實例 的關係
| 機制 | 做什麼 |
|---|---|
| 本概念(Worktree 工作流) | 物理層:每個 session 有獨立檔案空間 |
| 並行 Claude 實例 | 認知層:同時跑多個 Claude(worktree 是其底層) |
並行多實例沒有 worktree = 你會被檔案衝突咬死。Worktree 是並行多實例能成立的前提。
反模式
- ❌ 每個 worktree 都做同類工作 → 改用 Sub-agent / Agent Teams
- ❌ worktree 開了不關 → 久了一堆殭屍 branch;定期
git worktree prune - ❌ 沒系統通知 → 切 5 個 tab 找誰需要回應 = 把效率吃光
相關概念
強連結(原文明確提及)
- Boris Cherny — 創用此模式
- Boris Cherny 13 條心法 — 第 1 條心法的底層
- 並行 Claude 實例 — 認知層的並行
- Sub-agent —
isolation: worktree是進階用法 - Claude Code —
--worktreeflag 的載體
推斷連結(LLM 認為相關,待確認)
- Agent Teams ?? — 同 session 內並行可疊乘
- batch 大規模遷移 ?? — 大規模遷移用 worktree 隔離
深入閱讀
- 原文:Claude Code 之父怎麼用?Boris Cherny 的 10 個進階技巧
- Boris’s Feb 20 thread: https://howborisusesclaudecode.com/ Part 4
- Git 官方文件:https://git-scm.com/docs/git-worktree
← 回到 wiki