Skip to content

gh pr merge <url> --delete-branch removes a worktree and deletes a same-named branch in the cwd's unrelated repository #14537

Open

Agent suggestions

Public preview

Description

@wjramos

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

  1. Repo A (owner-a/repo-a) has an open PR whose head branch is feature-x.
  2. 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.
  3. With the cwd anywhere inside repo B, run:
    gh pr merge https://github.com/owner-a/repo-a/pull/N --merge --delete-branch
  4. 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 calls WorktreeRemove and then DeleteLocalBranch (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

  1. cli-triage commented on Sep 28, 2026

    @cli-triage

    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 --repo isn't set (including URL-based PR selectors), and localBranchExists is derived purely from HasLocalBranch matching 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, WorktreeRemove followed by DeleteLocalBranch runs unconditionally, and since git branch -D skips the merged-check, an unrelated worktree's unpushed commits can be lost. I found no exact duplicate; I'm suggesting bug, priority-2 (real workflow/data-loss risk but requires an uncommon cross-repo/shared-naming setup and has a --repo workaround), and gh-pr.

    Generated by Issue Triage (skills-driven) for #14537 · copilot · auto · 28.1 AIC · ⌖ 1.64 AIC · ⊞ 9.5K · ◷

  2. babakks commented on Oct 6, 2026

    @babakks
    Member

    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 upstream remote 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.

  3. added
    bug
    priority-3
    gh-pr
    on Oct 6, 2026
  4. removed
    needs-triage
    on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    buggh-prpriority-3stale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions