選項

設定隔離的 git 工作樹。在開始獨立工作時使用,或當 ce-work/ce-code-review 提供工作樹選項時使用;首先檢測現有的隔離狀態。

...展開全部
11
更新時間 2026-08-26

關於 ce-worktree

工作樹隔離(Worktree Isolation)負責設定隔離的 Git 工作樹,以便在不干擾使用者主簽出(main checkout)的情況下繼續工作。由於大多數編碼環境(coding harnesses)在會話開始時已經建立了工作樹,該技能的主要任務是檢測現有隔離狀態,從而避免建立冗餘的工作樹。它遵循嚴格的操作順序:檢測現有隔離狀態,優先使用原生工作樹工具,僅在兩者均不適用時才回退到純 Git 命令。

檢測機制透過比較解析後的絕對 Git 目錄與解析後的絕對公共 Git 目錄來實現。由於 Git 會根據當前目錄混合使用絕對路徑和相對路徑形式,該技能會先將兩者解析為絕對路徑,而不是進行原始字串比較,否則會導致錯誤的“已隔離”結果。當這兩個路徑不同時,它透過檢查超級專案的工作樹來區分連結的工作樹與子模組,並在已處於隔離工作樹內部時就地工作。當存在原生工作樹原語(例如 EnterWorktree 工具、/worktree 命令或 --worktree 標誌)時,它會使用這些原語並停止操作,因為後臺靜默執行的 git worktree add 會建立編碼環境無法看到或清理的幻影狀態。

Git 回退機制從倉庫根目錄執行,選擇基於工作描述生成的有意義分支名稱,確保 .worktrees/ 被 gitignore(透過檢查帶有尾部斜槓的路徑以遵守僅針對目錄的規則),盡力執行非致命的基線分支 fetch 操作,在 .worktrees/ 下建立工作樹,並切換至其中。如果建立因沙箱或許可權錯誤而失敗,該技能將其視為阻塞性決策,並透過平臺的問題工具向使用者詢問,而不是在主簽出中靜默工作。它還記錄了其他工作樹操作(list、remove、switch),並整合到 ce-work 和 ce-code-review 流程中,後者將其作為工作樹選項提供。

常見問題解答

為什麼該技能在建立工作樹之前要檢查現有隔離狀態?

大多數編碼環境預設在會話開始時建立工作樹,因此常見情況是隔離狀態已經存在。從現有工作樹內部建立另一個工作樹會導致進入錯誤的工作樹,且對建立當前工作樹的編碼環境不可見。

它如何在檢測過程中避免錯誤的“已隔離”結果?

它首先將 git 目錄和公共 git 目錄解析為絕對路徑,然後進行比較,而不是進行原始字串比較。Git 會根據當前目錄返回混合的絕對和相對形式,否則會導致假陽性結果。

何時應使用原生工作樹工具而不是純 Git?

每當編碼環境提供原生原語(如 EnterWorktree/WorktreeCreate 工具、/worktree 命令或 --worktree 標誌)時,該技能會使用它並停止操作。原生工具負責放置、跟蹤和清理工作樹,以便編碼環境可以管理它;後臺靜默執行的 git worktree add 會建立編碼環境無法看到的幻影狀態。

如果 git worktree add 因許可權或沙箱錯誤而失敗,會發生什麼?

此失敗要求在接觸當前簽出之前進行阻塞性的使用者決策。該技能會報告錯誤並透過平臺的問題工具(例如 Claude Code 中的 AskUserQuestion)詢問使用者,提供諸如在當前簽出中工作或停止以解決問題的選項,僅在獲得明確確認後才會繼續在主簽出中工作。

該技能如何確保工作樹內容不會被提交?

在建立任何內容之前,它會透過執行帶有尾部斜槓的 git check-ignore 來驗證 .worktrees/ 是否被 gitignore,因此即使目錄尚不存在,現有的僅針對目錄的規則也會被遵守;如果需要,它還會將 .worktrees/ 行新增到 .gitignore 中。

在 GitHub 上查看

Ensure the current work happens in an isolated workspace, without disturbing the user's main checkout. Most coding harnesses now create a worktree by default at session start, so the common case is that isolation already exists.

Done when: the caller is working in an isolated tree — existing or newly created — and its path and branch have been reported, or a blocker has been reported instead.

Order of operations: detect existing isolation -> prefer a native worktree tool -> fall back to plain git. Never create a worktree the harness cannot see.

Two modes, set by the caller's need:

  • New work (default). No ref named — create a fresh branch from a base (trunk). This is what ce-work and ce-code-review use when the user picks the worktree option.
  • Isolate an existing ref. The caller names a PR head, branch, or commit — attach the worktree to that ref instead of creating a new branch. A branch can be checked out in only one worktree at a time. If the named ref is already checked out anywhere (most commonly as the primary checkout's current branch), do not create a second worktree — report that it is already checked out at <path> and let the caller act (work there in place; or, only if a clean separate tree is essential, create a detached worktree at the same commit).

Step 0: Detect existing isolation

Compare the resolved absolute git dir against the resolved absolute common git dir. Git mixes absolute and relative forms depending on the current directory (from a subdirectory of a normal checkout, --git-dir comes back absolute while --git-common-dir may be relative), so a raw string compare yields a false "already isolated":

git rev-parse --absolute-git-dir                     # absolute git dir for this worktree(cd "$(git rev-parse --git-common-dir)" && pwd -P)   # absolute shared (common) git dir

Equal -> normal checkout; continue to Step 1.

Different -> a linked worktree or a submodule. Distinguish with git rev-parse --show-superproject-working-tree:

  • Non-empty -> submodule; treat it as a normal checkout and continue to Step 1.
  • Empty -> already isolated. Report the worktree path (git rev-parse --show-toplevel) and current branch, then work in place — a worktree-from-worktree lands in the wrong tree and is invisible to the harness that made the current one. In isolate-an-existing-ref mode, check that ref out here (unless it is already current) rather than nesting a worktree.

Step 1: Prefer the harness's native worktree tool

If the harness provides a native worktree primitive — for example an EnterWorktree / WorktreeCreate tool, a /worktree command, or a --worktree flag — use it and stop. Native tools place, track, and clean up the worktree so the harness can manage it. A behind-the-back git worktree add creates phantom state the harness cannot see, navigate to, or clean up.

Step 2: Git fallback

Only when there is no native tool and Step 0 found no existing isolation.

  1. Run from the repo root: cd "$(git rev-parse --show-toplevel)". The paths below are repo-root-relative, but the skill runs from the user's current directory — without this, .worktrees/<branch> and the .gitignore edit land in a subdirectory (e.g. src/.worktrees/...).
  2. Choose a meaningful branch name from the work description (e.g. feat/login, fix/email-validation) — never an opaque auto-generated one. Base: origin's default branch, else main.
  3. Ensure .worktrees/ is gitignored before creating anything: git check-ignore -q .worktrees/ — with the trailing slash, so an existing directory-only .worktrees/ rule is honored even before the directory exists (without the slash the probe misses it and dirties a correctly-configured repo). Not ignored -> add a .worktrees/ line to .gitignore.
  4. Refresh the base with git fetch origin <from-branch>. This is non-fatal — no origin remote, a differently-named remote, or a local-only branch is not an abort; continue with the local ref.
  5. Create the worktree, per mode:
    • New work: git worktree add -b <branch-name> .worktrees/<branch-name> origin/<from-branch> (use the local <from-branch> ref if origin/<from-branch> does not exist).
    • Existing branch or tag: git worktree add .worktrees/<slug> <target-ref>.
    • PR: check it out on a local branch — git fetch origin pull/<n>/head:pr-<n> then git worktree add .worktrees/pr-<n> pr-<n>. Never a detached FETCH_HEAD: that orphans the fix loop's commits instead of updating the PR. (For push-tracking back to the PR, create it detached — git worktree add --detach .worktrees/pr-<n> — then cd in and run gh pr checkout <n>, which is fork-safe.)
    • If git reports the ref is already checked out elsewhere, apply the one-branch-one-worktree rule above — do not force a second worktree.
  6. cd into it, then report the path and branch.

If git worktree add fails with a sandbox or permission error, the requested isolation does not exist. Do not proceed in the current checkout — the user chose isolation specifically to avoid it. Report the failure and ask, offering options such as "work in the current checkout" vs "stop and resolve the permission issue", using the platform's blocking question tool: AskUserQuestion in Claude Code (call ToolSearch with select:AskUserQuestion first if its schema isn't loaded), request_user_input in Codex, ask_question in Antigravity CLI (agy), ask_user in Pi (via the pi-ask-user extension). Only when no blocking tool exists in the harness or the call errors, present the numbered options in chat and wait for the reply. Never skip the confirmation, and do not retry alternative paths automatically.

所有檔案

0 個檔案

安裝 ce-worktree

將技能檔案下載並解壓至 .claude/skills/ 目錄。

下載 ZIP

複製儲存庫並將技能檔案複製到您的專案中。

git clone https://github.com/EveryInc/compound-engineering-plugin/blob/main/skills/ce-worktree/SKILL.md # Copy SKILL.md to your .claude/skills/ directory

複製 複製
快速設定: 將技能資料夾複製到 .claude/skills/,Claude 將自動檢測並使用該技能

相關技能

github-project-management
更新時間 2026-06-29
using-git-worktrees
更新時間 2026-06-29
readme-blueprint-generator
更新時間 2026-07-05
finishing-a-development-branch
更新時間 2026-06-29
OR