git-worktree-create

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktree Create

Git Worktree 创建

Overview

概述

This skill creates git worktrees in a standardized
.worktrees/
directory structure, enabling work on multiple branches simultaneously without stashing or committing current changes. Each worktree is created in
.worktrees/<branch-name>
for easy organization and discovery.
本工具会在标准化的
.worktrees/
目录结构中创建Git工作树,无需暂存或提交当前更改即可同时处理多个分支。每个工作树都会创建在
.worktrees/<branch-name>
路径下,便于组织和查找。

When to Use This Skill

使用场景

Use this skill when the user needs to:
  • Work on a different branch while preserving uncommitted changes in the current working directory
  • Create a temporary worktree for quick fixes or hotfixes
  • Review or test another branch without affecting current work
  • Work on multiple branches simultaneously
当用户需要以下操作时,可使用本工具:
  • 在保留当前工作目录未提交更改的同时,处理其他分支
  • 创建临时工作树以进行快速修复或热修复
  • 审核或测试其他分支,且不影响当前工作
  • 同时处理多个分支

How It Works

工作原理

The skill uses a bash script (
scripts/create_worktree.sh
) that:
  1. Validates the current directory is a git repository
  2. Sanitizes the branch name (replaces
    /
    with
    -
    ) for use as a directory name
  3. Runs
    git fetch --all
    to ensure remote branches are up-to-date
  4. Checks if the branch exists locally, if not checks remote repositories
  5. For multiple remotes, defaults to
    origin
    or prompts user to select
  6. Creates
    .worktrees/
    directory if it doesn't exist
  7. Creates a worktree at
    .worktrees/<sanitized-branch-name>
  8. Copies Claude configuration files (CLAUDE.md, .claude/) if they are not tracked in git
  9. Prints the absolute path to the worktree for easy navigation
本工具通过一个bash脚本(
scripts/create_worktree.sh
)实现功能,具体步骤如下:
  1. 验证当前目录是否为Git仓库
  2. 清理分支名称(将
    /
    替换为
    -
    ),以便用作目录名
  3. 执行
    git fetch --all
    确保远程分支为最新状态
  4. 检查分支是否在本地存在,若不存在则检查远程仓库
  5. 若存在多个远程仓库,默认使用
    origin
    或提示用户选择
  6. .worktrees/
    目录不存在则创建该目录
  7. .worktrees/<清理后的分支名>
    路径下创建工作树
  8. 若Claude配置文件(CLAUDE.md、.claude/)未被Git追踪,则将其复制到工作树中
  9. 打印工作树的绝对路径,方便导航

Usage

使用方法

To create a worktree for a branch, run:
bash
scripts/create_worktree.sh <branch-name>
Arguments:
  • <branch-name>
    : Name of the branch (can be local or remote)
Examples:
bash
undefined
要为某个分支创建工作树,运行以下命令:
bash
scripts/create_worktree.sh <branch-name>
参数说明:
  • <branch-name>
    :分支名称(可以是本地分支或远程分支)
示例:
bash
undefined

Create worktree for a local branch

为本地分支创建工作树

scripts/create_worktree.sh feature/new-feature
scripts/create_worktree.sh feature/new-feature

Create worktree for a remote branch

为远程分支创建工作树

scripts/create_worktree.sh hotfix/urgent-fix
scripts/create_worktree.sh hotfix/urgent-fix

Branch names with slashes are automatically sanitized

包含斜杠的分支名称会自动被清理

Branch: user.name/AOF-123 → Worktree: .worktrees/user.name-AOF-123

分支:user.name/AOF-123 → 工作树:.worktrees/user.name-AOF-123

scripts/create_worktree.sh user.name/AOF-123
undefined
scripts/create_worktree.sh user.name/AOF-123
undefined

Error Handling

错误处理

The script will error if:
  • Not in a git repository
  • Branch doesn't exist locally or on any remote
  • A worktree already exists for that branch at
    .worktrees/<branch-name>
当出现以下情况时,脚本会报错:
  • 当前目录不是Git仓库
  • 分支在本地或任何远程仓库中都不存在
  • 该分支的工作树已存在于
    .worktrees/<branch-name>
    路径下

Claude Configuration

Claude配置

The script ensures Claude configuration is available in the worktree:
  • If tracked in git: Configuration files (CLAUDE.md, .claude/) will automatically be present in the worktree
  • If not tracked: Configuration files are copied from the repository root to the worktree
This ensures a consistent Claude setup across all worktrees.
脚本会确保工作树中可使用Claude配置:
  • 若已被Git追踪:配置文件(CLAUDE.md、.claude/)会自动出现在工作树中
  • 若未被Git追踪:配置文件会从仓库根目录复制到工作树中
这确保了所有工作树中的Claude设置保持一致。

Worktree Organization

工作树组织

All worktrees are created in the
.worktrees/
directory at the repository root:
repo-root/
├── .worktrees/
│   ├── feature-branch/              # Worktree for feature-branch
│   ├── hotfix-123/                  # Worktree for hotfix-123
│   ├── user.name-AOF-456/           # Worktree for user.name/AOF-456 (sanitized)
│   └── bugfix-fix-critical-issue/   # Worktree for bugfix/fix-critical-issue (sanitized)
├── .git/
└── [main working directory files]
This convention keeps all temporary worktrees organized and easy to find.
Note: Branch names containing slashes (
/
) are automatically sanitized by replacing slashes with hyphens (
-
) to avoid nested directory structures. The script will inform you when sanitization occurs.
所有工作树都会创建在仓库根目录的
.worktrees/
目录中:
repo-root/
├── .worktrees/
│   ├── feature-branch/              # 对应feature-branch的工作树
│   ├── hotfix-123/                  # 对应hotfix-123的工作树
│   ├── user.name-AOF-456/           # 对应user.name/AOF-456的工作树(已清理)
│   └── bugfix-fix-critical-issue/   # 对应bugfix/fix-critical-issue的工作树(已清理)
├── .git/
└── [主工作目录文件]
此约定可让所有临时工作树保持有序,便于查找。
注意: 包含斜杠(
/
)的分支名称会自动被清理,将斜杠替换为连字符(
-
),以避免嵌套目录结构。当清理操作发生时,脚本会通知用户。