Skip to content

fix: error when head and base refs are identical in pr create#12376

Merged
BagToad merged 3 commits intocli:trunkfrom
majiayu000:fix-11903-pr-create-same-ref
Jan 21, 2026
Merged

fix: error when head and base refs are identical in pr create#12376
BagToad merged 3 commits intocli:trunkfrom
majiayu000:fix-11903-pr-create-same-ref

Conversation

@majiayu000
Copy link
Contributor

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 <1835304752@qq.com>
@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
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where gh pr create would allow creating a pull request when the head and base refs point to the same branch in the same repository, which would result in an invalid PR and could accidentally push commits directly to the base branch (e.g., main).

Changes:

  • Added isSameRef() helper function that detects when head and base refs are identical in the same repository while still allowing cross-repository PRs (e.g., from forks) with the same branch name
  • Added early validation in createRun() that returns a clear error message when attempting to create a PR with identical head and base refs
  • Added comprehensive unit tests for isSameRef() covering same-repo, different-repo, and different ref type scenarios
  • Added integration tests in Test_createRun validating the error cases

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
pkg/cmd/pr/create/create.go Added isSameRef() helper function and validation check in createRun() to prevent PR creation when head and base refs are identical in the same repository
pkg/cmd/pr/create/create_test.go Added comprehensive unit tests for isSameRef() and integration tests for the error cases in Test_createRun

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@BagToad BagToad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@majiayu000 this looks very good, thank you and apologies for the review delay 🙏 I just left a couple comment nitpicks that once we resolve, I think we're good to merge.

  • Thank you for testing our createRun func alongside the helper function, ensuring the actual use of the function is tested.
  • Thank you for leveraging the creationRefs type for this feature.
  • Overall, thank you for your adherence to existing patterns!

Signed-off-by: majiayu000 <1835304752@qq.com>
Copy link
Member

@BagToad BagToad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @majiayu000 this looks good 🙏

@BagToad BagToad merged commit 49f7223 into cli:trunk Jan 21, 2026
11 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Jan 22, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [cli/cli](https://github.com/cli/cli) | minor | `v2.85.0` → `v2.86.0` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>cli/cli (cli/cli)</summary>

### [`v2.86.0`](https://github.com/cli/cli/releases/tag/v2.86.0): GitHub CLI 2.86.0

[Compare Source](cli/cli@v2.85.0...v2.86.0)

#### :copilot:  Install and run GitHub Copilot CLI directly from `gh`

Since we deprecated the [GitHub Copilot in the CLI](https://github.blog/changelog/2025-09-25-upcoming-deprecation-of-gh-copilot-cli-extension/) extension in favor of the new agentic [GitHub Copilot CLI](https://github.com/github/copilot-cli), we want to give developers using `gh` a simple way to get started using our most powerful terminal assistant.

- `gh copilot` will prompt to install, then run Copilot CLI
- `gh copilot <args>` will execute the Copilot CLI, forwarding any arguments and flags

For more information and usage options, run `gh copilot --help`.

#### What's Changed

##### ✨ Features

- `gh copilot`: add native `copilot` command to execute/install copilot cli by [@&#8203;devm33](https://github.com/devm33) in [#&#8203;12444](cli/cli#12444)
- `gh cache delete`: allow for delete all caches for a ref by [@&#8203;davidspek](https://github.com/davidspek) in [#&#8203;12101](cli/cli#12101)
- `gh pr create`: error when head and base refs are identical in pr create by [@&#8203;majiayu000](https://github.com/majiayu000) in [#&#8203;12376](cli/cli#12376)

##### 📚 Docs & Chores

- Fix Windows asset URL in `copilot` command tests by [@&#8203;babakks](https://github.com/babakks) in [#&#8203;12500](cli/cli#12500)
- Update contributing guidelines for clarity by [@&#8203;BagToad](https://github.com/BagToad) in [#&#8203;12505](cli/cli#12505)

#### New Contributors

- [@&#8203;devm33](https://github.com/devm33) made their first contribution in [#&#8203;12444](cli/cli#12444)
- [@&#8203;davidspek](https://github.com/davidspek) made their first contribution in [#&#8203;12101](cli/cli#12101)

**Full Changelog**: <cli/cli@v2.85.0...v2.86.0>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi44Ni4xIiwidXBkYXRlZEluVmVyIjoiNDIuODYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6Om1pbm9yIl19-->
@bosniltd-a11y

This comment was marked as spam.

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)

4 participants