Skip to content

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

Open

Agent suggestions

Public preview

Description

@sean-from-japan

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.

Activity

  1. cli-triage commented on Sep 29, 2026

    @cli-triage

    Thanks for the detailed report and reproduction. This looks like a real bug: I verified the cited code at commit 9b03115 — DeleteLocalBranch is enabled whenever --repo isn't set, and the subsequent cleanup matches the PR's head branch by name only in the cwd's repository before calling DeleteLocalBranch, which runs git branch -D and bypasses git's merged-branch safety check. This matches the hypothesis: closing a PR from an unrelated repo with a same-named local branch can force-delete that branch, risking loss of unpushed commits.

    This is the same class of issue as #14537 (gh pr merge --delete-branch), which already has a fix PR (#14544) for the merge path — but as the reporter notes, pr close has its own separate cleanup code, so that fix doesn't cover this case. I'm suggesting bug, priority-2 (data-loss risk but requires a specific cross-repo same-branch-name setup), and gh-pr.

    Generated by Issue Triage (skills-driven) for #14550 · copilot · auto · 46 AIC · ⌖ 2.21 AIC · ⊞ 9.5K · ◷

  2. babakks commented on Oct 6, 2026

    @babakks
    Member

    Thanks for reporting this and confirming the separate cleanup path, @sean-from-japan! 🙏

    This is the same underlying bug as #14537. I am leaving this issue open because gh pr close has a separate cleanup path and should have its own regression coverage. However, the fix should address both commands and ensure that a local branch is not deleted unless it can be associated safely with the selected pull request.

  3. added
    bug
    gh-pr
    priority-3
    on Oct 6, 2026
  4. removed
    needs-triage
    on Oct 6, 2026
  5. akasakariko commented on Oct 12, 2026

    @akasakariko

    I'd like to work on this

  6. akasakariko commented on Oct 16, 2026

    @akasakariko

    @cli/code-reviewers I'd like to confirm whether external contributions are welcome on this issue

    The contribution guide asks for a help wanted label and clear acceptance criteria before external fixes
    This issue currently has bug, priority-3, and gh-pr, but no help wanted

    Could you add help wanted (or confirm eligibility) and clarify the accepted scope if this is open for outside PRs

  7. secheverrianogal-beep commented on Oct 17, 2026

    @secheverrianogal-beep

    Thanks for confirming that #14550 has a separate cleanup path from #14537. I would make local-branch deletion conditional on proving that the current local repository corresponds to the PR head repository and that the local branch is associated with the resolved PR head; a matching branch name alone is not enough.

    When a PR is selected by URL from an unrelated working repository, or that association cannot be verified, gh should close the remote PR normally but skip local deletion and print a clear warning rather than running git branch -D against the current repository.

    The intended cleanup behavior should remain available when the local repository and branch are actually the PR head.

    Please add regression tests for:

    1. A PR URL from repository A invoked while in repository B, with a same-named local branch containing an unpushed commit. The branch must not be deleted.
    2. The valid matching-repository case, where the intended cleanup behavior is preserved.
    3. The independent gh pr merge --delete-branch path in gh pr merge <url> --delete-branch removes a worktree and deletes a same-named branch in the cwd's unrelated repository #14537.

    Fixing one command path should not be assumed to cover the other.

  8. sean-from-japan commented on Oct 18, 2026

    @sean-from-japan
    Author

    As the reporter, I'd also be glad to work on the fix if this is marked help wanted. I would follow the direction in #14537: delete the local branch only when it can be tied to the selected PR (for example through its configured remote ref or the PR head SHA), and otherwise skip local cleanup, for both pr close and pr merge, with regression tests for each path, including turning the mocked reproduction from the report into a test. If the team would rather fix this internally, that's completely fine too.

  9. akasakariko commented on Oct 18, 2026

    @akasakariko

    Stepping back — the reporter would like to work on the fix, leaving it to them

  10. Alquimista888 commented on Oct 18, 2026

    @Alquimista888

    2 años de trabajo se fueron a la basura por un error de comandos, la mala interpretación de los mismos al ejecutar con una velocidad que no fue la correcta ppero su interfaz , tengo pruebas de con tar con 2 correos múltiples carteras y un promedio de más de 500 cryptomonedas de igual forma que carteras en casi todas las plataformas que se solicitó trabajar
    No sé que decir ya que realmente nunca hubo una mala intención por obtener algo en lo cual no hubiese puesto mi atención y empeño

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