worktrees

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktrees for Parallel Development

借助Git Worktrees实现并行开发

Overview

概述

This skill enables parallel development by using git worktrees. Each worktree provides an isolated working directory with its own branch, allowing multiple agents to work on different features simultaneously without conflicts.
本技能通过Git Worktrees实现并行开发。每个工作区都有独立的工作目录和专属分支,允许多个Agent同时开发不同功能,且不会产生冲突。

When to Use This Skill

适用场景

Use this skill when:
  • User explicitly requests to work in a new worktree (e.g., "Work in a new worktree")
  • User wants to develop a feature in isolation while preserving the main working directory
  • Multiple agents need to work on different tasks in parallel
在以下场景中使用本技能:
  • 用户明确要求在新工作区中工作(例如:“在新工作区中工作”)
  • 用户希望在保留主工作目录的同时,独立开发某个功能
  • 多个Agent需要同时并行处理不同任务

Workflow

工作流程

1. Determine Branch Name

1. 确定分支名称

Choose a descriptive branch name for the feature or task to be worked on. The branch name should follow standard git naming conventions (lowercase, hyphen-separated, e.g.,
add-user-authentication
,
fix-login-bug
).
为要开发的功能或任务选择一个描述性的分支名称。分支名称应遵循Git的标准命名规范(小写、连字符分隔,例如:
add-user-authentication
fix-login-bug
)。

2. Create Worktree

2. 创建工作区

Create a new worktree in the
.worktrees/
directory within the current project:
bash
git worktree add .worktrees/<branch-name> -b <branch-name>
This command:
  • Creates a new directory at
    .worktrees/<branch-name>
  • Creates and checks out a new branch named
    <branch-name>
  • Links the worktree to the current repository
在当前项目的
.worktrees/
目录下创建新工作区:
bash
git worktree add .worktrees/<branch-name> -b <branch-name>
该命令的作用:
  • .worktrees/<branch-name>
    路径下创建新目录
  • 创建并检出名为
    <branch-name>
    的新分支
  • 将该工作区与当前代码仓库关联

3. Switch to Worktree

3. 切换到工作区

Change the working directory to the newly created worktree:
bash
cd .worktrees/<branch-name>
切换到刚创建的工作区目录:
bash
cd .worktrees/<branch-name>

4. Work in Isolation

4. 独立开发

Proceed with development tasks in the worktree. This environment is completely isolated from the main working directory, allowing independent work without interference.
All standard git operations (commit, push, pull, etc.) work normally within the worktree.
Note: If this project runs services (web apps, docker-compose, etc.), see apps.md for setup steps including environment file copying, port allocation, and service startup.
在该工作区中进行开发任务。此环境与主工作区完全隔离,可独立工作而不会互相干扰。
所有标准Git操作(commit、push、pull等)在工作区内均可正常执行。
注意: 如果项目运行有服务(Web应用、docker-compose等),请查看apps.md了解设置步骤,包括环境文件复制、端口分配和服务启动。

5. List Active Worktrees (Optional)

5. 列出活跃工作区(可选)

To view all active worktrees:
bash
git worktree list
This displays all worktrees, their paths, and the branches they're on.
要查看所有活跃的工作区:
bash
git worktree list
该命令会显示所有工作区的路径及其对应的分支。

6. Remove Worktree (Optional)

6. 删除工作区(可选)

When you're done with a worktree, you can remove it:
bash
git worktree remove .worktrees/<branch-name>
Note: Don't automatically remove worktrees. Leave that decision to the user. If the worktree is running services (see apps.md), make sure to stop those services first before removing the worktree.
当工作区使用完毕后,可将其删除:
bash
git worktree remove .worktrees/<branch-name>
注意: 请勿自动删除工作区,该决策应由用户做出。如果工作区正在运行服务(参考apps.md),请先停止这些服务再删除工作区。

Important Notes

重要说明

  • The
    .worktrees/
    directory should be added to
    .gitignore
    if not already present
  • Each worktree maintains its own working directory but shares the same git repository
  • Worktrees enable true parallel development without the need for stashing or branch switching
  • After creating and switching to a worktree, inform the user of the new working directory path
  • 如果
    .worktrees/
    目录尚未添加到
    .gitignore
    中,应将其加入
  • 每个工作区拥有独立的工作目录,但共享同一个Git仓库
  • Worktrees无需暂存或切换分支即可实现真正的并行开发
  • 创建并切换到工作区后,需告知用户新工作目录的路径