AgentSkillsCN

git-worktree-cleanup

清理已合并分支或已关闭 PR 的 Git 工作树。当用户请求清理工作树、移除旧的工作树、修剪过期分支,或在合并 PR 后整理代码时,可使用此技能。

SKILL.md
--- frontmatter
name: git-worktree-cleanup
description: Clean up git worktrees whose branches have been merged or whose PRs are closed. Use when the user asks to clean up worktrees, remove old worktrees, prune stale branches, or tidy up after merged PRs.

Git Worktree Cleanup

Remove worktrees whose feature branches have been merged into develop or whose PRs are closed/merged.

Quick cleanup (merged branches)

Run the cleanup script from the repository root:

bash
bash scripts/worktree/cleanup-worktrees.sh

This script will:

  1. Fetch latest from remote
  2. Find all worktrees with branches merged into origin/develop
  3. Remove those worktrees and delete the local branches
  4. Run git worktree prune to clean up stale references
  5. Report what was removed

Targeted cleanup (single worktree)

To remove a specific worktree manually:

bash
git worktree remove ../worktrees/feature-AB-<ticket>-<short-desc>
git branch -d feature/AB-<ticket>-<short-desc>

Enhanced cleanup with PR status check

If the cleanup script misses worktrees (e.g. squash-merged PRs where the branch doesn't show as merged in git), use GitHub MCP to verify:

  1. Run git worktree list to get all active worktrees.
  2. For each worktree branch, use user-github MCP list_pull_requests (state: closed, head: branch name) to check if a PR was merged.
  3. If the PR state is merged, remove the worktree and branch:
    bash
    git worktree remove <worktree-path>
    git branch -D <branch-name>
    
  4. Run git worktree prune at the end.

Important notes

  • The script only removes worktrees under ../worktrees/ — it never touches the main working directory.
  • Only branches matching feature/*, bugfix/*, qol/*, or fix/* are considered for cleanup.
  • If a branch has uncommitted changes, git worktree remove will fail safely — no data loss.
  • Use git branch -D (force) for squash-merged branches since git won't recognize them as merged.