git-worktree-remove
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Worktree Remove
Git Worktree 移除
Overview
概述
This skill removes git worktrees from the directory and ensures all traces are completely cleaned up. It can remove a specific worktree by name or present an interactive selection menu when no name is provided.
.worktrees/该工具会从目录中移除Git worktree,并确保彻底清理所有相关痕迹。它可以通过名称移除指定的worktree,若未提供名称则会显示交互式选择菜单。
.worktrees/When to Use This Skill
适用场景
Use this skill when the user needs to:
- Remove a temporary worktree after completing work
- Clean up old worktrees that are no longer needed
- Free up disk space by removing unused worktrees
- Maintain a clean worktree directory
当用户需要以下操作时可使用该工具:
- 完成工作后移除临时worktree
- 清理不再需要的旧worktree
- 通过移除未使用的worktree释放磁盘空间
- 保持worktree目录整洁
How It Works
工作原理
The skill uses a bash script () that:
scripts/remove_worktree.sh- Validates the current directory is a git repository
- Sanitizes the worktree name (replaces with
/) to match-behaviorgit-worktree-create - Checks if directory exists
.worktrees/ - If no worktree name provided, lists all worktrees and prompts for selection
- Confirms deletion with the user
- Removes the worktree using
git worktree remove --force - Cleans up any remaining directory files
- Prunes git's worktree administrative data
- Removes directory if it becomes empty
.worktrees/
该工具使用bash脚本(),执行以下步骤:
scripts/remove_worktree.sh- 验证当前目录是否为Git仓库
- 对worktree名称进行清理(将替换为
/),与-的行为保持一致git-worktree-create - 检查目录是否存在
.worktrees/ - 若未提供worktree名称,则列出所有worktree并提示用户选择
- 向用户确认删除操作
- 使用命令移除worktree
git worktree remove --force - 清理剩余的目录文件
- 修剪Git的worktree管理数据
- 若目录变为空则将其移除
.worktrees/
Usage
使用方法
Remove a specific worktree by name:
通过名称移除指定的worktree:
bash
scripts/remove_worktree.sh <worktree-name>Examples:
bash
undefinedbash
scripts/remove_worktree.sh <worktree-name>示例:
bash
undefinedRemove worktree by directory name
通过目录名称移除worktree
scripts/remove_worktree.sh hotfix-123
scripts/remove_worktree.sh hotfix-123
Remove worktree using original branch name (will be auto-sanitized)
使用原始分支名称移除worktree(会自动进行名称清理)
scripts/remove_worktree.sh user.name/AOF-123
scripts/remove_worktree.sh user.name/AOF-123
This will remove: .worktrees/user.name-AOF-123
此命令将移除:.worktrees/user.name-AOF-123
Both approaches work the same:
以下两种方式效果相同:
scripts/remove_worktree.sh feature/new-feature
scripts/remove_worktree.sh feature-new-feature
undefinedscripts/remove_worktree.sh feature/new-feature
scripts/remove_worktree.sh feature-new-feature
undefinedInteractive selection (no arguments):
交互式选择(无参数):
bash
scripts/remove_worktree.shThis will display a numbered list of available worktrees and prompt you to select one.
bash
scripts/remove_worktree.sh执行此命令会显示可用worktree的编号列表,并提示用户选择要移除的项。
Interactive Selection
交互式选择
When called without arguments, the script displays:
No worktree name provided. Available worktrees:
[1] feature-branch
[2] hotfix-123
[3] bugfix-456
Select worktree to remove (number or name):You can respond with either:
- A number (e.g., )
2 - A worktree name (e.g., )
hotfix-123
当不带参数调用脚本时,会显示如下内容:
No worktree name provided. Available worktrees:
[1] feature-branch
[2] hotfix-123
[3] bugfix-456
Select worktree to remove (number or name):你可以通过以下两种方式回应:
- 输入编号(例如:)
2 - 输入worktree名称(例如:)
hotfix-123
Safety Features
安全特性
- Confirmation prompt: Always asks for confirmation before removing
- Validation: Checks that the worktree exists before attempting removal
- Error handling: Clear error messages if worktree not found or not in a git repo
- Force removal: Uses flag to handle worktrees with uncommitted changes
--force
- 确认提示:移除前始终会向用户确认
- 有效性验证:在尝试移除前检查worktree是否存在
- 错误处理:若worktree不存在或当前目录不是Git仓库,会显示清晰的错误信息
- 强制移除:使用标志处理包含未提交更改的worktree
--force
Complete Cleanup
彻底清理
The script ensures no traces remain by:
- Removing the worktree directory ()
.worktrees/<worktree-name> - Removing git's internal worktree tracking data
- Pruning stale worktree entries
- Cleaning up the directory if empty
.worktrees/
该脚本通过以下操作确保无任何痕迹残留:
- 移除worktree目录()
.worktrees/<worktree-name> - 移除Git内部的worktree跟踪数据
- 修剪过期的worktree条目
- 若目录为空则将其清理
.worktrees/
Error Handling
错误处理
The script will error if:
- Not in a git repository
- No directory exists
.worktrees/ - Specified worktree doesn't exist
- No worktrees available when using interactive mode
- User cancels the confirmation prompt
在以下情况脚本会报错:
- 当前目录不是Git仓库
- 目录不存在
.worktrees/ - 指定的worktree不存在
- 使用交互式模式时无可用worktree
- 用户取消确认提示
Example Workflow
示例流程
bash
undefinedbash
undefinedUser has finished work on hotfix-123 worktree
用户完成hotfix-123 worktree的工作后
$ scripts/remove_worktree.sh hotfix-123
⚠️ About to remove worktree: .worktrees/hotfix-123
Are you sure? (y/N): y
Removing worktree...
✅ Worktree removed successfully
Pruning worktree administrative data...
✅ Worktree 'hotfix-123' has been completely removed
All traces of the worktree have been cleaned up.
undefined$ scripts/remove_worktree.sh hotfix-123
⚠️ About to remove worktree: .worktrees/hotfix-123
Are you sure? (y/N): y
Removing worktree...
✅ Worktree removed successfully
Pruning worktree administrative data...
✅ Worktree 'hotfix-123' has been completely removed
All traces of the worktree have been cleaned up.
undefined