git-worktrees
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit 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.shThis 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.shShows 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.shInteractive 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:
-
Create worktree for feature A:bash
scripts/create_worktree.sh # Enter: feature-a # Script creates: ../repo-feature-a/ -
Open Claude Code in the new worktree:
- The script outputs the path
- Open Claude Code and navigate to that directory
- Run to orient Claude
/init
-
Give Claude the task:
"Build the user authentication feature with OAuth support" -
Continue your work in main:
- Your original directory is unchanged
- No conflicts, no waiting
- Claude works in parallel
-
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。
步骤:
-
为功能A创建工作区:bash
scripts/create_worktree.sh # 输入: feature-a # 脚本创建: ../repo-feature-a/ -
在新工作区中打开Claude Code:
- 脚本会输出路径
- 打开Claude Code并导航至该目录
- 运行让Claude熟悉环境
/init
-
为Claude分配任务:
"构建支持OAuth的用户认证功能" -
在主工作区继续你的工作:
- 原目录不受影响
- 无冲突,无需等待
- Claude在后台并行工作
-
完成后合并代码: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:
-
Create refactor worktree:bash
scripts/create_worktree.sh # Enter: refactor-auth-module -
Start Claude in refactor worktree:
"Refactor the authentication module to use dependency injection" -
Continue your daily work in main:
- No interruption
- No merge conflicts
- Check progress periodically
-
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重构大型模块的同时,继续日常开发。
步骤:
-
创建重构工作区:bash
scripts/create_worktree.sh # 输入: refactor-auth-module -
在重构工作区启动Claude:
"重构认证模块以使用依赖注入" -
在主工作区继续日常工作:
- 无中断
- 无合并冲突
- 定期检查进度
-
准备就绪后评审并集成: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:
-
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 -
Open 3 Claude Code sessions:
- Session 1:
../repo-feature-api-endpoints/ - Session 2:
../repo-feature-ui-dashboard/ - Session 3:
../repo-feature-email-notifications/
- Session 1:
-
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"
-
Monitor progress:bash
scripts/list_worktrees.sh -
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实例同时处理不同功能。
步骤:
-
创建3个工作区:bash
scripts/create_worktree.sh # feature-api-endpoints scripts/create_worktree.sh # feature-ui-dashboard scripts/create_worktree.sh # feature-email-notifications -
打开3个Claude Code会话:
- 会话1:
../repo-feature-api-endpoints/ - 会话2:
../repo-feature-ui-dashboard/ - 会话3:
../repo-feature-email-notifications/
- 会话1:
-
为每个Claude分配任务:
- Claude 1: "构建用户管理的REST API端点"
- Claude 2: "创建管理员控制面板UI"
- Claude 3: "实现邮件通知系统"
-
监控进度:bash
scripts/list_worktrees.sh -
功能完成后逐一合并: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:
-
Claude is already working in a feature worktree
- Let it continue, don't interrupt
-
Create hotfix worktree from main:bash
scripts/create_worktree.sh # Enter: hotfix-login-bug # Choose base branch: main -
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" -
Merge hotfix to main:bash
git checkout main git merge hotfix-login-bug git push origin main -
Sync feature worktree with latest main:bash
cd ../repo-feature-xyz scripts/sync_worktree.sh # Merges latest main into feature branch
场景:生产环境出现bug需立即修复,而Claude正在开发某个功能。
步骤:
-
Claude已在功能工作区中工作
- 让它继续,不要中断
-
基于主分支创建热修复工作区:bash
scripts/create_worktree.sh # 输入: hotfix-login-bug # 选择基础分支: main -
自行修复bug或启动另一个Claude会话处理:bash
cd ../repo-hotfix-login-bug # 修复bug git add . git commit -m "Fix login redirect bug" -
将热修复合并至主分支:bash
git checkout main git merge hotfix-login-bug git push origin main -
同步功能工作区与最新主分支: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:
- Prompts for feature name
- Validates git repo
- Creates branch from main (or specified base)
- Sets up worktree in parallel directory
- Outputs setup instructions
Usage:
bash
scripts/create_worktree.sh交互式工作区创建,包含所有合理默认配置。
功能:
- 提示输入功能名称
- 验证Git仓库
- 基于主分支(或指定基础分支)创建新分支
- 在并行目录中设置工作区
- 输出设置说明
用法:
bash
scripts/create_worktree.shPrompts:
提示:
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
**进阶用法**:
```bashCreate 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: cleanscripts/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.shOutput:
输出:
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:
- Fetches latest from remote
- Merges main into current worktree branch
- Handles conflicts with guidance
Usage:
bash
undefined保持工作区与主分支同步。
功能:
- 获取远程仓库最新内容
- 将主分支合并至当前工作区分支
- 提供冲突处理指引
用法:
bash
undefinedFrom 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 first:
/init/initThis ensures Claude:
- Understands the codebase structure
- Has proper context
- Can navigate files correctly
当你在新工作区中打开Claude Code会话时,务必先运行:
/init/init这能确保Claude:
- 理解代码库结构
- 获取正确上下文
- 可正确导航文件
Recommended Claude Code Workflow
推荐的Claude Code工作流
- Create worktree (using script)
- Open Claude Code in worktree directory
- Run to orient Claude
/init - Give task to Claude
- Monitor via or file watching
git log - Review when complete
- Merge if satisfied
- Cleanup worktree
- 创建工作区(使用脚本)
- 在工作区目录中打开Claude Code
- **运行**让Claude熟悉环境
/init - 为Claude分配任务
- 通过或文件监控查看进度
git log - 完成后评审成果
- 满意则合并代码
- 清理工作区
Best Practices
最佳实践
Naming Conventions
命名规范
Good names:
- ✅
feature-user-auth - ✅
refactor-api-layer - ✅
hotfix-login-bug - ✅
experiment-new-db
Bad names:
- ❌ (too vague)
test - ❌ (meaningless)
wt1 - ❌ (unclear)
fixes
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 worktreeThe 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
undefinedOption 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.shRegular cleanup (weekly):
bash
scripts/cleanup_worktrees.shFind stale worktrees:
bash
undefined快速查看所有工作区状态:
bash
scripts/list_worktrees.sh定期清理(每周一次):
bash
scripts/cleanup_worktrees.sh查找闲置工作区:
bash
undefinedWorktrees 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
undefinedBad
错误示例
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 pruneIssue: Claude seems "lost" in worktree
问题:Claude在工作区中似乎「迷失方向」
Cause: Didn't run after opening worktree
/initSolution: Always run when starting Claude in a new worktree:
/init/init原因:打开工作区后未运行
/init解决方案:在新工作区启动Claude时,务必先运行:
/init/initIssue: 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/mainFix 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/maingit add .
git commit -m "Merge main and resolve conflicts"
**解决方案2**:若需丢弃工作区变更并重新开始:
```bash
cd ../repo-feature-api
git fetch origin
git reset --hard origin/mainStart 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.shUse a different branch name
使用不同的分支名称
---
---Issue: Worktree takes up too much disk space
问题:工作区占用过多磁盘空间
Cause: Git worktrees share the directory, but files are duplicated
.gitSolution 1: Clean up old worktrees:
bash
scripts/cleanup_worktrees.shSolution 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 command for Claude Code. See for the complete setup.
/worktreereferences/slash_command_template.mdUsage after setup:
/worktree feature-new-apiClaude will:
- Create the worktree
- Provide setup instructions
- Offer to continue the conversation in the new worktree
为Claude Code创建命令。完整设置请查看。
/worktreereferences/slash_command_template.md设置后用法:
/worktree feature-new-apiClaude会:
- 创建工作区
- 提供设置说明
- 提议在新工作区继续对话
Sharing Worktrees in Team
团队中共享Worktrees
Worktrees are local only, but you can share the workflow:
-
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 -
Document your worktree structure in team docs
-
Use consistent naming across team
Worktrees仅为本地资源,但你可共享工作流:
-
共享分支而非工作区:bash
cd ../repo-feature-api git push origin feature-api # 队友创建自己的工作区 git worktree add ../repo-feature-api feature-api -
在团队文档中记录你的工作区结构
-
团队内使用统一的命名规范
Worktree Performance Optimization
Worktree性能优化
For large repos:
-
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/ -
Use worktree prune regularly:bash
git worktree prune -
Limit concurrent worktrees to 3-5 for best performance
针对大型仓库:
-
使用稀疏检出(仅检出所需文件):bash
git -C ../repo-feature-api sparse-checkout init git -C ../repo-feature-api sparse-checkout set src/ tests/ -
定期清理Worktree引用:bash
git worktree prune -
限制并发工作区数量为3-5个以获得最佳性能
Common Workflows Summary
常见工作流汇总
Quick Reference
快速参考
| Task | Command |
|---|---|
| Create new worktree | |
| List all worktrees | |
| Clean up worktrees | |
| Sync with main | |
| Manual create | |
| Manual list | |
| Manual remove | |
| Prune references | |
| 任务 | 命令 |
|---|---|
| 创建新工作区 | |
| 列出所有工作区 | |
| 清理工作区 | |
| 与主分支同步 | |
| 手动创建 | |
| 手动列出 | |
| 手动移除 | |
| 清理引用 | |
Real-World Examples
实际案例
Example 1: Parallel API & UI Development
案例1:并行API与UI开发
Setup:
bash
undefined设置:
bash
undefinedCreate 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.shExample 2: Code Review Without Context Switching
案例2:无需上下文切换的代码评审
Scenario: Need to review PR while Claude works on feature
Setup:
bash
undefined场景:需要评审PR,同时Claude正在开发功能
设置:
bash
undefinedClaude 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`中继续工作
- 无需切换上下文
**清理**:
```bashAfter 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.shName: 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`中自由实验
- 主工作区保持稳定
- 若实验失败,只需删除工作区
**决策**:
```bashIf 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 every Friday
scripts/cleanup_worktrees.sh- 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 to see what's active
scripts/list_worktrees.sh- 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 command
/worktree
- 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.shThat's it! The scripts make worktrees simple and practical.
Git Worktrees为Claude Code实现真正的并行开发:
✅ 同时运行多个Claude会话,无冲突
✅ 即时切换上下文,无需暂存
✅ 安全实验,不影响主工作区
✅ 简单脚本处理所有复杂操作
✅ 清晰工作流适配常见场景
快速开始:
bash
scripts/create_worktree.sh**就是这样!**脚本让Worktrees的使用简单且实用。