git-worktrees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktrees for Claude Code

用于Claude Code的Git Worktrees

Overview

概述

Run multiple Claude Code sessions in parallel on different branches using Git worktrees. This skill provides simple scripts and workflows to set up, manage, and clean up worktrees, enabling true parallel development without conflicts.
Why Worktrees?
  • Parallel Development: Run multiple Claude Code instances simultaneously
  • Zero Conflicts: Each worktree has independent file state
  • Fast Context Switching: No need to stash/commit when switching tasks
  • Isolated Experiments: Try different approaches without affecting main work
  • Long-Running Tasks: Let Claude work in background while you continue in main

借助Git Worktrees在不同分支上同时运行多个Claude Code会话。本技能提供简单的脚本与工作流,用于设置、管理和清理工作区,实现无冲突的真正并行开发。
为什么选择Worktrees?
  • 并行开发:同时运行多个Claude Code实例
  • 零冲突:每个工作区拥有独立的文件状态
  • 快速上下文切换:切换任务时无需暂存/提交
  • 隔离实验:尝试不同方案而不影响主工作区
  • 长时间任务运行:让Claude在后台工作,你可继续在主工作区操作

Quick Start

快速开始

1. Create a New Worktree (Super Easy!)

1. 创建新工作区(超简单!)

Just run the interactive script:
bash
scripts/create_worktree.sh
This will:
  • ✅ Prompt for feature name
  • ✅ Create a new branch
  • ✅ Set up the worktree
  • ✅ Open in VS Code / editor
  • ✅ Give you the Claude Code setup command
That's it! The script handles all complexity.

只需运行交互式脚本:
bash
scripts/create_worktree.sh
该脚本会:
  • ✅ 提示输入功能名称
  • ✅ 创建新分支
  • ✅ 设置工作区
  • ✅ 在VS Code / 编辑器中打开
  • ✅ 提供Claude Code设置命令
**就是这样!**脚本会处理所有复杂操作。

2. View All Worktrees

2. 查看所有工作区

bash
scripts/list_worktrees.sh
Shows a clean, formatted list of all your worktrees with their branches and status.

bash
scripts/list_worktrees.sh
以清晰格式化的列表展示所有工作区及其分支和状态。

3. Clean Up Old Worktrees

3. 清理旧工作区

bash
scripts/cleanup_worktrees.sh
Interactive removal of merged or abandoned worktrees.

bash
scripts/cleanup_worktrees.sh
交互式移除已合并或废弃的工作区。

Core Workflow

核心工作流

Pattern 1: Parallel Feature Development

模式1:并行功能开发

Scenario: You want Claude to build feature A while you work on feature B.
Steps:
  1. Create worktree for feature A:
    bash
    scripts/create_worktree.sh
    # Enter: feature-a
    # Script creates: ../repo-feature-a/
  2. Open Claude Code in the new worktree:
    • The script outputs the path
    • Open Claude Code and navigate to that directory
    • Run
      /init
      to orient Claude
  3. Give Claude the task:
    "Build the user authentication feature with OAuth support"
  4. Continue your work in main:
    • Your original directory is unchanged
    • No conflicts, no waiting
    • Claude works in parallel
  5. When both are done, merge:
    bash
    # Review Claude's work
    cd ../repo-feature-a
    git log
    
    # If good, merge back
    git checkout main
    git merge feature-a
    
    # Clean up
    scripts/cleanup_worktrees.sh

场景:你希望Claude开发功能A的同时,自己处理功能B。
步骤
  1. 为功能A创建工作区
    bash
    scripts/create_worktree.sh
    # 输入: feature-a
    # 脚本创建: ../repo-feature-a/
  2. 在新工作区中打开Claude Code
    • 脚本会输出路径
    • 打开Claude Code并导航至该目录
    • 运行
      /init
      让Claude熟悉环境
  3. 为Claude分配任务
    "构建支持OAuth的用户认证功能"
  4. 在主工作区继续你的工作
    • 原目录不受影响
    • 无冲突,无需等待
    • Claude在后台并行工作
  5. 完成后合并代码
    bash
    # 查看Claude的工作成果
    cd ../repo-feature-a
    git log
    
    # 若满意则合并至主分支
    git checkout main
    git merge feature-a
    
    # 清理工作区
    scripts/cleanup_worktrees.sh

Pattern 2: Long-Running Refactor

模式2:长期重构

Scenario: You want Claude to refactor a large module while you continue development.
Steps:
  1. Create refactor worktree:
    bash
    scripts/create_worktree.sh
    # Enter: refactor-auth-module
  2. Start Claude in refactor worktree:
    "Refactor the authentication module to use dependency injection"
  3. Continue your daily work in main:
    • No interruption
    • No merge conflicts
    • Check progress periodically
  4. Review and integrate when ready:
    bash
    cd ../repo-refactor-auth-module
    git log --oneline
    # Review changes
    
    # Merge when satisfied
    git checkout main
    git merge refactor-auth-module

场景:你希望Claude重构大型模块的同时,继续日常开发。
步骤
  1. 创建重构工作区
    bash
    scripts/create_worktree.sh
    # 输入: refactor-auth-module
  2. 在重构工作区启动Claude
    "重构认证模块以使用依赖注入"
  3. 在主工作区继续日常工作
    • 无中断
    • 无合并冲突
    • 定期检查进度
  4. 准备就绪后评审并集成
    bash
    cd ../repo-refactor-auth-module
    git log --oneline
    # 评审变更
    
    # 满意后合并
    git checkout main
    git merge refactor-auth-module

Pattern 3: Multiple AI Agents (Advanced)

模式3:多AI Agent(进阶)

Scenario: You want 3 Claude instances working on different features simultaneously.
Steps:
  1. Create 3 worktrees:
    bash
    scripts/create_worktree.sh  # feature-api-endpoints
    scripts/create_worktree.sh  # feature-ui-dashboard
    scripts/create_worktree.sh  # feature-email-notifications
  2. Open 3 Claude Code sessions:
    • Session 1:
      ../repo-feature-api-endpoints/
    • Session 2:
      ../repo-feature-ui-dashboard/
    • Session 3:
      ../repo-feature-email-notifications/
  3. Assign tasks to each Claude:
    • Claude 1: "Build REST API endpoints for user management"
    • Claude 2: "Create admin dashboard UI"
    • Claude 3: "Implement email notification system"
  4. Monitor progress:
    bash
    scripts/list_worktrees.sh
  5. Merge features as they complete:
    bash
    # Feature by feature
    git checkout main
    git merge feature-api-endpoints
    git merge feature-ui-dashboard
    git merge feature-email-notifications
    
    # Clean up
    scripts/cleanup_worktrees.sh

场景:你希望3个Claude实例同时处理不同功能。
步骤
  1. 创建3个工作区
    bash
    scripts/create_worktree.sh  # feature-api-endpoints
    scripts/create_worktree.sh  # feature-ui-dashboard
    scripts/create_worktree.sh  # feature-email-notifications
  2. 打开3个Claude Code会话
    • 会话1:
      ../repo-feature-api-endpoints/
    • 会话2:
      ../repo-feature-ui-dashboard/
    • 会话3:
      ../repo-feature-email-notifications/
  3. 为每个Claude分配任务
    • Claude 1: "构建用户管理的REST API端点"
    • Claude 2: "创建管理员控制面板UI"
    • Claude 3: "实现邮件通知系统"
  4. 监控进度
    bash
    scripts/list_worktrees.sh
  5. 功能完成后逐一合并
    bash
    # 逐个合并功能
    git checkout main
    git merge feature-api-endpoints
    git merge feature-ui-dashboard
    git merge feature-email-notifications
    
    # 清理工作区
    scripts/cleanup_worktrees.sh

Pattern 4: Hotfix While Development Continues

模式4:开发期间修复紧急问题

Scenario: Production bug needs immediate fix while Claude is working on a feature.
Steps:
  1. Claude is already working in a feature worktree
    • Let it continue, don't interrupt
  2. Create hotfix worktree from main:
    bash
    scripts/create_worktree.sh
    # Enter: hotfix-login-bug
    # Choose base branch: main
  3. Fix the bug yourself or with another Claude session:
    bash
    cd ../repo-hotfix-login-bug
    # Make fixes
    git add .
    git commit -m "Fix login redirect bug"
  4. Merge hotfix to main:
    bash
    git checkout main
    git merge hotfix-login-bug
    git push origin main
  5. Sync feature worktree with latest main:
    bash
    cd ../repo-feature-xyz
    scripts/sync_worktree.sh
    # Merges latest main into feature branch

场景:生产环境出现bug需立即修复,而Claude正在开发某个功能。
步骤
  1. Claude已在功能工作区中工作
    • 让它继续,不要中断
  2. 基于主分支创建热修复工作区
    bash
    scripts/create_worktree.sh
    # 输入: hotfix-login-bug
    # 选择基础分支: main
  3. 自行修复bug或启动另一个Claude会话处理
    bash
    cd ../repo-hotfix-login-bug
    # 修复bug
    git add .
    git commit -m "Fix login redirect bug"
  4. 将热修复合并至主分支
    bash
    git checkout main
    git merge hotfix-login-bug
    git push origin main
  5. 同步功能工作区与最新主分支
    bash
    cd ../repo-feature-xyz
    scripts/sync_worktree.sh
    # 将最新主分支合并至功能分支

Essential Scripts

核心脚本

scripts/create_worktree.sh

scripts/create_worktree.sh

Interactive worktree creation with all the right defaults.
What it does:
  1. Prompts for feature name
  2. Validates git repo
  3. Creates branch from main (or specified base)
  4. Sets up worktree in parallel directory
  5. Outputs setup instructions
Usage:
bash
scripts/create_worktree.sh
交互式工作区创建,包含所有合理默认配置。
功能
  1. 提示输入功能名称
  2. 验证Git仓库
  3. 基于主分支(或指定基础分支)创建新分支
  4. 在并行目录中设置工作区
  5. 输出设置说明
用法
bash
scripts/create_worktree.sh

Prompts:

提示:

Feature name: my-feature

Feature name: my-feature

Base branch (default: main):

Base branch (default: main):

Creates: ../repo-my-feature/

创建: ../repo-my-feature/

Branch: my-feature

Branch: my-feature


**Advanced usage:**
```bash

**进阶用法**:
```bash

Create from specific branch

基于指定分支创建

scripts/create_worktree.sh --base develop
scripts/create_worktree.sh --base develop

Specify location

指定位置

scripts/create_worktree.sh --dir ~/worktrees/my-feature

---
scripts/create_worktree.sh --dir ~/worktrees/my-feature

---

scripts/list_worktrees.sh

scripts/list_worktrees.sh

Shows all active worktrees with status.
Output:
╔════════════════════════════════════════════════╗
║            Active Git Worktrees                ║
╚════════════════════════════════════════════════╝

📁 Main Worktree
   Path: /home/user/project
   Branch: main
   Status: clean

📁 Feature Worktrees
   Path: /home/user/project-feature-api
   Branch: feature-api
   Status: 2 uncommitted changes

   Path: /home/user/project-refactor
   Branch: refactor-auth
   Status: clean

展示所有活跃工作区及其状态。
输出
╔════════════════════════════════════════════════╗
║            Active Git Worktrees                ║
╚════════════════════════════════════════════════╝

📁 Main Worktree
   Path: /home/user/project
   Branch: main
   Status: clean

📁 Feature Worktrees
   Path: /home/user/project-feature-api
   Branch: feature-api
   Status: 2 uncommitted changes

   Path: /home/user/project-refactor
   Branch: refactor-auth
   Status: clean

scripts/cleanup_worktrees.sh

scripts/cleanup_worktrees.sh

Interactive cleanup of old worktrees.
Features:
  • Lists all worktrees with merge status
  • Identifies merged branches (safe to remove)
  • Prompts for confirmation
  • Safely removes worktree and branch
Usage:
bash
scripts/cleanup_worktrees.sh
交互式清理旧工作区。
特性
  • 列出所有工作区及其合并状态
  • 识别已合并分支(可安全移除)
  • 提示确认
  • 安全移除工作区与分支
用法
bash
scripts/cleanup_worktrees.sh

Output:

输出:

Found 3 worktrees

Found 3 worktrees

1. feature-api (merged to main) - Safe to remove

1. feature-api (merged to main) - Safe to remove

2. feature-dashboard (not merged) - Keep

2. feature-dashboard (not merged) - Keep

3. old-experiment (not merged, 30 days old) - Consider removing

3. old-experiment (not merged, 30 days old) - Consider removing

Which worktrees to remove? (1,3): 1

Which worktrees to remove? (1,3): 1


---

---

scripts/sync_worktree.sh

scripts/sync_worktree.sh

Keep worktree up-to-date with main branch.
What it does:
  1. Fetches latest from remote
  2. Merges main into current worktree branch
  3. Handles conflicts with guidance
Usage:
bash
undefined
保持工作区与主分支同步。
功能
  1. 获取远程仓库最新内容
  2. 将主分支合并至当前工作区分支
  3. 提供冲突处理指引
用法
bash
undefined

From within a worktree

在工作区内运行

cd ../repo-feature-api scripts/sync_worktree.sh
cd ../repo-feature-api scripts/sync_worktree.sh

Or specify worktree

或指定工作区

scripts/sync_worktree.sh ../repo-feature-api

---
scripts/sync_worktree.sh ../repo-feature-api

---

Claude Code Integration

Claude Code集成

Important: Run /init in Each Worktree

重要提示:在每个工作区运行/init

When you open a new Claude Code session in a worktree, always run
/init
first
:
/init
This ensures Claude:
  • Understands the codebase structure
  • Has proper context
  • Can navigate files correctly
当你在新工作区中打开Claude Code会话时,务必先运行
/init
/init
这能确保Claude:
  • 理解代码库结构
  • 获取正确上下文
  • 可正确导航文件

Recommended Claude Code Workflow

推荐的Claude Code工作流

  1. Create worktree (using script)
  2. Open Claude Code in worktree directory
  3. Run
    /init
    to orient Claude
  4. Give task to Claude
  5. Monitor via
    git log
    or file watching
  6. Review when complete
  7. Merge if satisfied
  8. Cleanup worktree

  1. 创建工作区(使用脚本)
  2. 在工作区目录中打开Claude Code
  3. **运行
    /init
    **让Claude熟悉环境
  4. 为Claude分配任务
  5. 通过
    git log
    或文件监控查看进度
  6. 完成后评审成果
  7. 满意则合并代码
  8. 清理工作区

Best Practices

最佳实践

Naming Conventions

命名规范

Good names:
  • feature-user-auth
  • refactor-api-layer
  • hotfix-login-bug
  • experiment-new-db
Bad names:
  • test
    ❌ (too vague)
  • wt1
    ❌ (meaningless)
  • fixes
    ❌ (unclear)
Recommended format:
<type>-<short-description>

Types:
- feature-*
- refactor-*
- hotfix-*
- experiment-*
- review-*

良好命名
  • feature-user-auth
  • refactor-api-layer
  • hotfix-login-bug
  • experiment-new-db
不良命名
  • test
    ❌(过于模糊)
  • wt1
    ❌(无意义)
  • fixes
    ❌(不清晰)
推荐格式
<类型>-<简短描述>

类型:
- feature-*
- refactor-*
- hotfix-*
- experiment-*
- review-*

Directory Structure

目录结构

Recommended layout:
~/projects/
  └── myapp/              # Main worktree (main branch)
      ├── myapp-feature-api/      # Feature worktree
      ├── myapp-refactor-auth/    # Refactor worktree
      └── myapp-hotfix-bug/       # Hotfix worktree
The scripts default to creating worktrees as siblings to your main directory with a naming pattern:
<repo>-<branch-name>
.

推荐布局
~/projects/
  └── myapp/              # 主工作区(main分支)
      ├── myapp-feature-api/      # 功能工作区
      ├── myapp-refactor-auth/    # 重构工作区
      └── myapp-hotfix-bug/       # 热修复工作区
脚本默认在主目录的同级目录创建工作区,命名模式为:
<仓库名>-<分支名>

When to Use Worktrees

何时使用Worktrees

✅ Use worktrees when:
  • Running multiple Claude Code sessions in parallel
  • Working on independent features simultaneously
  • Doing long-running refactors
  • Experimenting with major changes
  • Quick hotfixes during feature development
  • Code reviews without interrupting work
❌ Don't use worktrees for:
  • Simple branch switching (just use
    git checkout
    )
  • Minor tweaks (stash instead)
  • Single-task workflows
  • Very short-lived branches (< 1 hour)

✅ 推荐使用Worktrees的场景
  • 同时运行多个Claude Code会话
  • 同时开发独立功能
  • 进行长期重构
  • 尝试重大变更
  • 开发期间快速修复紧急问题
  • 无需中断工作即可进行代码评审
❌ 不推荐使用Worktrees的场景
  • 简单分支切换(直接使用
    git checkout
    即可)
  • 小幅度调整(使用暂存功能)
  • 单任务工作流
  • 生命周期极短的分支(<1小时)

Keeping Worktrees in Sync

保持工作区同步

Problem: Feature branches can get out of date with main.
Solution: Regularly sync worktrees:
bash
undefined
问题:功能分支可能与主分支脱节。
解决方案:定期同步工作区:
bash
undefined

Option 1: Use the sync script

方式1:使用同步脚本

cd ../repo-feature-api scripts/sync_worktree.sh
cd ../repo-feature-api scripts/sync_worktree.sh

Option 2: Manual sync

方式2:手动同步

cd ../repo-feature-api git fetch origin git merge origin/main

**Recommended frequency:**
- Daily for long-running features
- After major merges to main
- Before creating PRs

---
cd ../repo-feature-api git fetch origin git merge origin/main

**推荐同步频率**:
- 长期开发的功能每日同步
- 主分支有重大合并后同步
- 创建PR前同步

---

Managing Many Worktrees

管理多个工作区

Quick status of all worktrees:
bash
scripts/list_worktrees.sh
Regular cleanup (weekly):
bash
scripts/cleanup_worktrees.sh
Find stale worktrees:
bash
undefined
快速查看所有工作区状态
bash
scripts/list_worktrees.sh
定期清理(每周一次)
bash
scripts/cleanup_worktrees.sh
查找闲置工作区
bash
undefined

Worktrees with old commits

查找包含旧提交的工作区

git worktree list | while read path branch commit; do cd "$path" last_commit=$(git log -1 --format=%cr) echo "$branch: Last commit $last_commit" done

---
git worktree list | while read path branch commit; do cd "$path" last_commit=$(git log -1 --format=%cr) echo "$branch: Last commit $last_commit" done

---

Troubleshooting

故障排查

Issue: "fatal: invalid reference"

问题:"fatal: invalid reference"

Cause: Branch name has invalid characters
Solution: Use alphanumeric + hyphens only:
bash
undefined
原因:分支名称包含无效字符
解决方案:仅使用字母数字和连字符:
bash
undefined

Bad

错误示例

feature/my_feature ❌
feature/my_feature ❌

Good

正确示例

feature-my-feature ✅

---
feature-my-feature ✅

---

Issue: "cannot remove worktree, path is not a working tree"

问题:"cannot remove worktree, path is not a working tree"

Cause: Worktree directory was manually deleted
Solution: Prune worktree references:
bash
git worktree prune

原因:工作区目录被手动删除
解决方案:清理工作区引用:
bash
git worktree prune

Issue: Claude seems "lost" in worktree

问题:Claude在工作区中似乎「迷失方向」

Cause: Didn't run
/init
after opening worktree
Solution: Always run
/init
when starting Claude in a new worktree:
/init

原因:打开工作区后未运行
/init
解决方案:在新工作区启动Claude时,务必先运行
/init
/init

Issue: Merge conflicts when syncing worktree

问题:同步工作区时出现合并冲突

Cause: Changes in worktree conflict with main
Solution 1: Resolve conflicts manually:
bash
cd ../repo-feature-api
git fetch origin
git merge origin/main
原因:工作区中的变更与主分支冲突
解决方案1:手动解决冲突:
bash
cd ../repo-feature-api
git fetch origin
git merge origin/main

Fix conflicts in editor

在编辑器中修复冲突

git add . git commit -m "Merge main and resolve conflicts"

**Solution 2:** If you want to discard worktree changes and restart:
```bash
cd ../repo-feature-api
git fetch origin
git reset --hard origin/main
git add . git commit -m "Merge main and resolve conflicts"

**解决方案2**:若需丢弃工作区变更并重新开始:
```bash
cd ../repo-feature-api
git fetch origin
git reset --hard origin/main

Start feature over

重新开始功能开发


---

---

Issue: "Cannot lock ref" error

问题:"Cannot lock ref"错误

Cause: Branch exists in multiple worktrees
Solution: You can't have the same branch checked out in multiple worktrees. Create a new branch:
bash
scripts/create_worktree.sh
原因:同一分支在多个工作区中被检出
解决方案:同一分支不能在多个工作区中检出,创建新分支:
bash
scripts/create_worktree.sh

Use a different branch name

使用不同的分支名称


---

---

Issue: Worktree takes up too much disk space

问题:工作区占用过多磁盘空间

Cause: Git worktrees share the
.git
directory, but files are duplicated
Solution 1: Clean up old worktrees:
bash
scripts/cleanup_worktrees.sh
Solution 2: For large repos, use sparse-checkout:
bash
git -C ../repo-feature-api sparse-checkout set src/ tests/

原因:Git Worktrees共享
.git
目录,但文件会被复制
解决方案1:清理旧工作区:
bash
scripts/cleanup_worktrees.sh
解决方案2:对于大型仓库,使用稀疏检出:
bash
git -C ../repo-feature-api sparse-checkout set src/ tests/

Advanced Usage

进阶用法

Custom Slash Command

自定义斜杠命令

Create a
/worktree
command for Claude Code. See
references/slash_command_template.md
for the complete setup.
Usage after setup:
/worktree feature-new-api
Claude will:
  1. Create the worktree
  2. Provide setup instructions
  3. Offer to continue the conversation in the new worktree

为Claude Code创建
/worktree
命令。完整设置请查看
references/slash_command_template.md
设置后用法
/worktree feature-new-api
Claude会:
  1. 创建工作区
  2. 提供设置说明
  3. 提议在新工作区继续对话

Sharing Worktrees in Team

团队中共享Worktrees

Worktrees are local only, but you can share the workflow:
  1. Share branch, not worktree:
    bash
    cd ../repo-feature-api
    git push origin feature-api
    
    # Teammate creates their own worktree
    git worktree add ../repo-feature-api feature-api
  2. Document your worktree structure in team docs
  3. Use consistent naming across team

Worktrees仅为本地资源,但你可共享工作流:
  1. 共享分支而非工作区
    bash
    cd ../repo-feature-api
    git push origin feature-api
    
    # 队友创建自己的工作区
    git worktree add ../repo-feature-api feature-api
  2. 在团队文档中记录你的工作区结构
  3. 团队内使用统一的命名规范

Worktree Performance Optimization

Worktree性能优化

For large repos:
  1. Use sparse-checkout (only checkout files you need):
    bash
    git -C ../repo-feature-api sparse-checkout init
    git -C ../repo-feature-api sparse-checkout set src/ tests/
  2. Use worktree prune regularly:
    bash
    git worktree prune
  3. Limit concurrent worktrees to 3-5 for best performance

针对大型仓库
  1. 使用稀疏检出(仅检出所需文件):
    bash
    git -C ../repo-feature-api sparse-checkout init
    git -C ../repo-feature-api sparse-checkout set src/ tests/
  2. 定期清理Worktree引用
    bash
    git worktree prune
  3. 限制并发工作区数量为3-5个以获得最佳性能

Common Workflows Summary

常见工作流汇总

Quick Reference

快速参考

TaskCommand
Create new worktree
scripts/create_worktree.sh
List all worktrees
scripts/list_worktrees.sh
Clean up worktrees
scripts/cleanup_worktrees.sh
Sync with main
scripts/sync_worktree.sh
Manual create
git worktree add ../path branch-name
Manual list
git worktree list
Manual remove
git worktree remove ../path
Prune references
git worktree prune

任务命令
创建新工作区
scripts/create_worktree.sh
列出所有工作区
scripts/list_worktrees.sh
清理工作区
scripts/cleanup_worktrees.sh
与主分支同步
scripts/sync_worktree.sh
手动创建
git worktree add ../path branch-name
手动列出
git worktree list
手动移除
git worktree remove ../path
清理引用
git worktree prune

Real-World Examples

实际案例

Example 1: Parallel API & UI Development

案例1:并行API与UI开发

Setup:
bash
undefined
设置
bash
undefined

Create API worktree

创建API工作区

scripts/create_worktree.sh
scripts/create_worktree.sh

Name: feature-api

名称: feature-api

Create UI worktree

创建UI工作区

scripts/create_worktree.sh
scripts/create_worktree.sh

Name: feature-ui

名称: feature-ui


**Execution:**
- Claude 1 in `../repo-feature-api`: "Build REST API for user management"
- Claude 2 in `../repo-feature-ui`: "Create React UI for user management"
- You in main: Continue other work

**Merge:**
```bash
git checkout main
git merge feature-api
git merge feature-ui
scripts/cleanup_worktrees.sh


**执行**:
- Claude 1在`../repo-feature-api`:"构建用户管理的REST API"
- Claude 2在`../repo-feature-ui`:"创建用户管理的React UI"
- 你在主工作区:继续其他工作

**合并**:
```bash
git checkout main
git merge feature-api
git merge feature-ui
scripts/cleanup_worktrees.sh

Example 2: Code Review Without Context Switching

案例2:无需上下文切换的代码评审

Scenario: Need to review PR while Claude works on feature
Setup:
bash
undefined
场景:需要评审PR,同时Claude正在开发功能
设置
bash
undefined

Claude is working in feature worktree

Claude正在功能工作区中工作

You need to review PR #123

你需要评审PR #123

scripts/create_worktree.sh
scripts/create_worktree.sh

Name: review-pr-123

名称: review-pr-123

Base: pr-branch-name

基础分支: pr-branch-name


**Execution:**
- Review code in `../repo-review-pr-123`
- Claude continues work in `../repo-feature-xyz`
- No context switching needed

**Cleanup:**
```bash

**执行**:
- 在`../repo-review-pr-123`中评审代码
- Claude在`../repo-feature-xyz`中继续工作
- 无需切换上下文

**清理**:
```bash

After review

评审完成后

scripts/cleanup_worktrees.sh
scripts/cleanup_worktrees.sh

Select review-pr-123

选择review-pr-123


---

---

Example 3: Experimentation Without Risk

案例3:无风险实验

Scenario: Want to try a radical refactor without breaking current work
Setup:
bash
scripts/create_worktree.sh
场景:尝试激进的重构,同时不破坏当前工作
设置
bash
scripts/create_worktree.sh

Name: experiment-new-architecture

名称: experiment-new-architecture


**Execution:**
- Experiment freely in `../repo-experiment-new-architecture`
- Main worktree remains stable
- If experiment fails, just delete worktree

**Decision:**
```bash

**执行**:
- 在`../repo-experiment-new-architecture`中自由实验
- 主工作区保持稳定
- 若实验失败,只需删除工作区

**决策**:
```bash

If experiment succeeded

若实验成功

git checkout main git merge experiment-new-architecture
git checkout main git merge experiment-new-architecture

If experiment failed

若实验失败

scripts/cleanup_worktrees.sh
scripts/cleanup_worktrees.sh

Delete experiment worktree (no harm done)

删除实验工作区(无任何影响)


---

---

Tips for Maximum Productivity

最大化生产力技巧

1. Name Worktrees Descriptively

1. 为工作区起描述性名称

  • You'll thank yourself later when you have 5 worktrees open
  • 当你打开5个工作区时,会感谢自己当初的命名

2. Always Run /init

2. 务必运行/init

  • First thing when opening Claude in a new worktree
  • 在新工作区打开Claude后首先执行

3. Merge Frequently

3. 频繁合并

  • Don't let worktrees diverge for weeks
    • Sync with main regularly
  • 不要让工作区与主分支脱节数周
    • 定期与主分支同步

4. Clean Up Weekly

4. 每周清理

  • Run
    scripts/cleanup_worktrees.sh
    every Friday
    • Keep your workspace tidy
  • 每周五运行
    scripts/cleanup_worktrees.sh
    • 保持工作区整洁

5. Use Scripts

5. 使用脚本

  • Don't memorize git worktree commands
    • Scripts handle edge cases and validation
  • 无需记忆Git Worktree命令
    • 脚本会处理边缘情况与验证

6. Monitor Progress

6. 监控进度

  • Use
    scripts/list_worktrees.sh
    to see what's active
    • Check git logs in worktrees periodically
  • 使用
    scripts/list_worktrees.sh
    查看活跃工作区
    • 定期检查工作区中的Git日志

7. Limit Concurrent Sessions

7. 限制并发会话数量

  • 3-5 worktrees max for best performance
    • More than that gets hard to manage

  • 最多3-5个工作区以获得最佳性能
    • 过多工作区会难以管理

Resources

资源

Scripts

脚本

  • create_worktree.sh - Interactive worktree creation
  • list_worktrees.sh - Show all active worktrees
  • cleanup_worktrees.sh - Remove old worktrees
  • sync_worktree.sh - Keep worktrees synchronized
  • create_worktree.sh - 交互式工作区创建
  • list_worktrees.sh - 展示所有活跃工作区
  • cleanup_worktrees.sh - 移除旧工作区
  • sync_worktree.sh - 保持工作区同步

References

参考文档

  • worktree_commands.md - Complete Git worktree command reference
  • best_practices.md - Detailed best practices and patterns
  • slash_command_template.md - Create custom
    /worktree
    command

  • worktree_commands.md - 完整的Git Worktree命令参考
  • best_practices.md - 详细的最佳实践与模式
  • slash_command_template.md - 创建自定义
    /worktree
    命令

Summary

总结

Git worktrees enable true parallel development with Claude Code:
Run multiple Claude sessions without conflicts ✅ Switch contexts instantly without stashing ✅ Experiment safely without breaking main ✅ Simple scripts handle all complexity ✅ Clean workflows for common scenarios
Get started:
bash
scripts/create_worktree.sh
That's it! The scripts make worktrees simple and practical.
Git Worktrees为Claude Code实现真正的并行开发:
同时运行多个Claude会话,无冲突 ✅ 即时切换上下文,无需暂存 ✅ 安全实验,不影响主工作区 ✅ 简单脚本处理所有复杂操作 ✅ 清晰工作流适配常见场景
快速开始
bash
scripts/create_worktree.sh
**就是这样!**脚本让Worktrees的使用简单且实用。