mcpField
U
Using Git Worktrees

Using Git Worktrees

github.com/obra开发755524 次安装55 周前更新
代码仓库

关于 Using Git Worktrees

Using Git Worktrees 是帮助开发者为并行分支工作创建隔离工作区的技能。它说明如何选择位置、检查安全性、安装依赖并验证测试基线。

主要功能

  1. 01选择 worktree 位置
  2. 02检查项目内的 gitignore 规则
  3. 03识别并运行项目设置
  4. 04验证测试基线
  5. 05报告工作区是否就绪

适用场景

  1. 01同时处理多个分支
  2. 02创建隔离的功能开发区
  3. 03检查已有测试失败

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/obra/superpowers-skills --skill using-git-worktrees

安装后在 Claude Code 中输入/using-git-worktrees确认已加载。

详细介绍

使用 Git Worktrees

概述

Git worktrees 可创建共享同一仓库的隔离工作区,让你同时在多个分支上工作,而无需切换分支。

核心原则: 系统化选择目录 + 安全验证 = 可靠的隔离。

开始时声明:「我正在使用 Using Git Worktrees 技能来设置隔离的工作区。」

目录选择流程

按以下优先级顺序操作:

1. 检查现有目录
# Check in priority order
ls -d .worktrees 2>/dev/null     # Preferred (hidden)
ls -d worktrees 2>/dev/null      # Alternative

**如果找到:**使用该目录。如果两个目录都存在,则优先使用 .worktrees。

2. 检查 CLAUDE.md
grep -i "worktree.*director" CLAUDE.md 2>/dev/null

**如果指定了偏好:**直接使用,无需询问。

3. 询问用户

如果目录不存在,且 CLAUDE.md 中未指定偏好:

No worktree directory found. Where should I create worktrees?

1. .worktrees/ (project-local, hidden)
2. ~/.config/superpowers/worktrees/<project-name>/ (global location)

Which would you prefer?

安全验证

对于项目本地目录(.worktrees 或 worktrees)

创建 worktree 前,必须验证 .gitignore:

# Check if directory pattern in .gitignore
grep -q "^\.worktrees/$" .gitignore || grep -q "^worktrees/$" .gitignore

如果未列入 .gitignore:

根据 Jesse 的规则「立即修复损坏之处」:

  1. 在 .gitignore 中添加适当的条目
  2. 提交更改
  3. 创建 worktree

**为何至关重要:**防止意外将 worktree 内容提交到仓库。

对于全局目录(~/.config/superpowers/worktrees)

无需验证 .gitignore,因为该目录完全位于项目之外。

创建步骤

1. 检测项目名称
project=$(basename "$(git rev-parse --show-toplevel)")
2. 创建 Worktree
# Determine full path
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"
3. 运行项目设置

自动检测并运行相应的设置流程:

# 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
4. 验证基线干净

运行测试,确保 worktree 初始状态干净:

# Examples - use project-appropriate command
npm test
cargo test
pytest
go test ./...

**如果测试失败:**报告失败情况,并询问是继续还是进行调查。

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

5. 报告位置
Worktree ready at <full-path>
Tests passing (<N> tests, 0 failures)
Ready to implement <feature-name>

快速参考

情况操作
.worktrees/ 存在使用该目录(验证 .gitignore)
worktrees/ 存在使用该目录(验证 .gitignore)
两者都存在使用 .worktrees/
两者都不存在检查 CLAUDE.md → 询问用户
目录未列入 .gitignore立即添加并提交
基线测试失败报告失败情况并询问
没有 package.json/Cargo.toml跳过依赖项安装

常见错误

跳过 .gitignore 验证

  • **问题:**worktree 内容会被跟踪,污染 git 状态
  • **修复:**创建项目本地 worktree 前,务必 grep .gitignore

想当然地选择目录位置

  • **问题:**造成不一致,违反项目约定
  • **修复:**遵循优先级:现有目录 > CLAUDE.md > 询问

在测试失败时继续

  • **问题:**无法区分新 Bug 和已有问题
  • **修复:**报告失败情况,征得明确同意后再继续

将设置命令写死

  • **问题:**项目使用其他工具时会出错
  • **修复:**根据项目文件(package.json 等)自动检测

工作流示例

You: I'm using the Using Git Worktrees skill to set up an isolated workspace.

[Check .worktrees/ - exists]
[Verify .gitignore - contains .worktrees/]
[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

注意事项

切勿:

  • 未验证 .gitignore 就创建 worktree(项目本地)
  • 跳过基线测试验证
  • 未经询问就在测试失败时继续
  • 目录位置不明确时自行假定
  • 跳过 CLAUDE.md 检查

务必:

  • 遵循目录优先级:现有目录 > CLAUDE.md > 询问
  • 验证项目本地目录是否列入 .gitignore
  • 自动检测并运行项目设置流程
  • 验证测试基线干净

集成

由以下内容调用:

  • skills/collaboration/brainstorming(阶段 4)
  • 任何需要隔离工作区的技能

搭配使用:

  • skills/collaboration/finishing-a-development-branch(清理)
  • skills/collaboration/executing-plans(在此处开展工作)

评论

支持 Markdown