Skip to content

gh pr view should use repo’s remote host when GH_HOST points to a different server #11935

Description

@Shion1305

Describe the feature or problem you’d like to solve

When running gh pr view (or similar commands) with the GH_HOST environment variable set to a GitHub Enterprise server, the command raises an error, even if the current repository’s origin clearly points to a public GitHub repository on github.com.

This behavior is inconvenient for users who work with both GitHub Enterprise and public GitHub, as it forces them to unset or reconfigure GH_HOST whenever switching contexts — even when the correct server is already obvious from the repository’s Git configuration.

Proposed solution

The CLI could automatically detect the appropriate host based on the repository’s remote URL.

If the GH_HOST environment variable is set but the current repository’s remote clearly points to a different GitHub host (for example, github.com), gh should:

  • Prefer the host detected from the repository’s remote, or

  • At least issue a clear warning and offer to proceed using the detected host.

This improvement would make the CLI more intuitive for users working across public GitHub server and Enterprise server, reducing configuration friction and improve user experience.

Additional context

Example reproduction:

git clone https://github.com/cli/cli.git

export GH_HOST=github.mycompany.com

gh repo view -w
> none of the git remotes configured for this repository correspond to the GH_HOST environment variable. Try adding a matching remote or unsetting the variable

unset GH_HOST

gh repo view -w
> works now

Activity

  1. github-actions commented on Oct 14, 2025

    @github-actions
    Contributor

    Thank you for your issue! We have categorized it as a feature request, and it has been added to our backlog. In doing so, we are not committing to implementing this feature at this time, but, we will consider it for future releases based on community feedback and our own product roadmap.

    Unless you see the help wanted Contributions welcome label, we are not currently looking for external contributions for this feature.

    If you come across this issue and would like to see it implemented, please add a thumbs up! This will help us prioritize the feature. Please only comment if you have additional information or viewpoints to contribute.

  2. babakks commented on Oct 23, 2025

    @babakks
    Member

    Thanks for the feedback, @Shion1305! 🍻

    I understand the frustration, but as a rule of thumb env vars are meant to override gh normal behaviour. So, setting a global GH_HOST env var is a bad practice unless it's used in an isolated/controlled environment (e.g. CI workflows). Please note that gh is smart about hosts and uses the git remotes to derive the right hostname, of course, unless the GH_HOST override is in place.

    That said, can you please tell me more about your setup/situation so that I can see if there's a way to have an effortless configuration.

  3. Shion1305 commented on Oct 23, 2025

    @Shion1305
    ContributorAuthor

    Hi @babakks, thanks again for the thoughtful reply! 😊

    To explain my setup a bit more — I regularly work across multiple GitHub Enterprise servers and my personal GitHub.com account.
    As far as I know, there’s currently no way to define a “default” server within gh itself, so I’ve been setting the GH_HOST variable in my .zshrc to one of the Enterprise hosts I use most often.

    The issue is that this setup quickly becomes inconvenient:

    • When I clone a repository from a different Enterprise instance or from GitHub.com, I need to manually switch the GH_HOST each time.
    • If I open a new terminal session, I have to remember to re-export the correct host for the project I’m working on.
    • Many gh commands don’t seem to consider the obvious intent when there’s only a single remote configured — they’ll still fail due to a mismatched GH_HOST.

    So my improvement suggestions would be:

    1. Add an internal way to specify a default host — so that users don’t have to rely solely on environment variables.
    2. Make gh smarter about host detection — for example, if a repository has just one remote, gh could automatically infer that remote’s host and proceed accordingly.
      Even limiting this behavior to public GitHub (github.com) would already be a huge quality-of-life improvement.

    Hope that helps clarify my situation! Totally understand the current design philosophy around env vars, but this kind of flexibility could make gh a lot smoother for folks who bounce between multiple hosts every day🙏

  4. babakks commented on Nov 17, 2025

    @babakks
    Member

    Thanks, @Shion1305! 🙏 I got your point, but I'm afraid, I'm still not in favour of modifying gh's behaviour around GH_HOST. It's definitely a breaking change, even if in some cases it provides a smoother experience.

    I think the right approach to your use case is to keep multiple gh configurations, as I have explained in another issue (See #12145 (comment)). You'd still have to manually override an env var to pick the config you need, but it's less frustrating than setting GH_HOST with less unexpected results.

    Can you please try that and let me know if that helps with your case?

  5. github-actions commented on Dec 18, 2025

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has not had any activity in the last 30 days,
    and it will be closed in 30 days if no further activity occurs.

    If you think this is a mistake, please comment on this issue to keep it open.

  6. github-actions commented on Jan 17, 2026

    @github-actions
    Contributor

    This issue has been automatically closed due to inactivity.

    If you think this is a mistake, please comment on this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementa request to improve CLIgh-prrelating to the gh pr commandmore-info-neededMore info needed from user/contributorneeds-triageneeds to be reviewedstale

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions