Skip to content
Agentmart
Submit a tool

clean-branches

Clean up the local leftovers of finished work in one pass, removing worktrees and local branches whose pull request merged or closed, detached worktrees…

Skill by toy-crane

IndexedMIT★ 3
Write a review

Chalk Labs

Need help deploying clean-branches?

Chalk Labs sets up agents like this inside real teams: connected to your tools, tested on your data, with a human in the loop where it matters.

Book a call

Install

Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.

npx agentmarket install skill/toy-crane-clean-branches

Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.

Get the weekly diff

The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.

We use your email only for this newsletter. Privacy

About

Clean up the local leftovers of finished work in one pass, removing worktrees and local branches whose pull request merged or closed, detached worktrees already contained in the base, and left-over worktree folders that lost their Git registration, while leaving open, uncommitted, session-attached, and Codex-managed work in place. Use when the user asks to clean up, prune, or tidy stale or piled-up branches or worktrees across a repository. merge hands this skill the one branch it just merged. Remote branches are never deleted.

Use cases: agent-building

No reviews yet. Installed it? Reviews come from verified installers only. How reviews work

Permissions

What this skill can do and what it asks for, from its SKILL.md and code. Checked Oct 26, 2026.

Tools
No tools found
Secrets
None asked for
Install scripts
None found
Runs as
Shell scripts

Network hosts it talks to 1

  • codeload.github.com · found in install command

Files it touches

  • ~/.claude/skills/clean-branches

What's inside

Skill name
clean-branches
Description
Clean up the local leftovers of finished work in one pass, removing worktrees and local branches whose pull request merged or closed, detached worktrees already contained in the base, and left-over worktree folders that lost their Git registration, while leaving open, uncommitted, session-attached, and Codex-managed work in place. Use when the user asks to clean up, prune, or tidy stale or piled-up branches or worktrees across a repository. merge hands this skill the one branch it just merged. Remote branches are never deleted.
License
MIT
Instructions
128 lines in SKILL.md · view on GitHub

Before installing

  • Ships executable scripts

Files (6)

  • SKILL.md
  • openai.yaml · agents/
  • evals.json · evals/
  • test-remove-worktree.sh · evals/
  • trigger-evals.json · evals/
  • remove-worktree.sh · scripts/

SKILL.md (first lines)

# Clean branches

Remove the local leftovers of finished work, then report what was removed,
what stayed, and why. There are two entry points: a sweep of the whole
repository when the user asks, and one branch that `merge` hands over right
after it verified the merge. Do not ask for confirmation along the way: safety
comes from the narrow targets below, and anything that cannot be judged stays
where it is.

## Find the candidates

Fetch the remote first. The base is the branch the user names, otherwise the
remote's advertised default branch. Read `git worktree list --porcelain` for
registered worktrees and `git branch` for local branches. For folders that lost
their registration, look only inside folders of the default checkout that also
hold a registered linked worktree, such as `.claude/worktrees/`.

A worktree is a candidate when one of these holds:

- Its checked-out branch has a pull request that merged or closed. Judge this
  from GitHub, for example `gh pr list --head <branch> --state all`, because a
  squash merge leaves the branch tip outside the base history. Any open pull
  request for the branch name keeps the worktree; otherwise use the most
  recently created one, and keep its head commit for the helper.
- Its HEAD is detached and contained in the fetched base.
- It is a folder whose Git registration is gone. The helper below decides
  whether such a folder is safe to remove.

A local branch with no worktree is a candidate when its pull request merged or
closed, judged the same way. A branch checked out in the primary checkout is
never a sweep candidate: the primary checkout is left in place, and only the
handoff from `merge` below touches a branch checked out there.

Leave these in place and list them in the report: the default checkout, a
worktree on the base branch, the base branch itself, an open pull request, a
branch with no pull request even when the base contains it, a branch whose tip
has commits beyond its pull request head that the base does not contain, and
any worktree with uncommitted changes, including one this session works
inside. If `gh` is missing or not authenticated, skip worktrees and branches
with a pull request to judge, say so, and keep going with detached worktrees
and folders.

## Release and remove each worktree candidate

The project releases a worktree's development resources with its own commands.
Read `AGENTS.md` and `CLAUDE.md` and use the first `## Worktree cleanup`
section found: its fenced code block lists one command per line, and text
outside the block is for people. Without that section, stop no process.

Decide each candidate with the bundled
[worktree removal helper](scripts/remove-worktree.sh), run from this session's
own working directory: `inspect <worktree> --base <remote>/<base>`, adding
`--pr-head <sha>` for a branch with a pull request. The helper counts its
working directory as this session's, so running it after changing into a
candidate reads as `session-inside`.

- `session-inside`: this session works inside the candidate. The helper does
  not check for uncommitted changes here, so leave the candidate when
  `git status --porcelain` shows any. Otherwise run the declared commands, then
  `remove` with the same arguments. The folder stays, HEAD detaches at the
  base, and only the branch is deleted.
- `ready`: run the declared commands, then `remove` with the same arguments.
  It checks again and removes the worktree, or the folder, and its branch only
  when no process still works inside.
- `attached`, `codex-managed`, or `blocked`: leave the candidate, its
  processes, and its branch as they are. A live session, terminal, or app still
  uses an attached worktree. The Codex app manages its own worktrees and
  removes one, after saving a snapshot, when its chat is archived.

When `inspect` reports `branch <name> unmerged`, leave that worktree too: its
branch has commits that exist nowhere else.

Run the declared commands in order inside the candidate's folder. Stop at the
first failure and leave that folder and branch so the commands can run again.
Never stop a process yourself or force a removal. Handle candidates one at a
time; a candidate that stays does not stop the others.

## Delete each branch-only candidate

A branch with no worktree needs no helper and no cleanup commands. Delete it
only when its tip equals its merged or closed pull request's head commit or
the fetched base contains it; otherwise leave it and report it as unmerged.
Never delete the base branch or a branch that is checked out anywhere.

## Clean the branch that merge hands over

When `merge` invokes this skill, it passes the merged branch, its worktree when
one exists, the base, and the merged pull request's head commit. Fetch the base
first, because the remote merge moved it after any fetch `merge` made, so the
helper's base argument, the detached HEAD it leaves for a session, and the
branch-only rule all see the merged base. Then skip the candidate search and
act on that branch alone:

- In a linked worktree: run the helper with those arguments and act on its
  verdict exactly as above, including the declared cleanup commands.
- Checked out in the primary checkout: the helper reports `main-checkout` and
  `blocked`, so do not use it. When `git status --porcelain` shows uncommitted
  changes, leave the branch and report it. Otherwise bring that checkout to
  the merged base state by checking out the base and fast-forwarding it to
  the freshly fetched remote, then delete only the local branch under the
  branch-only rule.
  When the base cannot be checked out there, for example because a linked
  worktree holds it, or cannot be fast-forwarded because the local base has
  diverged, leave the checkout and the branch as they are and report why;
  never force, reset, or detach to get past it.
- Neither: delete it under the branch-only rule.

Hand the result lines back so `merge` can report them beside the verified
merge.

## Report

Finish with one report grouped as follows:

- Removed: each worktree, folder, or branch with why it qualified, such as the
  merged or closed pull request number, containment in the base, or a lost
  registration.
- Folder kept for this session: HEAD detached and the branch deleted.
- Left in place: each one with its reason, such as the open pull request

From toy-crane/skills, MIT.

More from toy-crane/skills

Signals

Stars
3
Forks
0
Open issues
0
Last commit (this folder)
Oct 9, 2026 (21 days ago)

From GitHub (fetched Oct 24, 2026).

Details

License
MIT
First seen
Oct 24, 2026
Last checked
Oct 24, 2026

More from toy-crane

Done for you

Rather not set it up yourself?

Chalk Labs scopes, builds and runs agents for your team: the task audit, the eval harness, the integrations and the weekly ops report. Fixed scope, priced before we start.

See how we build AI agents →Read the case studies

Chalk Labs is a two-founder AI and growth studio. We build AI agents, CRMs and MVPs for Web3 and AI companies, and have shipped 50+ product and token launches.

launches shipped
50+
launches shipped
media placements
500+
media placements
founders on every account
2
founders on every account

Press we've placed: Forbes · CoinDesk · Decrypt · Cointelegraph · The Block

Rahil Jain, Co-founder, Chalk Labs. Ex-Goldman Sachs engineer. You talk to the person who builds it.

Get clean-branches running in your business

Tell us what you want to automate. A founder replies within one working day with a plan, not a pitch deck.

Goes to the Chalk Labs founders, not the newsletter. Privacy

Need help deploying clean-branches?

CallGet help
Cite this page

clean-branches skill. Agent Mart. https://agentmart.dev/skills/toy-crane-clean-branches (last updated 2026-10-24)