git-worktree-create
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktree Create
Git Worktree 创建
Overview
概述
This skill creates git worktrees in a standardized directory structure, enabling work on multiple branches simultaneously without stashing or committing current changes. Each worktree is created in for easy organization and discovery.
.worktrees/.worktrees/<branch-name>本工具会在标准化的目录结构中创建Git工作树,无需暂存或提交当前更改即可同时处理多个分支。每个工作树都会创建在路径下,便于组织和查找。
.worktrees/.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 () that:
scripts/create_worktree.sh- Validates the current directory is a git repository
- Sanitizes the branch name (replaces with
/) for use as a directory name- - Runs to ensure remote branches are up-to-date
git fetch --all - Checks if the branch exists locally, if not checks remote repositories
- For multiple remotes, defaults to or prompts user to select
origin - Creates directory if it doesn't exist
.worktrees/ - Creates a worktree at
.worktrees/<sanitized-branch-name> - Copies Claude configuration files (CLAUDE.md, .claude/) if they are not tracked in git
- Prints the absolute path to the worktree for easy navigation
本工具通过一个bash脚本()实现功能,具体步骤如下:
scripts/create_worktree.sh- 验证当前目录是否为Git仓库
- 清理分支名称(将替换为
/),以便用作目录名- - 执行确保远程分支为最新状态
git fetch --all - 检查分支是否在本地存在,若不存在则检查远程仓库
- 若存在多个远程仓库,默认使用或提示用户选择
origin - 若目录不存在则创建该目录
.worktrees/ - 在路径下创建工作树
.worktrees/<清理后的分支名> - 若Claude配置文件(CLAUDE.md、.claude/)未被Git追踪,则将其复制到工作树中
- 打印工作树的绝对路径,方便导航
Usage
使用方法
To create a worktree for a branch, run:
bash
scripts/create_worktree.sh <branch-name>Arguments:
- : Name of the branch (can be local or remote)
<branch-name>
Examples:
bash
undefined要为某个分支创建工作树,运行以下命令:
bash
scripts/create_worktree.sh <branch-name>参数说明:
- :分支名称(可以是本地分支或远程分支)
<branch-name>
示例:
bash
undefinedCreate 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
undefinedscripts/create_worktree.sh user.name/AOF-123
undefinedError 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 directory at the repository root:
.worktrees/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/
└── [主工作目录文件]此约定可让所有临时工作树保持有序,便于查找。
注意: 包含斜杠()的分支名称会自动被清理,将斜杠替换为连字符(),以避免嵌套目录结构。当清理操作发生时,脚本会通知用户。
/-