git-worktrees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktrees - Parallel Development Workflow

Git Worktrees - 并行开发工作流

Enable parallel feature development by creating isolated git worktree directories, then executing the same plan across multiple workspaces simultaneously.
通过创建独立的Git工作树目录,然后在多个工作区中同时执行同一计划,实现并行功能开发。

Workflow Overview

工作流概述

Phase 1: Prepare Worktrees

阶段1:准备工作树

Create N isolated worktrees for parallel development:
bash
undefined
为并行开发创建N个独立的工作树:
bash
undefined

Create worktree directory

创建工作树目录

mkdir -p trees
mkdir -p trees

Create worktrees (example: 3 parallel workspaces)

创建工作树(示例:3个并行工作区)

git worktree add -b feature-1 ./trees/feature-1 git worktree add -b feature-2 ./trees/feature-2 git worktree add -b feature-3 ./trees/feature-3
git worktree add -b feature-1 ./trees/feature-1 git worktree add -b feature-2 ./trees/feature-2 git worktree add -b feature-3 ./trees/feature-3

Verify worktrees

验证工作树

git worktree list

Each worktree is a complete, isolated copy of the codebase. All worktrees share git history but have independent working directories.
git worktree list

每个工作树都是代码库的完整独立副本。所有工作树共享Git历史记录,但拥有独立的工作目录。

Phase 2: Execute Tasks in Parallel

阶段2:并行执行任务

Launch N subagents (one per worktree) using the Task tool:
  1. Each agent independently implements the plan in their workspace
  2. Agents produce RESULTS.md summarising their changes
  3. Compare results and cherry-pick the best implementation
Example Task invocation:
Use the Task tool to launch a general-purpose agent with:
- prompt: "Working in trees/feature-1, implement [plan]. Write a RESULTS.md summarising changes."
- run_in_background: true (for parallel execution)
使用Task工具启动N个子代理(每个工作树对应一个):
  1. 每个代理在各自的工作区中独立执行计划
  2. 代理生成RESULTS.md文件总结其更改
  3. 比较结果并挑选最佳实现方案
任务调用示例:
使用Task工具启动通用代理,参数如下:
- prompt: "在trees/feature-1目录中,实现[计划]。编写RESULTS.md文件总结更改。"
- run_in_background: true(用于并行执行)

When to Use

适用场景

  • Experimental implementations - Compare different approaches
  • A/B testing code changes - Try multiple solutions simultaneously
  • Reducing iteration time - Run multiple attempts in parallel
  • Complex refactoring - Test different strategies concurrently
  • 实验性实现 - 比较不同方案
  • 代码变更A/B测试 - 同时尝试多种解决方案
  • 缩短迭代时间 - 并行运行多次尝试
  • 复杂重构 - 同时测试不同策略

Best Practices

最佳实践

  1. Keep worktree count reasonable (2-4) to manage cognitive load
  2. Focus on code changes only - No tests during parallel execution
  3. Clean up worktrees after selecting the winning implementation:
    bash
    git worktree remove ./trees/feature-1
    git worktree prune
  4. Cherry-pick the winner to your main branch:
    bash
    git cherry-pick feature-2
    # or merge the branch
    git merge feature-2
  1. 控制工作树数量(2-4个)以降低认知负担
  2. 仅关注代码变更 - 并行执行期间不进行测试
  3. 选择最优实现后清理工作树
    bash
    git worktree remove ./trees/feature-1
    git worktree prune
  4. 将最优实现挑选到主分支
    bash
    git cherry-pick feature-2
    # 或合并分支
    git merge feature-2

Directory Structure

目录结构

project/
├── trees/
│   ├── feature-1/     # Worktree 1 (full codebase copy)
│   │   └── RESULTS.md
│   ├── feature-2/     # Worktree 2 (full codebase copy)
│   │   └── RESULTS.md
│   └── feature-3/     # Worktree 3 (full codebase copy)
│       └── RESULTS.md
└── (main working directory)
project/
├── trees/
│   ├── feature-1/     # 工作树1(完整代码库副本)
│   │   └── RESULTS.md
│   ├── feature-2/     # 工作树2(完整代码库副本)
│   │   └── RESULTS.md
│   └── feature-3/     # 工作树3(完整代码库副本)
│       └── RESULTS.md
└── (主工作目录)

RESULTS.md Template

RESULTS.md 模板

Each agent should produce a summary in this format:
markdown
undefined
每个代理应按照以下格式生成总结:
markdown
undefined

Results - [Feature Name]

结果 - [功能名称]

Changes Made

已做变更

  • List of files modified/created
  • Summary of approach taken
  • 修改/创建的文件列表
  • 采用方案的总结

Key Decisions

关键决策

  • Design choices made
  • Trade-offs considered
  • 做出的设计选择
  • 考虑的权衡因素

Testing Notes

测试说明

  • How to verify the implementation
  • Known limitations
undefined
  • 如何验证实现
  • 已知限制
undefined