Community程式設計與開發github.com

使用 Git Worktrees

在開始需要與當前工作區隔離的新功能開發時,或執行實作計畫之前使用 - 透過原生工具或 git worktree 回退方式,確保存在一個隔離的工作區。

使用 Git Worktrees 是什麼?

此技能 (using-git-worktrees) 提供了一套明確的流程,用於在 AI 代理(例如 Claude Code、Cursor、Codex)進行開發工作前,建立一個隔離的工作區。其主要目的是防止對當前分支的意外修改,確保工作環境的乾淨與獨立性。該技能會優先檢測是否已存在隔離環境(如已連結的 worktree 或子模組),若無,則會依序嘗試使用平台的原生 worktree 工具(例如 `EnterWorktree` 命令)、或回退到傳統的 `git worktree add` 指令來手動建立。技能內建了詳細的安全性檢查,例如驗證 worktree 目錄是否已被 `.gitignore` 忽略,以避免將暫存檔案提交到倉庫。在建立隔離空間後,它會自動執行專案設置(如安裝依賴項),並運行測試以驗證基準線的乾淨狀態。透過一套嚴謹的檢測與回退機制,此技能確保開發者或 AI 代理在不同平台(如 Claude Code、Cursor、Codex)上都能以一致、安全且高效的方式啟動新的功能開發,並清楚記錄了常見的誤解與正確做法。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/obra/superpowers/tree/main/skills/using-git-worktrees

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

使用 Git Worktrees 是做什麼的?

概述

確保工作在隔離的工作區中進行。優先使用你平台的原生 worktree 工具。僅在沒有原生工具可用時,才回退到手動的 git worktree。

核心原則: 先偵測現有隔離。然後使用原生工具。最後再回退到 git。切勿與操控器(harness)對抗。

開始時宣告: 「我正在使用 using-git-worktrees 技能來設定一個隔離的工作區。」

步驟 0:偵測現有隔離

在建立任何東西之前,先檢查你是否已經在一個隔離的工作區中。

GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
BRANCH=$(git branch --show-current)

子模組防護: GIT_DIR != GIT_COMMON 這種情況在 git 子模組內部也會成立。在得出「已經在 worktree 中」的結論之前,請確認你不在子模組中:

# 如果這個指令回傳一個路徑,表示你在子模組中,而不是 worktree — 視為一般倉庫處理
git rev-parse --show-superproject-working-tree 2>/dev/null

如果 GIT_DIR != GIT_COMMON(且不在子模組中): 你已經在一個連結的 worktree 中。跳到步驟 2(專案設置)。不要再建立另一個 worktree。

根據分支狀態回報:

  • 在一個分支上:「已存在隔離工作區於 <path>,分支為 <name>。」
  • 分離的 HEAD:「已存在隔離工作區於 <path>(分離的 HEAD,外部管理)。在完成時需要建立分支。」

如果 GIT_DIR == GIT_COMMON(或在子模組中): 你處在一般倉庫的檢出狀態。

使用者是否已經在你的指示中表明他們的 worktree 偏好?如果沒有,請在建立 worktree 前徵求同意:

「您是否希望我設定一個隔離的 worktree?它可以保護您當前的分支免受變更影響。」

尊重任何已宣告的偏好,無需再次詢問。如果使用者拒絕同意,則在原地工作並跳到步驟 2。

步驟 1:建立隔離工作區

你有兩種機制。請依序嘗試。

1a. 原生 Worktree 工具(偏好)

使用者已要求隔離工作區(步驟 0 同意)。你是否有辦法建立一個 worktree?它可能是一個名為 EnterWorktree、WorktreeCreate、一個 /worktree 命令或一個 --worktree 標誌的工具。如果有,請使用它並跳到步驟 2。

原生工具會自動處理目錄位置、分支建立和清理。當你有原生工具時,使用 git worktree add 會建立你的操控器無法看到或管理的幽靈狀態。

僅當你沒有可用的原生 worktree 工具時,才繼續執行步驟 1b。

1b. Git Worktree 回退方案

僅當步驟 1a 不適用時才使用此方案 — 你沒有可用的原生 worktree 工具。使用 git 手動建立一個 worktree。

目錄選擇

遵循此優先順序。明確的使用者偏好永遠勝過觀察到的檔案系統狀態。

  1. 檢查你的指示中是否有已宣告的 worktree 目錄偏好。 如果使用者已經指定了一個,請直接使用,無需詢問。

  2. 檢查是否存在專案本地的 worktree 目錄:

    ls -d .worktrees 2>/dev/null     # 偏好(隱藏)
    ls -d worktrees 2>/dev/null      # 替代
    

    如果找到,就使用它。如果兩者都存在,.worktrees 優先。

  3. 如果沒有其他可用的指引,則預設使用專案根目錄下的 .worktrees/。

安全驗證(僅限專案本地目錄)

在建立 worktree 之前,必須驗證目錄已被忽略:

git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null

如果沒有被忽略: 將其加入 .gitignore,提交此變更,然後繼續。

為什麼這很重要: 防止意外將 worktree 內容提交到倉庫。

建立 Worktree

# 根據所選位置決定路徑
path="$LOCATION/$BRANCH_NAME"

git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"

沙箱回退: 如果 git worktree add 因權限錯誤(沙箱拒絕)而失敗,請告訴使用者沙箱封鎖了 worktree 的建立,你將改為在當前目錄中工作。然後在原地執行設置和基準測試。

步驟 2:專案設置

自動偵測並執行適當的設置:

# Node.js
if [ -f package.json ]; then npm install; fi

# Rust
if [ -f Cargo.toml ]; then cargo build; fi

# Python
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
if [ -f pyproject.toml ]; then poetry install; fi

# Go
if [ -f go.mod ]; then go mod download; fi

步驟 3:驗證乾淨的基準線

運行測試以確保工作區以乾淨的狀態啟動:

# 使用符合專案的命令
npm test / cargo test / pytest / go test ./...

如果測試失敗: 報告失敗,詢問是否繼續或進行調查。

如果測試通過: 報告準備就緒。

報告

Worktree 準備就緒於 <完整路徑>
測試通過(<N> 個測試,0 個失敗)
準備就緒實作 <功能名稱>

快速參考

情況動作
已在連結的 worktree 中跳過建立(步驟 0)
在子模組中視為一般倉庫(步驟 0 防護)
有可用的原生 worktree 工具使用它(步驟 1a)
沒有原生工具Git worktree 回退(步驟 1b)
存在 .worktrees/使用它(驗證已被忽略)
存在 worktrees/使用它(驗證已被忽略)
兩者都存在使用 .worktrees/
兩者都不存在檢查指示檔案,然後預設 .worktrees/
目錄未被忽略加入 .gitignore + 提交
建立時出現權限錯誤沙箱回退,在原地工作
基準測試失敗報告失敗 + 詢問
沒有 package.json/Cargo.toml跳過依賴安裝

常見合理化藉口

藉口實際情況
「我顯然不在一個 worktree 中 — 無需檢查」執行步驟 0。由操控器建立的隔離和子模組都會誤導肉眼;偵測命令可以解決這個問題。
「git worktree add 比尋找原生工具更快」原生工具(例如 EnterWorktree)掌管放置、分支和清理。繞過它是 #1 的錯誤 — 它會建立你的操控器無法看到或管理的幽靈狀態。
「worktree 目錄肯定已經被忽略了」執行 git check-ignore。未被忽略的 worktree 目錄會將整個樹提交到倉庫中。
「任何目錄名稱都可以」明確的指示勝過現有的專案本地目錄,後者勝過 .worktrees/ 預設值。
「工作區是全新的 — 基準測試可以稍後再跑」一個髒的基準線會讓後續的任何失敗都變得模稜兩可。現在就執行測試;是否在失敗的情況下繼續進行,由你的人類夥伴決定。

Individual skills in this repo

This repo contains 14 individual skills — each has its own dedicated page.

brainstorming

You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).

diagnosing-superpowers

Use when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.

dispatching-parallel-agents

Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies

executing-plans

Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available

finishing-a-development-branch

Use when implementation is complete, all tests pass, and you need to decide how to integrate the work

receiving-code-review

Use when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation

requesting-code-review

Use when completing tasks, implementing major features, or before merging to verify work meets requirements

subagent-driven-development

Use when executing implementation plans with independent tasks in the current session

systematic-debugging

Use when encountering any bug, test failure, or unexpected behavior, before proposing fixes

test-driven-development

Use when implementing any feature or bugfix, before writing implementation code

using-superpowers

Use when starting any conversation - establishes how to find and use skills, requiring skill invocation before ANY response including clarifying questions

verification-before-completion

Use when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always

writing-plans

Use when you have a spec or requirements for a multi-step task, before touching code

writing-skills

Use when creating new skills, editing existing skills, or verifying skills work before deployment

相關技能