merged-branch-cleanup

merged-branch-cleanup is a cursor rule for Cursor from coddy-project/coddy-agent. It costs 931 tokens per session, scanned A, original, MIT.

A workflow rule for cleaning up a branch and its worktree after a pull request merges. A worktree is another local checkout of the same repository.

In plain words
What is it for?
It is for removing the merged branch locally and on the remote repository, deleting its worktree, and handling branches that reached the main branch directly.
Why use it?
It prevents unused checkouts, dependencies, and build caches from taking disk space and stops finished branches from being reused accidentally.

Cursor rule for Cursor

Written for Cursor: installed under .cursor/.

Good fit It is for removing the merged branch locally and on the remote repository, deleting its worktree, and handling branches that reached the main branch directly.

Compare 6 cursor rules from other repositories ↓
Install with agentmods
npx agentmods add rules/coddy-project/coddy-agent/merged-branch-cleanup
Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

Clone the repo
git clone --depth 1 https://github.com/coddy-project/coddy-agent

Made for: Cursor.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for merged-branch-cleanup

README.md
[![agentmods](https://agentmods.dev/badge/rules/coddy-project/coddy-agent/merged-branch-cleanup/github.svg)](https://agentmods.dev/rules/coddy-project/coddy-agent/merged-branch-cleanup)
Your own site
<a href="https://agentmods.dev/rules/coddy-project/coddy-agent/merged-branch-cleanup"><img src="https://agentmods.dev/badge/rules/coddy-project/coddy-agent/merged-branch-cleanup/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for merged-branch-cleanup

Your own site · 80×15
<a href="https://agentmods.dev/rules/coddy-project/coddy-agent/merged-branch-cleanup"><img src="https://agentmods.dev/badge/rules/coddy-project/coddy-agent/merged-branch-cleanup.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 931 This file is loaded in full into every session.
When invoked 931 The same file — it is already loaded in full.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5.1 $0.00931 $0.00931
Opus 5.5 $0.00372 $0.00372
Sonnet 5.5 $0.00186 $0.00186
Haiku 4.5 $0.00093 $0.00093

Measured 2d ago against content hash 09d6a9fd5241, method: parsed. Prices are Anthropic first-party input rates as of 2026-10-07, from the pricing page.

Security

Grade A, and why

merged-branch-cleanup scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/merged-branch-cleanup.mdc · 62 lines

How it starts

The opening of the file, as written. The whole thing — 62 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Cleanup after a merge

A branch whose pull request has merged is finished, and its worktree and the branch itself are removed in the same session, as the last step of the merge rather than later. A worktree left behind keeps a checkout, a node_modules and a build cache on the disk; a branch left behind is where the next agent commits a follow-up that no pull request will ever pick up.

When it applies

As soon as you know that the pull request of a branch you worked on has merged: you merged it (Merging the pull request in the workflow rule), the operator says so, or gh pr view <PR> --json state reports MERGED. It covers a worktree a host made for the session (.claude/worktrees/, .coddy/worktrees/ and the like) as much as one made by hand, and the isolated site-repository worktree once its commit is pushed.

A branch that reached main by a direct push, with no pull request, is finished the same way once the push is confirmed. Step 1 then checks it against main instead of a pull request (git -C <main checkout> fetch origin, then git -C <main checkout> merge-base --is-ancestor <branch> origin/main), and step 4 has nothing to delete unless the branch itself was pushed.

A merged branch is never reused: a follow-up starts a new branch from current origin/main.

Steps

Run them from the main checkout (the first line of git worktree list) with git -C: the worktree being removed may be the session's own working directory, which is gone after step 2.

  1. Make sure nothing is lost:
    • git -C <worktree> status --porcelain prints nothing. Uncommitted or untracked files stop the cleanup: show them to the operator and wait, and never run git worktree remove --force without the operator's word. Ignored files (build output, node_modules) go with the worktree;
    • every commit of the branch reached the pull request: git -C <main checkout> fetch origin pull/<PR>/head, then git -C <main checkout> merge-base --is-ancestor <branch> FETCH_HEAD succeeds (FETCH_HEAD belongs to the worktree that fetched, so both run in the same one). A commit made after the merge stops the cleanup the same way: it goes to a new branch and a new pull request first.
  2. Remove the worktree: git -C <main checkout> worktree remove <worktree>, then git -C <main checkout> worktree prune.
  3. Delete the local branch: git -C <main checkout> branch -D <branch>. It is -D and not -d because a squash or rebase merge leaves the branch's own commits out of main, and -d refuses such a branch; the ancestry check of step 1 is what makes -D safe.
  4. Delete the branch on origin. GitHub deletes the head branch of a merged pull request by itself (delete_branch_on_merge is on for this repository), so this step is usually a check that git ls-remote --exit-code --heads origin <branch> finds nothing. When it finds the branch (the setting was off, or the merge was a local push), run git push origin --delete <branch>. A pull request from a fork has no branch on origin, and the fork's branch belongs to its author: leave it.
  5. Drop the stale remote-tracking ref: git -C <main checkout> fetch --prune origin.

Read the full file on GitHub · 62 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 62 lines · 931 tokens per session scan A 09d6a9fd5241

Subscribe to this mod's changes

merged-branch-cleanup is a cursor rule published in the GitHub repository coddy-project/coddy-agent (175 stars, last pushed today), licensed MIT. It adds 931 tokens to every session, about $0.0037 per session on Opus 5.5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-10-14.