Skip to content

gh pr checkout fails when git remote host differs from GitHub API host #12179

Open

Description

@fotoetienne

Summary

Commands like gh pr checkout, gh pr create, and gh repo fork fail with "none of the git remotes configured for this repository correspond to the GH_HOST environment variable" when the git remote hostname differs from the GitHub API hostname, even though this is a legitimate enterprise configuration.

Problem

In enterprise environments with git proxies or SSH bastions, git operations may route through a different hostname than API operations:

  • Git operations: git.company.com (proxy/bastion)
  • API operations: github.company.com (GitHub Enterprise)

gh currently rejects this configuration and returns an error, preventing commands that perform git operations from working.

Reproduction

Setup:

# Repository with git remote through proxy
$ git remote -v
origin  [email protected]:corp/repo.git

# Authenticated to GitHub Enterprise
$ gh auth login --hostname github.company.com
✓ Logged in as user

# Set GH_HOST to point to API
$ export GH_HOST=github.company.com

Error:

$ gh pr checkout 123
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

Commands affected:

  • gh pr checkout
  • gh pr create
  • gh repo fork
  • gh repo sync
  • gh issue develop

Commands that work fine:

  • gh pr view ✓
  • gh pr list ✓
  • gh issue view ✓
  • gh api ✓

Root Cause

In pkg/cmd/factory/remote_resolver.go:82-92, gh returns an error when no git remotes match authenticated hosts, even though this validation is unnecessarily strict for commands performing git operations.

Analysis

For git operations, gh:

  • ✅ Matches remotes by owner/repo (not hostname)
  • ✅ Uses remote name like "origin" for git commands
  • ❌ Never actually uses the remote hostname

For API operations, gh:

  • ✅ Uses hostname from GH_HOST or auth config
  • ✅ Already validated separately
  • ❌ Never uses git remote hostname

The git remote hostname and API hostname are separate concerns that shouldn't be coupled.

Real-World Use Cases

This affects enterprises using:

  1. Git proxies - Route git traffic through internal proxies for security/auditing

    • Example: Netflix uses git.netflix.net for git ops, github.netflix.net for API
  2. SSH bastions - Git operations through jump hosts for security

    • Example: git.bastion.company.com → github.company.com
  3. Git mirrors - Internal mirrors for performance with different hostnames

    • Example: git.mirror.company.com → github.company.com
  4. Split DNS - Different internal/external hostnames

    • Example: git.internal.company.com → github.company.com
  5. CDN/load balancers - Git traffic load balanced separately

    • Example: git.cdn.company.com → github.company.com

Expected Behavior

gh pr checkout 123 should:

  1. Match git remote by owner/repo (regardless of hostname)
  2. Use the matched remote for git operations
  3. Use GH_HOST for API operations
  4. Work successfully when both are available

The hostname mismatch should not be an error since these are separate concerns.

Current Workaround

Enterprises currently must maintain wrapper scripts that reimplement portions of gh CLI functionality, specifically pr checkout, to work around this limitation.

Additional Context

  • This is a regression from expected git behavior where remotes are matched by repository identity, not hostname
  • API authentication is already properly validated through GH_HOST and auth config
  • The hostname filtering is overly defensive and breaks legitimate enterprise setups
  • I have a working fix for this that I can contribute via PR

Impact

Without this fix:

  • ❌ Enterprise users cannot use gh pr checkout with git proxies
  • ❌ Teams must maintain custom wrapper scripts
  • ❌ Reduced adoption of gh CLI in enterprise environments

With this fix:

  • ✅ Works with git proxies, bastions, and mirrors
  • ✅ Maintains all security through proper API auth
  • ✅ No impact on standard configurations
  • ✅ Better separation of concerns

Environment:

  • gh version: 2.83.1
  • OS: macOS, Linux
  • GitHub type: GitHub Enterprise Server

Note: I have a tested implementation of a fix for this issue. See PR #12180.

Activity

  1. added
    needs-triage
    on Nov 21, 2025
  2. added 2 commits that reference this issue on Nov 21, 2025
    78008cc
    4d7b893
  3. williammartin commented on Nov 24, 2025

    @williammartin
    Member

    Oooh, this looks like a duplicate of #10891. Will have to double check though!

  4. added
    gh-pr
    on Nov 24, 2025
  5. added 2 commits that reference this issue on Jan 22, 2026
    db34fa7
    5410e10
  6. added
    enhancement
    and removed
    needs-triage
    on Mar 2, 2026
  7. github-actions commented on Mar 2, 2026

    @github-actions
    Contributor

    Thank you for your issue! We have categorized it as an enhancement (feature or improvement) 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.

  8. BagToad commented on Mar 2, 2026

    @BagToad
    Member

    I don't think this is a duplicate of #10891; they share the same error message but have different root causes. #10891 appears to be about SSH-format remotes (git@) not resolving on the same host, while this issue is about intentionally different hostnames (git proxy/bastion vs API host).

    I closed #12180 because the proposed fix appeared to remove diagnostic error messages that help users troubleshoot other configuration issues. I think this needs more investigation and design work.

  9. added
    needs-investigation
    core
    on Mar 2, 2026
  10. davidlin-stripe commented on May 9, 2026

    @davidlin-stripe

    Hi team, has there been any updates on the investigation? I think it's a valid enterprise setup that is currently not nicely supported due to the way the resolver is setup - there should be configuration that supports some kind of overwrite.

    Basically the pain has been coming from the git operations and the API operations hits different remotes, could we keep the entries in hosts.yml as the canonical host that handles the git operations but add a new configuration entry like api_base_url or api_host attached to the canonical host, and we can inject this at the http client level to perform the overwrite on the API operations - the rest of the logics remain unchanged and it makes the resolver happy.

    The caveat is the setup requires some explicit configuration, which often is already done or documented for folks in an enterprise setting, and the gh auth login requires some proper care. We would need an extra flag to configure the api host at the auth time and reasonably assume the auth urls is based on api host instead of the git host, something like

    gh auth login --hostname git.company.com --api-host github.company.com
    

    I'd love to discuss this further - feedback or concern are welcomed.

  11. changed the title [-]gh pr checkout fails when git remote host differs from GitHub API host[/-] [+]`gh pr checkout` fails when git remote host differs from GitHub API host[/+] on May 11, 2026
  12. DerekStride commented on May 28, 2026

    @DerekStride

    I have a narrower variant of this issue that may avoid the concern raised in #12180.

    Setup:

    $ git remote -v
    github  https://github.com/cli/cli.git (fetch)
    github  https://github.com/cli/cli.git (push)
    origin  https://mirror.io/cli/cli.git (fetch)
    origin  https://mirror.io/cli/cli.git (push)

    The current branch tracks origin, but origin is a mirror and is filtered out by remote resolution. gh pr view then fails with:

     could not resolve remote "origin": no matching remote found

    However, there is an unambiguous GitHub remote available: github.

    It should be possible to scan the remote list to identify the correct org & repo, would this narrower approach be acceptable?

  13. williammartin commented on Jul 28, 2026

    @williammartin
    Member

    @davidlin-stripe I'm just back from parental leave and was picking up a similar thread from #13717 which looks almost identical and in fact, your PRs look similar at a glance. This has made its way to the top of my priority list. I haven't decided exactly whether these are precise duplicates yet, but in terms of having an api_host or similar field in config, I will be working over on that issue if you'd like to follow along.

  14. davidlin-stripe commented on Jul 28, 2026

    @davidlin-stripe

    @davidlin-stripe I'm just back from parental leave and was picking up a similar thread from #13717 which looks almost identical and in fact, your PRs look similar at a glance. This has made its way to the top of my priority list. I haven't decided exactly whether these are precise duplicates yet, but in terms of having an api_host or similar field in config, I will be working over on that issue if you'd like to follow along.

    Thank you for following up William, looking at #13717 it's largely identical to the use case I've described, I will keep an eye on it and it will be great to have this in the upstream!

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

    coreenhancementgh-prneeds-investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions