Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@majiayu000
Copy link

Summary

When gh pr create is invoked with head and base refs pointing to the same ref in the same repository, the command now returns an error instead of proceeding with an invalid PR creation.

This addresses the issue where running gh pr create from the base branch (e.g., main) would accidentally push commits to main.

Changes

  • Added isSameRef() helper function to check if head and base point to identical refs
  • Added validation in createRun() that errors early when refs are identical
  • Cross-repository PRs (e.g., from forks) with the same branch name continue to work

Test plan

  • Added unit tests for isSameRef() function covering:
    • Same branch in same repo (should error)
    • Different branches in same repo (should work)
    • Same branch name in different repos/forks (should work)
  • Added integration tests in Test_createRun for the error case
  • All existing tests pass

Fixes #11903

When a user runs `gh pr create` with head and base refs pointing to
the same ref in the same repository, the command now exits with an
error instead of proceeding with an invalid PR creation.

This prevents accidental pushes to the main branch and provides clear
feedback when users attempt to create a PR from the base branch.

Cross-repository PRs (e.g., from forks) with the same branch name
continue to work as expected.

Fixes cli#11903

Signed-off-by: majiayu000 <[email protected]>
@majiayu000 majiayu000 requested a review from a team as a code owner December 26, 2025 11:14
@majiayu000 majiayu000 requested a review from BagToad December 26, 2025 11:14
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Dec 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external pull request originating outside of the CLI core team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gh pr create should warn or abort when run from the base branch (e.g., main)

2 participants