git-worktree-remove

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Worktree Remove

Git Worktree 移除

Overview

概述

This skill removes git worktrees from the
.worktrees/
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,若未提供名称则会显示交互式选择菜单。

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 (
scripts/remove_worktree.sh
) that:
  1. Validates the current directory is a git repository
  2. Sanitizes the worktree name (replaces
    /
    with
    -
    ) to match
    git-worktree-create
    behavior
  3. Checks if
    .worktrees/
    directory exists
  4. If no worktree name provided, lists all worktrees and prompts for selection
  5. Confirms deletion with the user
  6. Removes the worktree using
    git worktree remove --force
  7. Cleans up any remaining directory files
  8. Prunes git's worktree administrative data
  9. Removes
    .worktrees/
    directory if it becomes empty
该工具使用bash脚本(
scripts/remove_worktree.sh
),执行以下步骤:
  1. 验证当前目录是否为Git仓库
  2. 对worktree名称进行清理(将
    /
    替换为
    -
    ),与
    git-worktree-create
    的行为保持一致
  3. 检查
    .worktrees/
    目录是否存在
  4. 若未提供worktree名称,则列出所有worktree并提示用户选择
  5. 向用户确认删除操作
  6. 使用
    git worktree remove --force
    命令移除worktree
  7. 清理剩余的目录文件
  8. 修剪Git的worktree管理数据
  9. .worktrees/
    目录变为空则将其移除

Usage

使用方法

Remove a specific worktree by name:

通过名称移除指定的worktree:

bash
scripts/remove_worktree.sh <worktree-name>
Examples:
bash
undefined
bash
scripts/remove_worktree.sh <worktree-name>
示例:
bash
undefined

Remove 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
undefined
scripts/remove_worktree.sh feature/new-feature scripts/remove_worktree.sh feature-new-feature
undefined

Interactive selection (no arguments):

交互式选择(无参数):

bash
scripts/remove_worktree.sh
This 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

安全特性

  1. Confirmation prompt: Always asks for confirmation before removing
  2. Validation: Checks that the worktree exists before attempting removal
  3. Error handling: Clear error messages if worktree not found or not in a git repo
  4. Force removal: Uses
    --force
    flag to handle worktrees with uncommitted changes
  1. 确认提示:移除前始终会向用户确认
  2. 有效性验证:在尝试移除前检查worktree是否存在
  3. 错误处理:若worktree不存在或当前目录不是Git仓库,会显示清晰的错误信息
  4. 强制移除:使用
    --force
    标志处理包含未提交更改的worktree

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
    .worktrees/
    directory if empty
该脚本通过以下操作确保无任何痕迹残留:
  • 移除worktree目录(
    .worktrees/<worktree-name>
  • 移除Git内部的worktree跟踪数据
  • 修剪过期的worktree条目
  • .worktrees/
    目录为空则将其清理

Error Handling

错误处理

The script will error if:
  • Not in a git repository
  • No
    .worktrees/
    directory exists
  • Specified worktree doesn't exist
  • No worktrees available when using interactive mode
  • User cancels the confirmation prompt
在以下情况脚本会报错:
  • 当前目录不是Git仓库
  • .worktrees/
    目录不存在
  • 指定的worktree不存在
  • 使用交互式模式时无可用worktree
  • 用户取消确认提示

Example Workflow

示例流程

bash
undefined
bash
undefined

User 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