跳到正文
Skillpedia
部署与云(DevOps)33k · #8856

worktree-cleaner

它能做什么

这项技能清点仓库中的 git worktree,显示每个 worktree 包含的内容,并只删除你批准的部分。适合项目因并行任务积累了大量 worktree 的开发者。

适用场景

  • ✓当 git worktree list 的列表变得很长,你想整理时。
  • ✓当智能体或并行工作流为每个任务创建了 worktree,却从未删除过时。
  • ✓当你想知道哪些 worktree 已合并、已过期,或仍有未推送的工作的时候。

技能链接

https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/git/worktree-cleaner/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/davila7/claude-code-templates/blob/main/cli-tool/components/skills/git/worktree-cleaner/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的仓库里有几十个遗留的 worktree。请告诉我哪些可以安全删除,并在删除前先问我。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会为每个 worktree 收集证据,例如合并状态、拉取请求状态和未提交的改动,推荐要删除的内容,并且只在你批准每一步后才执行删除。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Worktree Cleaner

Agents and parallel workflows create one git worktree per task, and nobody removes them. This skill finds them, shows the user what each one holds, and removes only what the user approves, checking the result after every step.

The user decides. You gather evidence, recommend, and execute exactly what was approved. Nothing is removed in bulk without the user seeing the list first.

Hard rules

  1. Ask before every destructive command. git worktree remove, git worktree prune, git worktree unlock and git branch -d/-D each need the user's approval for that worktree or that named group. Approval of one group does not carry to the next.
  2. Never --force on work that would be lost. If a worktree has uncommitted changes, untracked files, or commits that exist nowhere else, show the exact files and commits and ask a separate, explicit question ("remove it and lose these changes?"). A general "yes, clean everything" is not that confirmation.
  3. Never touch the main worktree, the worktree you are running in, or a worktree with live processes inside it (another Claude Code session, an editor, a dev server). Report them and move on.
  4. Never delete a remote branch (git push --delete, git push origin :branch, API calls). Remote branches are out of scope; mention them in the report if they look stale.
  5. Deleting the local branch is a separate question, asked after the worktree is gone. Removing a worktree never deletes its branch.
  6. Re-check right before acting. State changes while you talk to the user: re-run the inventory for that one path immediately before removing it, and stop if the verdict got worse.
  7. Verify after every action and report what you checked. If a check fails, stop and tell the user; do not retry with stronger flags.

Step 1: Inventory (read-only)

Run the bundled script from anywhere inside the repository. It changes nothing; --fetch only runs git fetch --prune so the merged/pushed checks use fresh remote refs. Ask before fetching if the user is offline or on a metered connection; otherwise fetch.

bash "${CLAUDE_SKILL_DIR}/scripts/worktree-inventory.sh" --fetch

If CLAUDE_SKILL_DIR is not set, the script is at .claude/skills/worktree-cleaner/scripts/worktree-inventory.sh (project install) or ~/.claude/skills/worktree-cleaner/scripts/worktree-inventory.sh (user install).

Flags: --path <worktree> reports a single worktree (use it for the re-check in rule 6; it exits 3 with an error when no worktree matches, so a typo never looks like an empty result), --no-gh skips the GitHub CLI PR lookup, --repo <dir> targets another repository. The fetch never prompts for credentials; if it cannot authenticate it reports fetch: FAILED.

It prints a header (default_branch, whether the fetch worked, whether gh and git merge-tree were available) and then one block per worktree. Key fields:

Field Meaning
modified_tracked, untracked_files, changed_paths Uncommitted work. git worktree remove refuses these without --force.
ignored_local_files Gitignored files and directories that are not regenerable artifacts (dependencies, build output and caches such as node_modules/, dist/, .venv/ are left out): .env, .dev.vars, local settings, keys, a whole secrets/ directory. git worktree remove deletes ignored files without warning, so these must be shown to the user.
commits_not_on_any_remote Commits reachable from this worktree's HEAD and from no remote-tracking ref. On a branch, removing the worktree keeps them on the branch, but deleting the branch afterwards loses them. On a detached worktree there is no branch: removing it makes them unreachable (the script marks it AT_RISK).
git_error git could not read the worktree (broken .git file, safe.directory ownership, permissions). Nothing else in the block is reliable.
head_merged_into_default HEAD is an ancestor of the default branch (merge or fast-forward).
content_already_in_default Merging HEAD into the default branch would change nothing (git merge-tree). This catches squash and rebase merges, where the commits differ but the content landed.
pr #N STATE url from gh, none, or unknown when gh is not available.
processes_inside Running processes whose working directory is inside the worktree.
claude_session_hours_ago Hours since a Claude Code
常见问题

它会删除我的工作吗?

每个破坏性命令执行前,这项技能都会请求批准;如果有未提交或未保存的提交,删除前还会单独询问你。

它会删除远程分支吗?

不会。这项技能不处理远程分支,不过可能会在报告中提到过期的远程分支。

它会动我正在使用的 worktree 吗?

不会。它从不触碰主 worktree、它自己所在的 worktree,或任何有进程在运行的 worktree。

详情

作者的原始描述

Inventory the git worktrees of the current repository, classify each one with evidence (merged, PR state, uncommitted or unpushed work, locked, stale, in use by a running session) and remove them one at a time or by group, always asking the user first and verifying every removal. Use when worktrees have piled up (for example one per Claude Code task or thread), when `git worktree list` is long, or when the user asks to clean up, prune or tidy worktrees.

repo: davila7/claude-code-templates

热度

GitHub 星标
33k
排名
#8856
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能