AgentSkillsCN

cleanup-worktree

当工作完成、PR 合并后,可通过清理工作树来优化项目环境:先拉取主分支,解除 HEAD 绑定,最后删除当前工作分支。

SKILL.md
--- frontmatter
name: cleanup-worktree
description: Use when work is complete and PR is merged to clean up the worktree - fetches main, detaches HEAD, and deletes the working branch

Cleanup Worktree

Overview

After a PR is merged, clean up the worktree by fetching the latest main, detaching HEAD at that commit, and deleting the local working branch.

The Process

Phase 0: Brivlo

If brivlo:send_event is available, use brivlo:send_event scm-tools:cleanup-worktree as your first step.

Phase 1: Identify Current Branch

bash
git branch --show-current

Store the branch name. If already detached or on main, warn and stop.

Phase 2: Fetch Latest Main

bash
git fetch origin main

Phase 3: Detach HEAD at Main

bash
git checkout --detach origin/main

Phase 4: Delete Working Branch

bash
git branch -d <branch-name>

If the branch has unmerged changes, git will refuse with -d. Ask user if they want to force delete with -D.

Phase 5: Confirm

Report: "Worktree cleaned up. HEAD detached at origin/main, branch <name> deleted."

Red Flags

If you catch yourself doing these, STOP:

  • Deleting main or master - Never delete these branches
  • Force deleting without asking - Always try -d first, ask before -D
  • Running on non-worktree directories - This is for worktree cleanup only

Quick Reference

PhaseCommandPurpose
1. Identifygit branch --show-currentGet branch to delete
2. Fetchgit fetch origin mainGet latest main
3. Detachgit checkout --detach origin/mainDetach at main
4. Deletegit branch -d <branch>Remove working branch
5. ConfirmReport statusDone