Skip to content

Add repo_default_remote config option - #12044

Closed
friederbluemle wants to merge 1 commit into
cli:trunkfrom
friederbluemle:default-remote
Closed

friederbluemle wants to merge 1 commit into
cli:trunkfrom
friederbluemle:default-remote

Conversation

@friederbluemle

@friederbluemle friederbluemle commented Oct 30, 2025 •

Copy link
Copy Markdown

Problem

At the moment, it’s a bit cumbersome having to explicitly set the default remote in every single local Git repository just to be able to use commands like gh repo, gh pr, gh issue, gh workflow, etc.

I constantly run into the following error:

X No default remote repository has been set.
To learn more about the default repository, run: gh repo set-default --help

please run `gh repo set-default` to select a default remote repository.

Context

For my workflow, the canonical default remote name origin always points to the source repository.
Fork remotes typically have short two-character initials (for user forks) or the organization name (for org forks).
In other words, origin is universally the correct default for my setup.

Solution

To avoid having to manually run gh repo set-default origin, I added a simple, optional global override:

gh config set repo_default_remote origin

Once set, I no longer see the “No default repository” error — gh automatically uses the specified remote (e.g., origin) when no explicit repo is configured.

Behavior / Precedence

The new repo_default_remote config key is an opt-in global setting.
When unset (the default), existing behavior remains unchanged.
When set, repository resolution follows this precedence (highest → lowest):

  1. -R / --repo flag
  2. GH_REPO environment variable
  3. Per-repo default set via gh repo set-default
  4. NEW (optional global fallback): repo_default_remote
  5. Otherwise → existing “No default repository…” error

Directly relates to #9399 (while it does not auto-default to any name, it provides a simple one-time way to solve the issue).

Also related:

#9398
#9149
#8659

Copilot AI review requested due to automatic review settings October 30, 2025 00:40
@friederbluemle
friederbluemle requested a review from a team as a code owner October 30, 2025 00:40
@cliAutomation cliAutomation added the external pull request originating outside of the CLI core team label Oct 30, 2025

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 introduces a new configuration option repo_default_remote that allows users to specify a default remote name for repository operations. When no per-repo resolution is already present (via --repo, GH_REPO, or gh repo set-default), the system will fall back to using the configured default remote name.

Key Changes:

  • Added repo_default_remote configuration key that accepts a remote name as its value
  • Modified SmartBaseRepoFunc to check for this configuration and use the named remote when no other resolution exists
  • Added comprehensive test coverage for the new fallback behavior

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
internal/config/config.go Adds the repoDefaultRemote configuration key constant and implements the RepoDefaultRemote method to retrieve it
internal/gh/gh.go Defines the RepoDefaultRemote interface method for the Config interface
internal/gh/mock/config.go Adds mock implementation of RepoDefaultRemote method for testing
pkg/cmd/factory/default.go Implements the logic to use repo_default_remote as a fallback when no per-repo resolution exists
pkg/cmd/factory/default_test.go Adds comprehensive test cases covering the new fallback behavior

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

key := entry.Value
if key != "" {
if named, nerr := remotes.FindByName(key); nerr == nil && named != nil {
return named.Repo, nil

Copilot AI Oct 30, 2025

Copy link

Choose a reason for hiding this comment

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

The code silently ignores errors when looking up the configured remote name. If a user explicitly configures repo_default_remote to a non-existent remote, they receive no feedback that their configuration is being ignored. Consider logging a warning or returning an error when the configured remote name cannot be found, so users are aware their configuration is not taking effect.

Suggested change
return named.Repo, nil
return named.Repo, nil
} else {
fmt.Fprintf(f.IOStreams.ErrOut, "warning: configured repo_default_remote %q not found among git remotes; falling back to default resolution\n", key)

Copilot uses AI. Check for mistakes.
Comment thread internal/config/config.go
},
{
Key: repoDefaultRemote,
Description: "the default remote name to use for gh repo/pr/issue/workflow/etc.",

Copilot AI Oct 30, 2025

Copy link

Choose a reason for hiding this comment

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

The description mentions 'gh repo/pr/issue/workflow/etc.' but this configuration only affects base repo resolution in SmartBaseRepoFunc. Commands that don't use this function won't be affected. The description should be more precise about its scope, e.g., 'the default remote name to use when resolving the base repository for commands'.

Suggested change
Description: "the default remote name to use for gh repo/pr/issue/workflow/etc.",
Description: "the default remote name to use when resolving the base repository for commands",

Copilot uses AI. Check for mistakes.
Comment thread pkg/cmd/factory/default.go Outdated
Comment on lines +170 to +176
var hasResolved bool
for _, r := range remotes {
if r.Resolved != "" {
hasResolved = true
break
}
}

Copilot AI Oct 30, 2025

Copy link

Choose a reason for hiding this comment

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

This loop iterates through all remotes to check if any have a Resolved field set. Since context.Remotes already has a ResolvedRemote() method that performs this exact check (see context/remote.go lines 51-58), you should use that method instead: _, err := remotes.ResolvedRemote(); hasResolved := err == nil. This eliminates code duplication and improves maintainability.

Suggested change
var hasResolved bool
for _, r := range remotes {
if r.Resolved != "" {
hasResolved = true
break
}
}
_, err := remotes.ResolvedRemote()
hasResolved := err == nil

Copilot uses AI. Check for mistakes.
@friederbluemle
friederbluemle force-pushed the default-remote branch 2 times, most recently from 9cbc3a8 to c1c0fa4 Compare October 31, 2025 07:53
@friederbluemle

Copy link
Copy Markdown
Author

Rebased onto latest default branch.

@friederbluemle

Copy link
Copy Markdown
Author

Rebased again.

Can @babakks (or someone else) take a look please?

@babakks

babakks commented Nov 4, 2025 •

Copy link
Copy Markdown
Member

Thanks for this, @friederbluemle! 🙏

I have already explained on the issue why we're closing this PR. See #9399 (comment).

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.

4 participants