`gh pr merge <url> --delete-branch` removes a worktree and deletes a same-named branch in the cwd's unrelated repository
Describe the bug
gh pr merge <PR-URL> --delete-branch runs its local cleanup in the repository of the current working directory, even when that repository is not the one the PR belongs to. It matches the head branch by name only. If a linked worktree there has a branch with the same name checked out, gh removes that worktree and force-deletes the branch. The branch and worktree belong to an unrelated repository.
git worktree remove (without --force) only refuses uncommitted changes, so a clean worktree whose branch holds unpushed commits is removed. git branch -D then leaves those commits reachable only from the reflog, so the work is effectively lost.
Version: gh version 2.100.0 (2026-09-03), macOS.
Steps to reproduce
- Repo A (
owner-a/repo-a) has an open PR whose head branch isfeature-x. - An unrelated local repo B, whose remote is a different GitHub repository, has a linked worktree with its own branch also named
feature-x. That branch holds a commit that has not been pushed. - With the cwd anywhere inside repo B, run:
gh pr merge https://github.com/owner-a/repo-a/pull/N --merge --delete-branch - The PR in repo A merges and its remote branch is deleted, as expected. In addition, gh removes repo B's worktree and deletes repo B's local
feature-x. The unpushed commit is no longer reachable from any ref. A process running in that worktree (a build, in our case) was also broken.
Where it comes from (v2.100.0 source)
pkg/cmd/pr/merge/merge.go:124:opts.CanDeleteLocalBranch = !cmd.Flags().Changed("repo"). A URL selector does not set--repo, so local cleanup stays enabled.merge.go:594:localBranchExists: opts.CanDeleteLocalBranch && opts.GitClient.HasLocalBranch(ctx, pr.HeadRefName). This is evaluated in the cwd's repo, by name, and nothing checks that the cwd's repo is the PR's repository.merge.go:494-504: if the branch is checked out in another linked worktree, gh callsWorktreeRemoveand thenDeleteLocalBranch(git branch -D). This worktree handling appears to have come in with #13380.
Passing --repo avoids it, because CanDeleteLocalBranch becomes false, and the remote branch is still deleted.
Expected behavior
Local cleanup should only touch a repository whose remote actually is the PR's base (or head) repository. For example, skip local cleanup unless one of the cwd repo's remotes resolves to the PR's repo, and/or unless the local branch's upstream tracks the PR's head repo and branch.
Independently, gh should not remove a worktree, or force-delete a branch, whose tip has commits that are not on the PR head (the merged commit). A safe check is that the local tip is equal to, or an ancestor of, headRefOid; otherwise, skip and warn. git branch -D bypasses git's own merged check, so this is the only guard.
Additional context
This mostly bites people who work across several repos with a shared branch-naming convention (e.g. tooling that names branches claude/<topic>), or who merge a PR by URL from whichever repo their shell happens to be in. Nothing in the output hints that a different repository was touched: gh prints Removed worktree <path> and Deleted local branch <name>, and the path belongs to the other repo.
Source: cli/cli