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

Skip to content

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

@Shion1305

Description

@Shion1305

Describe the feature or problem you’d like to solve

When running gh pr create, if the local branch is behind or doesn’t exist on the remote, the CLI prompts to push the branch before proceeding. However, if the current branch happens to be main, this behavior results in accidentally pushing commits directly to the main branch — which is usually unintended and potentially harmful.

Additionally, creating a pull request from the main branch to the main branch is not valid, and in this situation, the command flow can be confusing. When a user runs gh pr create from the base branch (e.g., main), it implies that the user intends to create a PR targeting that same base branch — which is not possible. The CLI should handle this case more intelligently.

Proposed solution

When a user runs gh pr create while on the base branch (e.g., main or master), instead of prompting to push the branch, the CLI should:

  • Abort the process automatically with a clear message, such as:
    “You are currently on the base branch (main). Creating a pull request from this branch is not allowed.”

    or

  • Offer an explicit prompt to create a new branch and continue, for example:
    “You are on the main branch. Would you like to create a new branch for your pull request? (y/N)”

This change would prevent accidental pushes to the main branch and guide users toward the correct workflow for creating a pull request.

Additional context

This issue occurred when running gh pr create immediately after committing changes, forgetting to switch to a feature branch. The CLI prompted to push the branch — but since I was still on main, it pushed the main branch instead.

Adding this safeguard would prevent accidental pushes and clarify behavior when users attempt to create a PR from the base branch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-prrelating to the gh pr commandhelp wantedContributions welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions