360 Secure SkillHub
360 安全技能中心

done

面向基于 worktree 的 git 工作流收尾:先调研各分支对应的 PR 状态,删除已合并或已关闭 PR 的远程与本地分支及干净的 worktree,保留仍在进行中的工作,最后切回默认分支并快进更新;遇到未提交改动或找不到 PR 时会停下询问。

分类:开发
zhuconv
热度 0.00
最近更新 2026-12-02 12:15:19

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add zhuconv/viberesearch/done
兼容性
通用兼容
联网要求
需要联网
数据出网
必须出网
Metadata
name
done
description
Use when finished with a branch or PR and ready to wrap up — clears legacy worktrees and branches (remote and local) whose PR has landed, and switches back to the default branch. Triggers on "I'm done with this branch/PR", "clean up this worktree", "wrap up and go back to main", "close out this PR".

Done

Wraps up a worktree-based git workflow: removes branches/worktrees whose PR has landed (merged or closed), leaves anything still open strictly alone, and returns you to the default branch — fetching it fresh only once nothing is left in flight.

0. Survey, before touching anything

git fetch --prune origin        # updates remote-tracking refs, drops ones GitHub already deleted on merge
git worktree list --porcelain   # every worktree + the branch it's on
git branch -vv                  # local branches, upstream, and `[gone]` markers
gh pr list --state all --json number,headRefName,state,url --limit 200
DEFAULT_BRANCH="$(gh repo view --json defaultBranchRef -q .defaultBranchRef.name 2>/dev/null \
  || git remote show origin | sed -n 's/.*HEAD branch: //p')"

For every local branch that isn't $DEFAULT_BRANCH, match it to a PR by headRefName and note the PR's state.

Classify, don't assume:

  • MERGED or CLOSED → legacy, safe to remove.
  • OPEN → leave completely alone — no delete, no prune.
  • No PR found for the branch → do not delete. Flag it and ask; it could be unpushed WIP nobody filed a PR for yet.

1. Remote: delete legacy branches

git fetch --prune already dropped the remote-tracking ref for any branch GitHub auto-deleted on merge (the common case — nothing left to do there). For a legacy branch whose ref is still on the remote:

git ls-remote --exit-code --heads origin <branch> && git push origin --delete <branch>

2. Local: remove legacy worktrees and branches

For each legacy branch that has a worktree:

git -C <worktree-path> status --porcelain   # must be empty

If it's dirty: stop and tell the user what's uncommitted there instead of removing anything — never discard work silently.

If clean:

git worktree remove <worktree-path>
git branch -D <branch>

Use -D, not -d. The PR's state (from gh, checked in step 0) is already the source of truth that the branch's work landed — git branch -d's own merge-ancestry check will falsely refuse a squash-merged branch, since its commits' SHAs never actually appear on $DEFAULT_BRANCH even though the PR merged cleanly. A legacy branch with no worktree just skips the worktree remove line.

3. Switch back to the default branch

Move the session to the primary worktree (the one already checked out to $DEFAULT_BRANCH — look for it in the step 0 git worktree list output).

  • If any branch is still classified OPEN (including ones skipped in step 0 for having no PR at all): print each one — branch name + PR URL — as a clear warning, and stop there. Do not fetch or fast-forward past it silently; the user should know work is still in flight.
  • If nothing is left open (every branch found was either just cleaned up as legacy in steps 1–2, or there were none to begin with): bring $DEFAULT_BRANCH up to date —
    git switch "$DEFAULT_BRANCH"
    git fetch origin "$DEFAULT_BRANCH"
    git merge --ff-only "origin/$DEFAULT_BRANCH"
    
    If --ff-only refuses (local branch diverged from remote), stop and report it rather than force-resetting.

Report

Summarize: what was deleted (remote branches, local worktrees, local branches), what was left alone and why (open PR, or no PR found), and whether $DEFAULT_BRANCH was fetched and fast-forwarded.


Claude Code note: if a worktree being cleaned up was created by the EnterWorktree tool (its path is under .claude/worktrees/), prefer ExitWorktree with action: "remove" for that one instead of a raw git worktree remove — it keeps the harness's own bookkeeping in sync. Everything else here is plain git/gh and behaves the same in Codex.