October 23, 2026

適用於 AI 程式開發代理的 Git Worktree

git worktree 是你儲存庫的第二份 checkout,位於自己的分支上,並共用同一份歷史。為每個 Claude Code 或 Codex 代理各配一個 worktree,平行執行的代理就無法覆寫彼此的成果。本文說明如何建立 worktree、在其中執行代理、合併結果並清理,以及 Chief 如何替你讓平行代理互不干擾。

什麼是 git worktree?

git worktree 是附加在同一個儲存庫上的額外工作目錄。每個 worktree 都有自己的檔案和自己 checkout 的分支,但所有 worktree 共用同一份 .git 歷史、同一組 remote 和同一個物件儲存區。在任何一個 worktree 中建立的 commit,都能立即從其他所有 worktree 看到。

你能得到把儲存庫 clone 好幾次的效果,卻不需要額外的 clone:建立一個 worktree 只需幾秒,只會複製 checkout 出來的檔案,也不需要再執行一次 git fetch。

切換分支第二份 cloneWorktree
磁碟上有獨立的檔案否是是
共用歷史與 remote是否,需另外 fetch是
可同時開啟多個分支否是是
建立時間即時一次完整 clone幾秒

為什麼 AI 程式開發代理需要 worktree

一個代理在一個資料夾裡沒有問題。兩個代理在同一個資料夾裡,就會共用每一個檔案。當一個代理重構到一半時,另一個代理可能會重新格式化同一個檔案、安裝相依套件,或對改到一半的程式碼執行測試。兩個代理都不知道對方的存在。

worktree 讓每個代理都有自己的一份檔案和自己的分支。代理並肩工作,每個 commit 都留在各自的分支上,等你合併時再一次一個分支地整合結果。

如何使用 git worktree:五個指令

這些指令適用於任何代理或編輯器,手動設定時只需要這些。

在新分支上建立 worktree:

git worktree add ../myapp-invoices -b invoices-api

這會建立 ../myapp-invoices 資料夾,並 checkout 從你目前 commit 開始的 invoices-api 分支。若要從其他位置建立分支,請加上起點,例如 git worktree add ../myapp-invoices -b invoices-api main。

為已存在的分支建立 worktree:

git worktree add ../myapp-bugfix fix-login-redirect

列出所有 worktree:

git worktree list

完成後移除 worktree:

git worktree remove ../myapp-invoices

如果 worktree 有未 commit 的變更,Git 會拒絕移除。請先 commit 或捨棄這些變更,或在確定後加上 --force。

整理手動刪除資料夾後留下的紀錄:

git worktree prune

有一條規則要知道:同一個分支同一時間只能在一個 worktree 中 checkout。 如果 git worktree add 顯示某個分支已被 checkout,請改為新的 worktree 建立新分支。

Claude Code worktree:逐步說明

Claude Code 會替你建立並清理 worktree。

1. 在新的 worktree 中啟動工作階段。 在儲存庫根目錄執行:

claude --worktree invoices-api

Claude Code 會在名為 worktree-invoices-api 的新分支上建立 .claude/worktrees/invoices-api/,並在其中啟動工作階段。在另一個終端機以不同名稱再次執行這個指令,就能啟動第二個代理。-w 是簡寫,省略名稱則會自動產生一個。

2. 讓 worktree 不出現在 git status 中。 將資料夾加入 .gitignore,只需一次:

echo ".claude/worktrees/" >> .gitignore

3. 將本機設定複製到每個 worktree。 新的 worktree 只包含受追蹤的檔案,因此 .env 和其他被 gitignore 的檔案都不會出現。請在儲存庫根目錄的 .worktreeinclude 檔案中,以 .gitignore 語法列出它們:

.env
.env.local

Claude Code 會將所有符合的 gitignore 檔案複製到它建立的每個 worktree 中。

4. 選擇起點。 預設情況下,新的 worktree 會從 remote 上儲存庫的預設分支(通常是 main)建立分支。若要改從你目前的本機工作建立分支,請在 .claude/settings.json 中設定:

{
  "worktree": { "baseRef": "head" }
}

若要從 pull request 開始,請傳入它的編號:claude --worktree "#1234"。

5. 讓 Claude Code 清理。 結束時,若是未命名工作階段的乾淨 worktree,Claude Code 會連同其分支一併移除。如果 worktree 有變更或新的 commit,它會詢問要保留還是移除。保留的 worktree 可以用 claude --worktree <name> --resume 繼續,Claude Code 結束時會顯示這個指令。

以 claude --bg "<task>" 啟動的背景工作階段,會在編輯檔案前自動移到自己的 worktree 中;frontmatter 中設有 isolation: worktree 的子代理也會各自取得一個。兩者都在〈如何平行執行多個 Claude Code 代理〉中說明。

Codex worktree:逐步說明

Codex 會在你啟動它的目錄中執行,因此請用 git 建立 worktree,再於每個 worktree 中各啟動一個 Codex 工作階段:

git worktree add ../myapp-search -b search-filters
git worktree add ../myapp-emails -b email-templates

cd ../myapp-search && codex
# in a second terminal
cd ../myapp-emails && codex

若要無人值守地執行,請使用 codex exec 並以任務作為提示,每個 worktree 一個:

(cd ../myapp-search && codex exec "add date filters to the search page, with tests") &
(cd ../myapp-emails && codex exec "move the email templates to the new layout") &
wait

同樣的模式也適用於 Claude Code、Gemini CLI 或任何其他終端機代理,也能混合使用:一個 worktree 用 Claude Code,下一個用 Codex。

讓每個 worktree 都能執行

worktree 共用的是歷史,而不是建置環境。每個 worktree 都要:

  • 安裝相依套件。 node_modules、虛擬環境和建置輸出都位於工作目錄中。請在每個 worktree 中執行你的設定指令,或請代理把它當成第一步。
  • 使用不同的連接埠。 兩個開發伺服器無法同時使用 3000 埠。請在每個代理的任務中指定一個連接埠。
  • 分開本機資料庫。 多個代理對同一個共用資料庫執行 migration 可能會衝突。請為每個 worktree 指定自己的資料庫名稱,或將 migration 限定在單一任務中。
  • 檢查磁碟空間。 每個 worktree 是一份 checkout 出來的檔案,加上設定過程安裝的內容。git 歷史不會被複製。

合併結果

每個代理都在自己的分支上完成工作。請一次一個地審查並合併:

git log --oneline main..invoices-api
git diff main...invoices-api
git switch main && git merge invoices-api

每次合併後都執行測試。逐一分支合併能讓衝突保持小規模且容易定位;如果後面的分支需要更新,請在它自己的 worktree 中將它 rebase 到新的 main 上。

清理 worktree

git worktree list
git worktree remove ../myapp-invoices
git branch -d invoices-api
git worktree prune

git branch -d 只會在分支合併後才刪除它,因此適合作為安全的最後一步。如果 git 因為 worktree 被鎖定而拒絕移除,請先執行 git worktree unlock <path>。

Chief 如何讓平行代理互不干擾

worktree 在 checkout 階段解決隔離問題。Chief 則更早一步,在規劃階段處理,所以你不必為每個代理建立、追蹤或清理 worktree。

  • Chief 規劃哪些工作可以一起執行。 你設定目標,Chief 會將它拆解成里程碑,每個里程碑都有完成標準以及它所依賴的里程碑。大多會編輯相同檔案的工作會被串接或合併成一個里程碑,而型別、介面和 migration 等共用部分會優先處理。
  • 獨立的里程碑平行執行。 Chief 會同時執行所有依賴已完成的里程碑,透過你電腦上的 Chief 中繼,在你的專案資料夾中,使用你自己的 Claude Code、Codex 和其他代理訂閱。
  • 每項變更都可追溯。 每個代理只會 commit 它為任務變更的檔案,並在 commit 訊息中註明任務,讓你能看出每項變更是由哪個里程碑產生的。
  • 每個結果都經過審查。 Chief 會依照完成標準檢查每個完成的里程碑,並將只有你能做的決定集中在一個佇列中。

當你在 Chief 旁邊手動工作時,worktree 很適合作為你自己工作階段的空間:你的編輯會留在你的分支上,而 Chief 的代理則在專案資料夾中工作。

請參閱〈Chief 與 Codex〉和〈Chief 與 Claude Code〉了解 Chief 如何與兩者搭配,或參閱〈多代理協作指南〉了解更全面的介紹。

常見問題

git worktree 和 git clone 有什麼不同?

clone 是一個獨立的儲存庫,有自己的歷史、remote 和物件,需要透過網路 fetch。worktree 是同一個儲存庫的另一個工作目錄:它共用歷史與 remote,幾秒就能建立,也不需要 fetch。在一個 worktree 中的 commit,會立即在其他所有 worktree 中看到。

git worktree 應該放在哪裡?

放在主要 checkout 受追蹤檔案以外的任何地方都可以。像 ../myapp-feature 這樣的同層資料夾能讓它們保持分開。Claude Code 會將 worktree 放在儲存庫內的 .claude/worktrees/ 中,因此請將該資料夾加入 .gitignore。

worktree 會複製我的 .env 檔案嗎?

不會。新的 worktree 只包含受追蹤的檔案。請自行複製被 gitignore 的檔案,或將它們列在 .worktreeinclude 檔案中,Claude Code 就會將它們複製到它建立的每個 worktree 中。

兩個 worktree 可以使用同一個分支嗎?

不行。Git 只允許同一個分支同一時間在一個 worktree 中 checkout。請為每個 worktree 建立新分支,這本來就是平行代理所需要的做法。

Chief 會使用 git worktree 嗎?

不會。Chief 在你的專案資料夾中執行代理,並在規劃時讓平行的里程碑互不干擾:會編輯相同檔案的工作會被串接或合併,獨立的工作則同時執行。你仍然可以在 Chief 旁邊為自己的工作階段使用 worktree。

More articles

AI Agent Orchestration Guide

What AI agent orchestration is, how multi-agent orchestration works for Claude Code and Codex, and a comparison of orchestration approaches. Free guide.

What Is Agentic Engineering?

Agentic engineering means directing AI coding agents that write and test code while you set goals and review results. How it differs from agentic coding.

AI Code Review at Scale

What AI code review is, the main types of AI code review tools, and five workflows for reviewing AI-generated code from several coding agents at once.

Claude Code and Codex Pricing

Claude Code pricing from $20/mo (Pro) to $200/mo (Max 20x), Codex pricing from ChatGPT Free to Pro, usage limits and a comparison table, checked Oct 2026.

From Single Agent to Multi-Agent

Already using one AI coding agent? Here's how to scale to multiple instances running in parallel — the concurrency patterns, worktree isolation, and task decomposition strategies that make it work.

Vibe Coding at Scale

Vibe coding with one agent is fun. Vibe coding with five is chaos — unless you have orchestration. Here's how to scale the vibe without losing the quality.

Why AI Agents Need an Orchestration Layer

Running AI coding agents in terminal tabs works for two. Five signs you've outgrown it, tabs vs an orchestration layer, and what to look for in one.

Multi-Provider AI Orchestration

Claude Code, Codex, Cursor, Aider — each excels at different work. Here's how to combine providers into one coordinated workflow and why cross-provider review catches bugs single-provider setups miss.

Use Chief From Your AI Assistant

Add Chief's remote MCP server to Claude, Claude Code or Codex. Ask what's waiting on you, catch up on what shipped and propose milestones from chat.

Claude Code vs Codex: When to Use Each

Codex vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Codex isn't either-or; Chief runs both on your projects.

Claude Code vs Cursor: When to Use Each

Cursor vs Claude Code: when each fits, a comparison table and FAQ. Claude Code vs Cursor isn't either-or; keep Cursor and let Chief run Claude Code.

How to Run Multiple Claude Code Agents in Parallel

Run multiple Claude Code agents in parallel, step by step: worktrees, background agents, subagents and review. Then let Chief orchestrate them for you.

How to Run Claude Code From Your Phone

Run Claude Code from your phone: Remote Control, cloud sessions in the Claude mobile app, or SSH and tmux, step by step. Plus approving agents in Discord.

The Best AI Coding Agents in 2026

The best AI coding agents of 2026: Claude Code, Codex, Cursor, Copilot and more agentic AI coding tools compared, and how to run several with Chief.

Subagents vs Parallel Agents

What is a subagent? How Claude Code subagents and Codex subagents work, how they differ from parallel agents, and when to use each one.

How to Install and Set Up Codex CLI

Install Codex CLI on macOS, Linux or Windows, sign in, run your first session and update it. Every command, then run Codex alongside Claude Code in Chief.

Ready to manage your agents?

Chief 平行執行你的 Claude Code 和 Codex 代理,審查每個結果,並集中只有你能做的決定。