Repository navigation
Agent suggestions
Public previewDescription
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 feat(merge): handle worktree context in gh pr merge --delete-branch #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.
Activity
cli-triage commentedon Sep 28, 2026
This looks like a genuine bug rather than a support question: the report includes clear reproduction steps, version info, and a plausible mechanism grounded in the current source. In
merge.go,opts.CanDeleteLocalBranch = !cmd.Flags().Changed("repo")leaves local cleanup enabled whenever--repoisn't set (including URL-based PR selectors), andlocalBranchExistsis derived purely fromHasLocalBranchmatching by name in the cwd's repo, with no check that the cwd repo is actually the PR's repo. When the branch is checked out in another linked worktree,WorktreeRemovefollowed byDeleteLocalBranchruns unconditionally, and sincegit branch -Dskips the merged-check, an unrelated worktree's unpushed commits can be lost. I found no exact duplicate; I'm suggestingbug,priority-2(real workflow/data-loss risk but requires an uncommon cross-repo/shared-naming setup and has a--repoworkaround), andgh-pr.babakks commentedon Oct 6, 2026
Thanks for reporting this and for the detailed investigation, @wjramos! 🙏
This is a bug. The underlying problem predates worktree support: local cleanup has relied on the pull request head branch name without confirming that the local branch belongs to the selected pull request. Previously, Git would refuse deletion when that branch was checked out in another worktree. Worktree support made the existing problem more destructive by removing a matching linked worktree before force deleting the branch.
The same underlying issue also affects
gh pr close, as reported in #14550. That command has a separate local cleanup path, so the eventual fix should account for both commands.Checking only whether the current repository has a remote matching the pull request repository would not be sufficient. For example, a clone of one fork commonly has an
upstreamremote pointing to the base repository. A pull request may originate from the base repository or another fork while the current fork happens to contain a branch with the same name. The matching remote would not prove that the local branch is the pull request branch.Before performing local cleanup, we should establish that the local branch corresponds to the pull request, such as by checking its configured remote ref or its relationship to the pull request head SHA. If that relationship cannot be established safely, we should skip local cleanup and leave the branch and worktree untouched. The exact validation rules can be determined during implementation.