用途
aile-git-worktrees 用于在开始需要与当前工作空间隔离的功能工作时,或在执行实施计划之前,创建隔离的 Git 工作树。Git 工作树建立共用相同存储库的独立工作区,允许同时处理多个分支而无需切换。其核心原则是:系统的目录选择与安全验证相结合,实现可靠隔离。
使用时需在开始时声明:“我正在使用 aile-git-worktrees 技能来设置隔离的工作区。”
主要能力
- 按优先级自动选择工作树目录。
- 对项目本地目录进行忽略验证,防止工作树内容被跟踪。
- 自动检测项目类型并运行相应的依赖安装与基线测试。
- 报告工作树位置和测试结果,确认是否就绪。
使用方式
目录选择过程
按以下优先顺序执行:
- 检查现有目录:
ls -d .worktrees 2>/dev/null # Preferred (hidden)
ls -d worktrees 2>/dev/null # Alternative
如果找到则使用该目录;若两者都存在,.worktrees 优先。
- 检查 CLAUDE.md:
grep -i "worktree.*director" CLAUDE.md 2>/dev/null
若指定偏好,无需询问即可使用。
- 询问用户:
如果不存在目录且没有 CLAUDE.md 偏好,则提示:
No worktree directory found. Where should I create worktrees?
- .worktrees/ (project-local, hidden)
- ~/.config/superpowers/worktrees/<project-name>/ (global location)
Which would you prefer?
安全验证
对于项目本地目录(.worktrees 或 worktrees),建立工作树之前必须验证目录是否被忽略:
git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null
如果不被忽略:将适当行加入 .gitignore,提交更改,然后继续创建。此举可防止意外将工作树内容提交到存储库。对于全局目录(~/.config/superpowers/worktrees),无需 .gitignore 验证,因为它完全在项目之外。
创作步骤
- 检测项目名称:
project=$(basename "$(git rev-parse --show-toplevel)")
- 创建工作树:
case $LOCATION in
.worktrees|worktrees)
path="$LOCATION/$BRANCH_NAME"
;;
~/.config/superpowers/worktrees/*)
path="~/.config/superpowers/worktrees/$project/$BRANCH_NAME"
;;
esac
# Create worktree with new branch
git worktree add "$path" -b "$BRANCH_NAME"
cd "$path"
- 运行项目设置,自动检测并运行合适的命令:
# 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
- 验证干净基线,执行测试:
# Examples - use project-appropriate command
npm test
cargo test
pytest
go test ./...
若测试失败:报告失败,询问是否继续或调查。若测试通过:报告就绪。
- 报告位置:
Worktree ready at <full-path> Tests passing (<N> tests, 0 failures) Ready to implement <feature-name>
快速参考
| 情况 | 行动 |
|---|---|
| .worktrees/ 存在 | 使用它(验证忽略) |
| worktrees/ 存在 | 使用它(验证忽略) |
| 两者都存在 | 使用 .worktrees/ |
| 两者都不存在 | 检查 CLAUDE.md → 询问用户 |
| 目录不被忽略 | 新增到 .gitignore + 提交 |
| 测试在基线期间失败 | 报告失败 + 询问 |
| 没有 package.json/Cargo.toml | 跳过依赖安装 |
常见错误
- 跳过忽略验证:工作树内容被跟踪,污染 git 状态。修复:始终在创建项目本地工作树之前使用 git check-ignore。
- 假设目录位置:造成不一致,违反项目约定。修复:遵循优先:现有 > CLAUDE.md > 询问。
- 继续失败测试:无法区分新错误和先前存在的问题。修复:报告失败,获得明确许可才能继续。
- 硬编码设置命令:使用不同工具的项目中断。修复:从项目文件中自动检测(package.json 等)。
示例工作流程
You: I'm using the aile-git-worktrees skill to set up an isolated workspace.
[Check .worktrees/ - exists] [Verify ignored - git check-ignore confirms .worktrees/ is ignored] [Create worktree: git worktree add .worktrees/auth -b feature/auth] [Run npm install] [Run npm test - 47 passing]
Worktree ready at /Users/jesse/myproject/.worktrees/auth Tests passing (47 tests, 0 failures) Ready to implement auth feature
危险信号
绝不:在不验证是否被忽略的情况下建立工作树(项目本地);跳过基线测试验证;不询问就继续失败测试;当目录位置不明确时假设目录位置;跳过 CLAUDE.md 检查。
总是:遵循目录优先:现有 > CLAUDE.md > 询问;验证项目本地目录被忽略;自动检测并运行项目设置;验证干净测试基线。
一体化
调用者:脑力激荡(第 4 阶段)——当设计获得批准并随后实施时需要;子代理驱动开发——执行任何任务之前需要;执行计划——执行任何任务之前需要;任何需要独立工作空间的技能。 搭配:完成开发分支——工作完成后需要清理。