Community编程与开发github.com

使用Git工作树

在开始需要与当前工作区隔离的功能开发或执行实现计划之前使用——确保通过原生工具或git工作树回退方式创建一个隔离的工作区。

使用Git工作树 是什么?

该技能旨在确保AI代理在隔离的工作区中进行开发操作,避免干扰当前分支或工作区。它优先检测是否已经处于隔离环境(如链接的工作树或子模块),然后尝试使用平台原生工作树工具(如`EnterWorktree`或`WorktreeCreate`),仅在无原生工具时回退到手动`git worktree add`。技能详细描述了目录选择、安全验证(确保工作树目录被.gitignore忽略)、项目依赖自动安装、以及基线测试验证的完整流程。支持Node.js、Rust、Python和Go项目,并包含了常见误区的澄清(如跳过检测或绕过原生工具的风险)。适用于Claude Code、Cursor、Codex等AI代理平台,帮助用户安全地管理并行功能开发。

兼容平台~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工作树 是做什么的?

概述

确保工作在隔离的工作区中进行。优先使用平台的原生工作树工具。仅在无原生工具可用时回退到手动git工作树。

核心原则: 首先检测现有隔离状态。然后使用原生工具。最后回退到git。切勿与管理系统对抗。

开始时声明: “我正在使用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子模块内部也为真。在得出结论“已在工作树中”之前,验证你不在子模块中:

# 如果返回路径,说明在子模块中,而不是工作树——视为普通仓库
git rev-parse --show-superproject-working-tree 2>/dev/null

如果 GIT_DIR != GIT_COMMON(且不是子模块): 你已经处于一个链接的工作树中。跳到第2步(项目设置)。不要创建另一个工作树。

报告分支状态:

  • 在某个分支上:“已经在隔离工作区 <path>,位于分支 <name>。”
  • 分离HEAD:“已经在隔离工作区 <path>(分离HEAD,外部管理)。完成时需要创建分支。”

如果 GIT_DIR == GIT_COMMON(或在子模块中): 你处于一个普通的仓库检出中。

用户是否已经在你的指令中表明了他们的工作树偏好?如果没有,请在创建工作树之前征求同意:

“您希望我设置一个隔离的工作树吗?它可以保护您当前的分支免受更改影响。”

如果有已有的声明偏好,请直接遵守,无需询问。如果用户拒绝同意,则在原地工作并跳到第2步。

第1步:创建隔离工作区

你有两种机制。按此顺序尝试。

1a. 原生工作树工具(首选)

用户已要求隔离的工作区(第0步的同意)。你是否已经有创建工作树的方法?可能是一个名为EnterWorktree、WorktreeCreate的工具,一个/worktree命令,或一个--worktree标志。如果有,使用它并跳到第2步。

原生工具会自动处理目录放置、分支创建和清理。在拥有原生工具时使用git worktree add会创建你的管理系统看不到也无法管理的幽灵状态。

只有在没有原生工作树工具可用时才继续执行第1b步。

1b. Git工作树回退

仅当第1a步不适用时使用——你没有可用的原生工作树工具。使用git手动创建工作树。

目录选择

按以下优先级进行。明确的用户偏好始终优先于观察到的文件系统状态。

  1. 检查你的指令中是否有声明的工作树目录偏好。 如果用户已经指定了,直接使用,无需询问。

  2. 检查是否存在项目本地的已有工作树目录:

    ls -d .worktrees 2>/dev/null     # 首选(隐藏)
    ls -d worktrees 2>/dev/null      # 备选
    

    如果找到,使用它。如果两者都存在,.worktrees 胜出。

  3. 如果没有其他指导可用,默认在项目根目录使用 .worktrees/。

安全验证(仅针对项目本地目录)

在创建工作树之前,必须验证目录是否被忽略:

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

如果未忽略: 添加到.gitignore,提交更改,然后继续。

为什么关键: 防止意外将工作树内容提交到仓库中。

创建工作树

# 根据选定位置确定路径
path="$LOCATION/$BRANCH_NAME"

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

沙箱回退: 如果git worktree add因权限错误(沙箱拒绝)而失败,告知用户沙箱阻止了工作树创建,你将改为在当前目录中工作。然后在该目录中执行设置和基线测试。

第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 ./...

如果测试失败: 报告失败,询问是否继续或调查。

如果测试通过: 报告准备就绪。

报告

工作树在 <full-path> 准备就绪
测试通过(<N> 个测试,0 个失败)
准备实现 <feature-name>

快速参考

情况操作
已在链接的工作树中跳过创建(第0步)
在子模块中视为普通仓库(第0步防护)
有原生工作树工具可用使用它(第1a步)
无原生工具Git工作树回退(第1b步)
.worktrees/ 存在使用它(验证已忽略)
worktrees/ 存在使用它(验证已忽略)
两者都存在使用 .worktrees/
两者都不存在检查指令文件,然后默认使用 .worktrees/
目录未被忽略添加到 .gitignore + 提交
创建时权限错误沙箱回退,原地工作
基线测试失败报告失败 + 询问
无 package.json/Cargo.toml跳过依赖安装

常见辩解

借口现实
“我显然不在工作树中——无需检查”运行第0步。管理系统创建的隔离和子模块都会欺骗肉眼;检测命令能确认。
“git worktree add 比寻找原生工具更快”原生工具(例如 EnterWorktree)拥有放置、分支和清理的职责。绕过它是#1的错误——它创建了你的管理系统无法看到或管理的幽灵状态。
“工作树目录肯定已经被忽略了”运行 git check-ignore。未忽略的工作树目录会将整个树提交到仓库中。
“任何目录名都可以”明确的指令优先于已有的项目本地目录,后者又优先于 .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

相关技能