Skip to content

Conversation

@bayandin
Copy link

@bayandin bayandin commented Dec 10, 2025

This PR adds support for explicitly specifying the head repository when creating pull requests. This enables creating PRs from same-org forks and cases where the head repository cannot be automatically determined.

  • New flag: --head-repo <owner/repo> to specify the repository containing the head branch
  • Requires --head to be specified and must be a plain branch name (not user:branch format)

Example:

gh pr create --head-repo owner/repo --head feature-branch --title "My PR" --body "Description" 

Note: A comment on the original issue (#6462 (comment)) raised concerns about GitHub Enterprise Server support. While headRepositoryId was added to GHES 3.8, that version was discontinued on 2024-03-26 already. Therefore, adding it without feature detection.

Fixes #6462
Fixed #10093

@bayandin bayandin requested a review from a team as a code owner December 10, 2025 20:53
@bayandin bayandin requested a review from BagToad December 10, 2025 20:53
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Dec 10, 2025
@github-actions
Copy link

Thank you for your pull request! 🎉

This PR appears to fix the following issues that are not labeled with help wanted Contributions welcome :

As outlined in our Contributing Guidelines, we expect that PRs are only created for issues that have been labeled help wanted.

While we appreciate your initiative, please note that:

  • PRs for non-help wanted issues may not be reviewed immediately as they might not align with our current priorities
  • The issue might already be assigned to a team member or planned for a specific release
  • We may need to close this PR. For example, if it conflicts with ongoing work or architectural decisions

What happens next:

  • Our team will review this PR and the associated issues
  • We may add the help wanted label to the issues, if appropriate, and review this pull request
  • In some cases, we may need to close the PR. For example, if it doesn't fit our current roadmap

Thank you for your understanding and contribution to the project! 🙏

This comment was automatically generated by cliAutomation.

@williammartin
Copy link
Member

Think this also fixes #10093 which is effectively a duplicate of part of #6462

@bayandin
Copy link
Author

Think this also fixes #10093 which is effectively a duplicate of part of #6462

Thanks for pointing that out! Added the referent to #10093 to the PR description

Copy link
Member

@andyfeller andyfeller left a comment

Choose a reason for hiding this comment

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

Thanks for opening this PR, @bayandin! ✨

I was doing some local testing and can confirm this works to open up intra-org PRs across forks 💯

My main concern is enhancing this behavior to determine some of this context from the local repository to reduce the number of flags a user has to provide.

As a side note, I think this work will expose some other places within gh pr and other commands where the existing cross-repo PR logic doesn't show the exact information we would want showing up such as gh pr view showing the org/repo:branch, which can be a follow up.

tinyfists/fork ‹12286-test›$ gh repo set-default
This command sets the default remote repository to use when querying the
GitHub API for the locally cloned repository.

gh uses the default repository for things like:

 - viewing and creating pull requests
 - viewing and creating issues
 - viewing and creating releases
 - working with GitHub Actions

### NOTE: gh does not use the default repository for managing repository and environment secrets.

? Which repository should be the default?  [Use arrows to move, type to filter]
> tinyfists/upstream
  tinyfists/fork


tinyfists/fork ‹12286-test›$ gh pr view 1                          
Working...
Add file for cross-repo PR testing tinyfists/upstream#1
Open • andyfeller wants to merge 1 commit into main from 12286-test • about 4 hours ago
+3 -0 • No checks


  No description provided


View this pull request on GitHub: https://github.com/tinyfists/upstream/pull/1

Comment on lines +329 to +338
if opts.HeadRepo != "" {
if opts.HeadBranch == "" {
return cmdutil.FlagErrorf("`--head-repo` requires `--head` to be specified")
}
// When --head-repo is provided, --head must be a plain branch name (no colon)
// because headRepositoryId and qualified headRefName (user:branch) are mutually exclusive
if strings.Contains(opts.HeadBranch, ":") {
return cmdutil.FlagErrorf("`--head-repo` cannot be used with `--head` in `user:branch` format. Use a plain branch name with `--head` when specifying `--head-repo`")
}
}
Copy link
Member

Choose a reason for hiding this comment

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

question: should this require --head when being called locally within a repository?

I can see this being required if calling this outside of a local repo, however I would expect the head branch and repo to be determined from the local context if possible.

Comment on lines 353 to +354
fl.StringVarP(&opts.HeadBranch, "head", "H", "", "The `branch` that contains commits for your pull request (default [current branch])")
fl.StringVar(&opts.HeadRepo, "head-repo", "", "The `repository` that contains the head branch (owner/repo format)")
Copy link
Member

Choose a reason for hiding this comment

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

suggest: I think gh users familiar using forks will expect --head owner:branch to work.

To be clear, I don't personally like the complex formatting behind the existing flag as it is leaking a detail of the GitHub API. However, I think having this new flag as a one-off for cross-org forks is inconsistent experience.

@bayandin bayandin force-pushed the gh-pr-create-head_repo branch from d00e979 to 86ef121 Compare December 15, 2025 18:24
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.

Enhance gh pr create to support cross repo pull requests within the same organization gh and REST API unable to work with same-org forked repos

4 participants