You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gh pr close <URL> --delete-branch can force-delete a same-named branch in an unrelated repository #14550
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
Have an open PR in owner-a/repo-a with head branch feature-x.
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.
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
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.
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.
@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
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:
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.
The valid matching-repository case, where the intended cleanup behavior is preserved.
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.
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
Activity
cli-triage commentedon Sep 29, 2026
Thanks for the detailed report and reproduction. This looks like a real bug: I verified the cited code at commit
9b03115—DeleteLocalBranchis enabled whenever--repoisn't set, and the subsequent cleanup matches the PR's head branch by name only in the cwd's repository before callingDeleteLocalBranch, which runsgit branch -Dand 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 themergepath — but as the reporter notes,pr closehas its own separate cleanup code, so that fix doesn't cover this case. I'm suggestingbug,priority-2(data-loss risk but requires a specific cross-repo same-branch-name setup), andgh-pr.babakks commentedon Oct 6, 2026
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 closehas 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.akasakariko commentedon Oct 12, 2026
I'd like to work on this
akasakariko commentedon Oct 16, 2026
@cli/code-reviewers I'd like to confirm whether external contributions are welcome on this issue
The contribution guide asks for a
help wantedlabel and clear acceptance criteria before external fixesThis issue currently has
bug,priority-3, andgh-pr, but nohelp wantedCould you add
help wanted(or confirm eligibility) and clarify the accepted scope if this is open for outside PRssecheverrianogal-beep commentedon Oct 17, 2026
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,
ghshould close the remote PR normally but skip local deletion and print a clear warning rather than runninggit branch -Dagainst 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:
gh pr merge --delete-branchpath ingh pr merge <url> --delete-branchremoves 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.
sean-from-japan commentedon Oct 18, 2026
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 bothpr closeandpr 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.akasakariko commentedon Oct 18, 2026
Stepping back — the reporter would like to work on the fix, leaving it to them
Alquimista888 commentedon Oct 18, 2026
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