Skip to content

Delete the remote branch when release deletes a PR-merged branch - #3

Merged
dinoderek merged 2 commits into
mainfrom
fix/release-remote-branch
Oct 20, 2026
Merged

dinoderek merged 2 commits into
mainfrom
fix/release-remote-branch

Conversation

@dinoderek

Copy link
Copy Markdown
Owner

Objective

./dev worktree release <branch> now deletes origin/<branch> when a PR merged the branch, including when git branch -d succeeds, for example a squash-merged branch whose upstream still exists. Before this change, only the -D path deleted the remote branch, so on the -d path it had to be removed by hand. The remote branch is deleted only while it is still at the local tip.

Gates

Lane Result Evidence
./dev test (every required lane) PASS .test-ai-results/summary.md: format, vet, lint, unit, coverage, file-length, imports, versions, docs-check all PASS, Overall: PASS
  • New or changed tests (all in devtools/internal/dev/worktree_test.go, real git with a bare origin):
    • TestWorktreeDeletesTheRemoteOfAMergedBranchWithAnUpstream: push -u, PR merged at the tip, release. The local branch and the origin's refs/heads/feat/p are both gone.
    • TestWorktreeKeepsAMovedRemoteBranch: origin moved past the local tip after the PR merged. The local branch is deleted, the remote survives at the newer commit, and no remote delete is reported.
    • TestWorktreeKeepsTheRemoteOfAnUnmergedPushedBranch: push -u with no PR. -d passes, but the remote survives.
    • TestWorktreeKeepsUnmergedBranch: now also pushes the branch and asserts that the remote of a kept branch survives.
    • Mutation checks: removing the PR gate fails the unmerged test, removing the lease fails the moved test, and the code on origin/main fails the merged-with-upstream test.
  • Not run, with reason: none

Rulings

Ruling (risk: low): the remote delete requires a PR merged at exactly the local tip, on both the -d and -D paths, as well as the --force-with-lease=refs/heads/<branch>:<tip> lease.
Why: when a branch has an upstream, git branch -d checks only that upstream, so a branch pushed with -u and never merged passes it. Deleting its remote then would close an open PR and drop the only remote copy. A PR check is used instead of an ancestor-of-main check because a squash-merged tip is never an ancestor of main.
If wrong: a branch merged without a PR (pushed straight to main) keeps its remote branch, which can be deleted by hand. Nothing is lost.

Ruling (risk: low): the lease and the delete name refs/heads/<branch>, not the short name.
Why: if origin has a tag with the same name, the short name makes the push ambiguous.
If wrong: none. Both forms name the same branch.

Review

  • Fresh reviewer: Claude Code subagent (general-purpose), Claude Opus 5.5. First pass: CHANGES_REQUESTED; it found the unmerged-with-upstream data loss, which the PR-gate ruling fixes. Second pass at 3e67357: APPROVED (three non-blocking style nits left as is)
  • Declined findings: one. Print "kept remote branch" when the push delete fails: declined. GitHub's auto-delete of head branches often removes the remote branch first, so the message would be misleading, and the task asks for silence when the branch is already gone.
  • Review hard:
    • devtools/internal/dev/worktree.go deleteBranch: the PR gate and the ordering of -d and -D.
    • deleteRemoteBranch: the lease semantics.

Deviations

The task said to delete the remote after any successful -d if it was at the local tip. This PR also requires a merged PR at that tip; see Rulings. Otherwise none.

🤖 Generated with Claude Code

dinoderek and others added 2 commits October 20, 2026 18:58
./dev worktree release deleted origin/<branch> only on the merged-PR path.
When git branch -d succeeded (for example a squash-merged branch whose
upstream still exists), the remote branch survived. Release now captures the
local tip before deleting and, on either path, deletes origin/<branch> with
--force-with-lease=<branch>:<tip>, so a remote that has moved is kept.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
git branch -d passes any branch that is merged into its upstream, so a
branch pushed with -u and never merged passed it, and release then deleted
its remote branch (closing any open PR). The remote delete now requires a PR
merged at exactly the local tip, on both the -d and the -D paths. The lease
and the delete name refs/heads/<branch>, so a same-named tag cannot make the
push ambiguous. Tests share commit, gitIn and ghSays helpers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dinoderek
dinoderek merged commit f1a4f4b into main Oct 20, 2026
1 check passed
@dinoderek
dinoderek deleted the fix/release-remote-branch branch October 20, 2026 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant