Overview
Core principle: Verify tests → detect environment → present options → execute choice → clean up.
Announce at the start: "I am using the finishing-a-development-branch skill to finish this work."
Step 1: Verify tests
Run the project's full test suite (npm test / cargo test / pytest / go test ./...)。
If tests fail, report the failure and stop—the menu only appears after tests are all green:
测试失败(<N> 个)。完成之前必须先修:
[展示失败详情]
If tests pass: continue to step 2.
Step 2: Detect environment
GIT_DIR=$(cd "$(git rev-parse --git-dir)" 2>/dev/null && pwd -P)
GIT_COMMON=$(cd "$(git rev-parse --git-common-dir)" 2>/dev/null && pwd -P)
# 现在就捕获 —— 此刻还在工作区里面。步骤 5 会切换目录,
# 而清理(步骤 6)需要这个值
WORKTREE_PATH=$(git rev-parse --show-toplevel)
This determines which menu to present and how to clean up:
| State | Menu | Cleanup |
|---|---|---|
GIT_DIR == GIT_COMMON(normal repository) |
Standard 3 options | No worktree to clean up |
GIT_DIR != GIT_COMMON, named branch |
Standard 3 options | Decide by origin (see step 6) |
GIT_DIR != GIT_COMMON, detached HEAD |
Narrowed to 2 options (no merge) | Externally managed—leave it in place |
Step 3: Determine the base branch
The base branch is the one this work branched off from—it's usually already stated in the plan, in the conversation, or in the branch's upstream. If you still don't know, ask: "This branch branched off from <your best guess>, right?"Confirm before merging: merging into the wrong base branch is very costly.
Step 4: Present options
Normal repository and named branch worktree—present exactly these 3 options:
实现已完成。你想怎么做?
1. 本地合并回 <base-branch>
2. 推送并创建 Pull Request
3. 保留分支不动(我稍后自己处理)
选哪个?
Detached HEAD—present exactly these 2 options:
实现已完成。你当前处于分离 HEAD(由外部管理的工作区)。
1. 作为新分支推送并创建 Pull Request
2. 保持原样(我稍后自己处理)
选哪个?
Present the menu verbatim—concise, with each option drawn from the list above.Discarding work only happens when your human partner explicitly requests it(see "If your human partner asks to discard this work" below). Wait for their answer; whether to integrate is their decision.
Step 5: Execute the choice
Option 1: Local merge
# 切到主仓库根目录,保证 CWD 安全
MAIN_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel)
cd "$MAIN_ROOT"
# 先合并 —— 在删除任何东西之前先验证合并成功
git checkout <base-branch>
git pull
git merge <feature-branch>
# 在合并结果上验证测试
<测试命令>
If tests onthe merge resultfail: stop, leave the worktree and branch in place, and investigate—nothing has been pushed yet, so this merge is local and recoverable.
Once the merge result is all green: clean up the worktree (step 6), then delete the branch:
git branch -d <feature-branch>
Option 2: Push and create a PR
git push -u origin <feature-branch>
# 从分离 HEAD 出发时,在远端指定新分支名:
# git push origin HEAD:refs/heads/<new-branch>
Then usecode hosting platform(forge) tooling to create a pull/merge request against —use a CLI if available, otherwise use the creation URL that most platforms print when you push—follow any PR templates and conventions already in the repository (if any), and report the URL to your human partner.
Keep the worktree—your human partner will keep iterating there based on PR feedback.
Option 3: Keep as is
Report: "Keeping branch . Worktree kept at ."
If your human partner asks to discard this work
This path exists only as a response to an "explicit request to throw the work away." Confirm first:
这将永久删除:
- 分支 <name>
- 所有 commit:<commit 列表>
- 位于 <path> 的工作树
输入 'discard' 以确认。
WaitThis exactconfirmation word. After receiving it:
MAIN_ROOT=$(git -C "$(git rev-parse --git-common-dir)/.." rev-parse --show-toplevel)
cd "$MAIN_ROOT"
Then clean up the worktree (step 6), then force-delete the branch:
git branch -D <feature-branch>
Step 6: Clean up the workspace
Only do this for option 1 and a confirmed discard. Options 2 and 3 always keep the worktree. Both callers have already switched to the main repository root—removing a worktree must be done from outside the worktree—so here we usecaptured in step 2 GIT_DIR / GIT_COMMON / WORKTREE_PATH, that is, the value from before that directory change.
⚠️ Do not recompute these values here. At this moment
git rev-parse --show-toplevelreturns the main repository root, not the worktree path—the provenance check will never match, cleanup will silently no-op, and then branch deletion will fail because the worktree is still attached.
If GIT_DIR == GIT_COMMON: an ordinary repository, no worktree to clean up. End.
If WORKTREE_PATH is under .worktrees/ or worktrees/ : This is a worktree created by Superpowers—we are responsible for cleaning it up:
git worktree remove "$WORKTREE_PATH"
git worktree prune # 自愈:清理任何过期的注册记录
If deletion is refused(contains modified or untracked files): this worktree containsexists nowhere elsefiles—uncommitted plans, notes, or drafts.Never add --force。 on your own. Lay out the stakes for your human partner, then ask them:
git -C "$WORKTREE_PATH" status --porcelain -uall
worktree 删除被拒绝 —— 这些文件从未被提交:
<文件列表>
1. 先把它们提交到 <branch>,再做清理
2. 把它们移到 <主仓库根目录>
3. 删掉它们(不可恢复)
选哪个?
Do what they choose, then delete the worktree.
Otherwise: This workspace belongs to the host environment—leave it in place. If your platform provides a workspace exit tool, use it.
Quick reference
| Option | Merge | Push | Keep worktree | Clean up branch |
|---|---|---|---|---|
| 1. Local merge | Yes | - | - | Yes |
| 2. Create PR | - | Yes | Yes | - |
| 3. Keep as is | - | - | Yes | - |
| Discard (only when explicitly requested) | - | - | - | Yes (force) |
Common rationalizations
| Excuse | Reality |
|---|---|
| "The tests passed earlier in this session" | Onthe very tree you are about to integraterun the test suite. A single green run only proves the tree it ran on at that time. |
| "They obviously want to merge" | Integration is your human partner's decision. Lay out the menu, then wait. |
| "They look done with this feature—I'll propose discarding" | The menu is exactly as written, no more, no less. Discarding only happens when your human partner raises it in explicit words. |
| "'Eh, just delete it' counts as confirmation" | Only entering discard that word authorizes deletion. |
| "The PR is already open, the worktree is now annoying junk" | PR feedback gets fixed in that worktree. It must stay until the work lands. |
| "That other worktree looks stale—I'll clean it up too while I'm at it" | Only clean up .worktrees/ or worktrees/ under the worktree. Everything else belongs to the host environment. |
| "The merge result failure is probably sporadic" | A merge result failure brings everything to a halt. While you investigate, the branch and worktree stay put. |
| "The base branch is obviously main" | Confirm the fork point, or just ask. Merging into the wrong base branch is costly. |
| "The push was rejected—just force-push" | A rejected push means the remote has changed. Investigate; only force-push when your human partner explicitly asks for it. |
"The delete was rejected—adding --force just finishes the cleanup" |
Being rejected is exactly what shows there are files that exist only in that worktree.--force will delete them irrecoverably. Ask your human partner first. |