git-worktree
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktree Manager
Git工作树管理器
Manage isolated Git worktrees for parallel development, following project conventions.
遵循项目规范,管理用于并行开发的隔离式Git工作树。
Quick Reference
快速参考
| Command | Description |
|---|---|
| Create worktree with symlinked .env |
| List all worktrees |
| Remove inactive worktrees |
| Switch to a worktree |
| 命令 | 说明 |
|---|---|
| 创建带有符号链接.env文件的工作树 |
| 列出所有工作树 |
| 移除闲置工作树 |
| 切换到指定工作树 |
CRITICAL: Always Use the Manager Script
重要提示:请始终使用管理器脚本
NEVER call directly. Always use the script.
git worktree addbash
undefined切勿直接调用命令。请始终使用本脚本。
git worktree addbash
undefinedCORRECT
正确方式
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature-name
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature-name
WRONG - Never do this directly
错误方式 - 切勿直接执行以下命令
git worktree add .github/worktrees/feature-name -b feature-name develop
The script handles:
1. Symlinks `.env` file (not copy) from project root
2. Ensures `.github/worktrees/` is in `.gitignore`
3. Creates consistent directory structure at `.github/worktrees/`git worktree add .github/worktrees/feature-name -b feature-name develop
本脚本会处理以下事项:
1. 从项目根目录为`.env`文件创建符号链接(而非复制)
2. 确保`.github/worktrees/`已添加到`.gitignore`
3. 在`.github/worktrees/`目录下创建统一的目录结构CRITICAL: Create the Worktree, Then STOP
重要提示:创建工作树后请立即停止当前会话
After creating a worktree, STOP. Do NOT implement anything in the current session. Do NOT into the worktree and keep working.
cdYour job in this session is ONLY:
- Create the worktree
- Show the user the copy-paste command from the script output
- STOP and tell the user to run that command in a new terminal
All planning, brainstorming, and implementation happens in the new Claude Code instance inside the worktree — NOT here.
Why: Claude Code's working directory and statusline are set at launch time. If you work here after creating a worktree, auto-compact will lose the worktree context and revert to the main repo mid-work.
创建工作树后,请停止当前操作。不要在当前会话中进行任何开发工作,不要到工作树中继续操作。
cd你在本次会话中的任务仅为:
- 创建工作树
- 向用户展示脚本输出中的可复制命令
- 停止操作并告知用户在新终端中运行该命令
所有的规划、头脑风暴和开发工作都应在工作树内的新Claude Code实例中进行——而非当前会话。
原因: Claude Code的工作目录和状态行是在启动时设置的。如果在创建工作树后继续在当前会话中操作,自动压缩功能会丢失工作树上下文,中途切换回主仓库。
Project Conventions
项目规范
- Default base branch: (not
develop)main - Symlinks for files, not copies — single source of truth
.env - Worktree directory:
.github/worktrees/
- 默认基准分支:(而非
develop)main - 文件使用符号链接而非复制——保持唯一可信源
.env - 工作树目录:
.github/worktrees/
Commands
命令说明
create <branch-name> [from-branch]
create <branch-name> [from-branch]create <branch-name> [from-branch]
create <branch-name> [from-branch]Creates worktree in . Defaults to branching from .
.github/worktrees/<branch-name>developbash
undefined在目录下创建工作树。默认基于分支创建。
.github/worktrees/<branch-name>developbash
undefinedCreate from develop (default)
基于develop分支创建(默认)
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature/pipeline-steps
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature/pipeline-steps
Create from specific branch
基于指定分支创建
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create hotfix/auth main
**What happens:**
1. Checks if worktree exists
2. Creates `.github/worktrees/` directory if needed
3. Updates base branch from remote
4. Creates worktree and branch
5. **Symlinks `.env` from project root**
6. Verifies symlink
7. **Outputs a copy-paste command** to launch Claude Code in the worktreebash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create hotfix/auth main
**执行流程:**
1. 检查工作树是否已存在
2. 若需要则创建`.github/worktrees/`目录
3. 从远程仓库更新基准分支
4. 创建工作树和分支
5. **从项目根目录为.env创建符号链接**
6. 验证符号链接
7. **输出可复制的命令**,用于在工作树中启动Claude Codelist
or ls
listlslist
或 ls
listlsbash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh listShows:
- Worktree name and branch
- Current worktree marked with
* - Main repo status
bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh list展示内容:
- 工作树名称和分支
- 当前工作树标记为
* - 主仓库状态
switch <name>
or go <name>
switch <name>go <name>switch <name>
或 go <name>
switch <name>go <name>bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh switch feature/pipeline-stepsbash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh switch feature/pipeline-stepscleanup
or clean
cleanupcleancleanup
或 clean
cleanupcleanRemoves inactive worktrees interactively.
bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanupSafety: Won't remove current worktree.
交互式移除闲置工作树。
bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup安全机制: 不会移除当前工作树。
Workflow Examples
工作流示例
Feature Development
功能开发
bash
undefinedbash
undefined1. Create worktree from develop
1. 基于develop分支创建工作树
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature/auth-system
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create feature/auth-system
Script output:
脚本输出:
━━━ Launch Claude Code in this worktree ━━━
━━━ 在该工作树中启动Claude Code ━━━
cd /path/to/.github/worktrees/feature-auth-system && claude
cd /path/to/.github/worktrees/feature-auth-system && claude
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
2. CLAUDE STOPS HERE. Tells user to copy-paste the command above into a new terminal.
2. CLAUDE在此处停止操作。告知用户将上述命令复制粘贴到新终端中。
DO NOT continue with any work in this session.
请勿在当前会话中继续进行任何工作。
3. User pastes command → new Claude Code instance starts in the worktree
3. 用户粘贴命令 → 新的Claude Code实例在工作树中启动
User brainstorms, plans, and implements in that fresh session
用户在新会话中进行头脑风暴、规划和开发
4. When done, cleanup from main repo
4. 完成后,从主仓库清理工作树
cd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup
undefinedcd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup
undefinedPR Review in Isolation
隔离式PR评审
bash
undefinedbash
undefinedCreate worktree from PR branch
基于PR分支创建工作树
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create pr-42-auth-fix origin/pr-branch
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh create pr-42-auth-fix origin/pr-branch
CLAUDE STOPS HERE. Tells user to copy-paste the launch command into a new terminal.
CLAUDE在此处停止操作。告知用户将启动命令复制粘贴到新终端中。
Cleanup after review
评审完成后清理工作树
cd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup
undefinedcd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup
undefinedDirectory Structure
目录结构
text
project-root/
├── .env # Source of truth
├── .github/
│ └── worktrees/ # All worktrees live here
│ ├── feature-auth/
│ │ ├── .env -> ../../.. # Relative symlink to root .env
│ │ ├── src/
│ │ └── ...
│ └── feature-pipeline/
│ ├── .env -> ../../.. # Relative symlink to root .env
│ └── ...
└── .gitignore # Includes .github/worktreestext
project-root/
├── .env # 唯一可信源
├── .github/
│ └── worktrees/ # 所有工作树存储在此
│ ├── feature-auth/
│ │ ├── .env -> ../../.. # 指向根目录.env的相对符号链接
│ │ ├── src/
│ │ └── ...
│ └── feature-pipeline/
│ ├── .env -> ../../.. # 指向根目录.env的相对符号链接
│ └── ...
└── .gitignore # 已包含.github/worktreesTroubleshooting
故障排除
"Worktree already exists"
"Worktree already exists"(工作树已存在)
Switch to it instead:
bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh switch <name>请切换到该工作树:
bash
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh switch <name>"Cannot remove worktree: it is current"
"Cannot remove worktree: it is current"(无法移除工作树:当前正处于该工作树)
Return to main repo first:
bash
cd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanup请先返回主仓库:
bash
cd "$(git rev-parse --show-toplevel)"
bash ~/.claude/skills/git-worktree/scripts/worktree-manager.sh cleanupSymlink broken?
符号链接损坏?
Recreate manually from the worktree directory:
bash
cd .github/worktrees/<name>
rm .env在工作树目录中手动重新创建:
bash
cd .github/worktrees/<name>
rm .envCompute relative path to project root .env
计算指向项目根目录.env的相对路径
GIT_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel)
ln -s "$(python3 -c "import os; print(os.path.relpath('$GIT_ROOT/.env', '$(pwd)'))")" .env
ls -la .env # Verify
undefinedGIT_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel)
ln -s "$(python3 -c "import os; print(os.path.relpath('$GIT_ROOT/.env', '$(pwd)'))")" .env
ls -la .env # 验证
undefinedWhy a New Terminal?
为什么需要新终端?
Claude Code's CWD and statusline are set at launch. If you into a worktree from an existing session, the statusline stays wrong, and auto-compact causes Claude to revert to the main repo.
cdThe script outputs a copy-paste command that:
- s to the worktree
cd - Launches a new instance
claude
This ensures correct statusline and stable context throughout the session.
Claude Code的当前工作目录(CWD)和状态行是在启动时设置的。如果在现有会话中到工作树,状态行将保持错误信息,且自动压缩功能会导致Claude切换回主仓库。
cd脚本输出的可复制命令会:
- 到工作树目录
cd - 启动一个全新的实例
claude
这样可以确保在整个会话过程中状态行正确且上下文稳定。