git-worktree
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktree Manager
Git工作区管理器
Overview
概述
This skill provides a unified interface for managing git worktrees, enabling isolated parallel development. Worktrees allow you to have multiple branches checked out simultaneously in separate directories.
Key features:
- Automatic file copying from main repo to new worktrees
.env - Unified storage in directory
.worktrees/ - Cleanup of merged and stale worktrees
本Skill提供了一个统一的Git工作区管理界面,支持隔离式并行开发。工作区允许你在不同目录中同时检出多个分支。
核心特性:
- 自动将主仓库的.env文件复制到新工作区
- 统一存储在.worktrees/目录下
- 清理已合并和过期的工作区
When to Use This Skill
适用场景
- Creating isolated environments for feature development
- Working on multiple branches simultaneously
- Reviewing PRs without stashing current work
- Cleaning up completed feature branches
- 为功能开发创建隔离环境
- 同时处理多个分支
- 无需暂存当前工作即可评审PR
- 清理已完成的功能分支
Critical: Always Use the Manager Script
重要提示:请始终使用管理脚本
Always use the worktree-manager.sh script rather than raw commands. The script handles:
git worktree- Automatic file copying to new worktrees
.env - Consistent storage in directory
.worktrees/ - Proper management
.gitignore
请始终使用worktree-manager.sh脚本,而非原生命令。该脚本可处理:
git worktree- 自动将.env文件复制到新工作区
- 在.worktrees/目录下统一存储
- 正确管理.gitignore文件
Core Commands
核心命令
All operations use the unified script:
worktree-manager.shbash
bash scripts/worktree-manager.sh <command> [options]所有操作均通过统一的脚本执行:
worktree-manager.shbash
bash scripts/worktree-manager.sh <command> [options]Create Worktree
创建工作区
bash
worktree-manager.sh create <branch-name> [source-branch]Creates a new worktree in . If the branch exists, it checks it out. If not, creates a new branch from the source (defaults to main/master).
.worktrees/<branch-name>bash
worktree-manager.sh create <branch-name> [source-branch]在目录下创建新工作区。若分支已存在,则检出该分支;若不存在,则基于源分支(默认是main/master)创建新分支。
.worktrees/<branch-name>List Worktrees
列出工作区
bash
worktree-manager.sh listShows all worktrees with their branch, commit, and status (clean/dirty/missing).
bash
worktree-manager.sh list显示所有工作区的分支、提交记录和状态(干净/有修改/缺失)。
Switch Worktree
切换工作区
bash
worktree-manager.sh switch <branch-name|path>Provides information for switching to a worktree by branch name or path.
bash
worktree-manager.sh switch <branch-name|path>提供通过分支名称或路径切换到对应工作区的相关信息。
Cleanup Worktrees
清理工作区
bash
worktree-manager.sh cleanup [--force]Identifies and removes:
- Worktrees with merged branches
- Worktrees with deleted remote branches
- Missing worktree directories
Use to skip confirmation prompt.
--forcebash
worktree-manager.sh cleanup [--force]识别并移除以下工作区:
- 对应分支已合并的工作区
- 对应远程分支已删除的工作区
- 目录已缺失的工作区
使用参数可跳过确认提示。
--forceCopy Environment Files
复制环境文件
bash
worktree-manager.sh copy-env [worktree-path|branch-name]Copies files (excluding ) from the main repo to a worktree. Useful for:
.env*.env.example- Adding env files to existing worktrees created before this feature
- Refreshing env files after main repo changes
If run inside a worktree without arguments, copies to current location.
bash
worktree-manager.sh copy-env [worktree-path|branch-name]将主仓库中的.env*文件(排除.env.example)复制到指定工作区。适用于:
- 为该特性推出前创建的现有工作区添加环境文件
- 主仓库环境文件更新后同步到工作区
若在工作区目录内执行且不带参数,则会将环境文件复制到当前目录。
Storage
存储位置
Worktrees are stored in within the repository root. This directory is automatically added to .
.worktrees/.gitignore工作区统一存储在仓库根目录下的目录中,该目录会自动被添加到.gitignore文件中。
.worktrees/Example Workflow
示例工作流
bash
undefinedbash
undefinedStart new feature
启动新功能开发
worktree-manager.sh create feature-auth
worktree-manager.sh create feature-auth
Work in the new worktree
进入新工作区开展工作
cd .worktrees/feature-auth
cd .worktrees/feature-auth
List all worktrees
列出所有工作区
worktree-manager.sh list
worktree-manager.sh list
When done, clean up
完成后清理工作区
worktree-manager.sh cleanup
undefinedworktree-manager.sh cleanup
undefinedTroubleshooting
故障排除
Missing .env files in existing worktrees
现有工作区缺失.env文件
If you have existing worktrees created before the automatic env copying feature:
bash
bash scripts/worktree-manager.sh copy-env feature-branchOr from within the worktree directory:
bash
bash scripts/worktree-manager.sh copy-env若你有在自动复制环境文件特性推出前创建的现有工作区,可执行以下命令:
bash
bash scripts/worktree-manager.sh copy-env feature-branch或在工作区目录内执行:
bash
bash scripts/worktree-manager.sh copy-env