- Home
- Skills
- DevOps & Cloud
- Merged Worktree Cleanup: Safe Post-PR Git Teardown
Merged Worktree Cleanup: Safe Post-PR Git Teardown
Remove a story's worktree and local branch only after the exact pull request is confirmed merged. Works across any AI agent workflow. No force, no guesses, no remote deletion.
Free
Works with the AI Agents you already use
Merged Worktree Cleanup: Safe Post-PR Git Teardown
Example session with this skill installed
PR merged, clean up the CAN-001 worktree. PR: https://github.com/<owner>/<repo>/pull/1
- Read your context and instructions
- Compiled the merged worktree cleanup
Summary
The CAN-001 worktree and local branch are cleaned up. The merge was confirmed on GitHub before anything was removed.
Real run from our install test, translated and lightly trimmed.
| Target | Result |
|---|---|
| PR #1 | Merged into main at 2026-10-15T16:47:43Z, checked with gh pr view before any removal |
Worktree .claude/worktrees/CAN-001 | Clean, removed without --force. git worktree list now shows only the primary worktree. |
Local branch worktree-CAN-001 (cb9cea7) | Deleted with git branch -d after the script confirmed the commit is in the updated origin/main |
Remote branch origin/worktree-CAN-001 | Kept, because you did not ask to delete it |
Note. Git printed "not yet merged to HEAD" while deleting the branch. Nothing was lost: the warning appears only because local main is behind origin/main. Run git pull on main to bring in the CAN-001 change.
Same request, before the merge. An earlier run of this exact prompt found state: OPEN, mergedAt: null on PR #1, changed nothing, and left the worktree and branch intact with a note to come back once GitHub shows the PR as merged.
Next steps
git pullonmain- Delete the remote branch yourself if you no longer need it
Connects securely to your tools. The creator never sees your data.
About this skill
What it does
The problem. Worktrees and story branches pile up after merges across Claude Code, Codex, Cursor, Antigravity, and Windsurf sessions. Cleaning them by hand invites --force, branch -D, or deleting the wrong branch while a PR is still open.
What the skill does. It confirms mergedAt on the exact PR with gh, requires the worktree to be clean, fetches the base, checks the branch tip is in it, previews the targets, then removes the worktree and deletes the local branch with safe Git commands. Accepts any branch prefix (worktree-*, codex/*, claude/*, agent/*, story/*).
Why not just prompt for it. The bundled script verifies merge state in both preview and apply mode and refuses to continue when anything is ambiguous. If the PR is still open, nothing changes.
Use it when a story PR has been merged and you want the worktree and local branch gone safely.
Known limits.
- After a squash merge the branch tip is not in the base, so the skill stops and asks rather than deleting.
- GitHub only, through an authenticated
gh.
Where it fits
- Project Setup, once per repository: plan, backlog, verifier
- Multi-Agent Story Flow or Dual-Session Story Flow, once per story: build, verify, independent review
- You: read the diff, push, open the PR, merge
- Merged Worktree Cleanup (this skill), after the merge: remove the worktree and branch
Project Setup is optional but recommended: without it the story flows fall back to auto-detected checks, with no backlog or project-owned verifier.
How to use
-
Unzip once into a shared folder that every agent can use:
mkdir -p ~/.agents/skills/close-story-worktree && unzip -o close-story-worktree-<version>.zip -d ~/.agents/skills/close-story-worktree. Codex CLI and Gemini CLI read~/.agents/skillsdirectly. Point the others at the same folder instead of making a second copy:- Claude Code:
mkdir -p ~/.claude/skills && ln -sfn ~/.agents/skills/close-story-worktree ~/.claude/skills/close-story-worktree - Cursor:
mkdir -p ~/.cursor/skills && ln -sfn ~/.agents/skills/close-story-worktree ~/.cursor/skills/close-story-worktree - Antigravity:
mkdir -p ~/.gemini/config/skills && ln -sfn ~/.agents/skills/close-story-worktree ~/.gemini/config/skills/close-story-worktree
Keep only this one copy: a second install of the same skill can make the agent pick the wrong one.
- Claude Code:
-
After merging, say
PR merged, clean up the worktreewith the story ID or branch.
Requirements
git and gh authenticated against the repository. Works in any agent that loads SKILL.md skills, including Claude Code, Codex CLI, Cursor, Google Antigravity, Gemini CLI, Windsurf, GitHub Copilot, and OpenCode.
Works best with: Multi-Agent Story Flow and Dual-Session Story Flow, which create the worktrees this skill removes.
GitHub only, through an authenticated gh. After a squash or rebase merge the branch tip is not in the base, so the skill stops and asks instead of deleting. It never removes a dirty worktree and never deletes the remote branch unless you ask.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before any payment.
Reviews
This skill has been used 2 times.
Trust & safety
Security scan
Security scan passed 7 days ago
- Free with an Agensi account
Requires
Permissions
File Scopes
Network: `gh pr view` and `git fetch origin <base>`. Writes: only after the merge is confirmed: `git worktree remove` (never `--force`) and `git branch -d`. Never: deletes the remote branch, force-removes a dirty worktree, or cleans up before the PR shows as merged.