#14550·cli

gh pr close <URL> --delete-branch can force-delete a same-named branch in an unrelated repository

Author: sean-from-japanCreated Sep 29, 2026Updated Sep 29, 2026
Labelsneeds-triage

Describe the bug

Running gh pr close <PR URL> --delete-branch from an unrelated local repository can force-delete a local branch in that repository if it has the same name as the PR's head branch. The command closes the selected PR, then looks up the branch by name in the current working directory and runs git branch -D there. An unpushed commit on the unrelated branch loses its branch reference.

This is related to #14537, which reports the same class of problem in gh pr merge. gh pr close has a separate cleanup path in pkg/cmd/pr/close/close.go; fixing pr merge alone does not change it.

Affected version

Local build of cli/cli trunk at 9b031151a825bda919203c5202876a725d637368:

gh version 9b03115 (2026-09-29)

Steps to reproduce the behavior

  1. Have an open PR in owner-a/repo-a with head branch feature-x.
  2. In an unrelated local repository owner-b/repo-b, have a local branch also named feature-x with an unpushed commit. Keep another branch checked out, and run the command from this repository.
  3. Run gh pr close https://github.com/owner-a/repo-a/pull/123 --delete-branch (substitute the actual PR number).

I confirmed this command path with mocked GraphQL, REST, and Git interactions, without closing a live PR. The URL selector leaves DeleteLocalBranch enabled, and the Git stub for git branch -D feature-x is consumed.

Expected vs actual behavior

Expected: Closing a PR in repo A must not delete a branch in unrelated repo B, even if the names match. If local cleanup cannot establish that the branch belongs to the selected PR, it should skip that cleanup.

Actual: gh pr close checks only whether feature-x exists in the current directory's Git repository, then runs git branch -D feature-x. Git's merged-branch safety check is bypassed, so unpushed local commits can lose their branch reference.

Logs

The isolated mocked reproduction passed:

=== RUN   TestIssueReproURLDeletesSameNamedLocalBranch
--- PASS: TestIssueReproURLDeletesSameNamedLocalBranch (0.00s)
PASS

The relevant paths are DeleteLocalBranch = !cmd.Flags().Changed("repo"), the name-only local branch lookup and deletion, and git branch -D.