agentleFS
coddy-projectcoddy-project/coddy-agent/.cursor/rules/merged-branch-cleanup.mdc

coddy-agent / rules

Cursor rule187 starsChanged 9 days agoMITView on GitHub

After a pull request merges, remove its worktree and delete its branch, locally and on origin

  • Commits and pushes

We split this merged-branch-cleanup.mdc into the rules it gives an agent

7 rules. About 0 are common, 2 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    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 …

    line 31
  2. Distinctive:

    Make sure nothing is lost:

    line 34
  3. Very specific:

    every commit of the branch reached the pull request: git -C <main checkout> fetch origin pull/<PR>/head, then git -C <main checkout> …

    line 38
  4. Very specific:

    Remove the worktree: git -C <main checkout> worktree remove <worktree>, then git -C <main checkout> worktree prune.

    line 43
  5. Very specific:

    Delete the local branch: git -C <main checkout> branch -D <branch>.

    line 45
  6. Very specific:

    Delete the branch on origin.

    line 48
  7. Very specific:

    Never delete main, the orphan screenshots branch or a tag, whatever the cleanup lists.

    line 55

≈ “Common” is an estimate: at least 1 in 5 of the 6,350 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
merged-branch-cleanup.mdc
---
description: After a pull request merges, remove its worktree and delete its branch, locally and on origin
alwaysApply: true
---

# 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`.

Never delete `main`, the orphan `screenshots` branch or a tag, whatever the cleanup lists.

## Report

The final answer names what was removed: the worktree path, the local branch, and the branch on
origin (deleted by you, or already gone). When a step stopped the cleanup, it names what stopped it
and what is left on the disk, so the operator decides.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

Reviews can't be read right now.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.