mislav/hub · error

could not find a git remote for '%s/%s'

Error message

could not find a git remote for '%s/%s'

What it means

RemoteForRepo(project) iterates the loaded remotes and returns the first whose Project() matches the given owner/name. If no remote points at that GitHub repository, it returns this error including owner/name. It exists so callers can map a GitHub repo back to its local remote.

Source

Thrown at github/localrepo.go:222

	if err := r.loadRemotes(); err != nil {
		return nil, err
	}

	repoURL, err := url.Parse(repo.HTMLURL)
	if err != nil {
		return nil, err
	}

	project := NewProject(repo.Owner.Login, repo.Name, repoURL.Host)

	for _, remote := range r.remotes {
		if rp, err := remote.Project(); err == nil {
			if rp.SameAs(project) {
				return &remote, nil
			}
		}
	}
	return nil, fmt.Errorf("could not find a git remote for '%s/%s'", repo.Owner.Login, repo.Name)
}

func (r *GitHubRepo) RemoteForProject(project *Project) (*Remote, error) {
	if err := r.loadRemotes(); err != nil {
		return nil, err
	}

	for _, remote := range r.remotes {
		remoteProject, err := remote.Project()
		if err == nil && remoteProject.SameAs(project) {
			return &remote, nil
		}
	}
	return nil, fmt.Errorf("could not find a git remote for '%s'", project)
}

func (r *GitHubRepo) MainRemote() (*Remote, error) {
	r.loadRemotes()

View on GitHub (pinned to 5c547ed804)

Solutions

  1. Add a remote pointing to the target repo: git remote add <name> <url-of-owner/repo>
  2. Check `git remote -v` and confirm a URL actually contains owner/repo
  3. Match against the remote you actually have (e.g. origin for the fork)
  4. Fix URL rewrites (url.<base>.insteadOf in git config) that distort remote URLs

Example fix

// before
repo.RemoteForRepo(canonicalProject) // no remote for org/repo
// after
cmd := exec.Command("git", "remote", "add", "upstream", "https://github.com/org/repo.git")
cmd.Run()
repo.RemoteForRepo(canonicalProject) // now matches upstream
Defensive patterns

Strategy: fallback

Validate before calling

out, _ := exec.Command("git", "remote", "-v").Output()
hasMatch := strings.Contains(string(out), "github.com/"+owner+"/"+name)
if !hasMatch { /* add a remote for owner/name or use another lookup */ }

Try / catch

remote, err := repo.RemoteForRepo(project)
if err != nil {
    if strings.Contains(err.Error(), "could not find a git remote for") {
        // fall back to MainRemote or prompt user to add the remote
        return repo.MainRemote()
    }
    return nil, err
}

Prevention

When it happens

Trigger: Calling RemoteForRepo(&Project{Owner:"owner", Name:"repo"}) when no configured remote URL resolves to owner/repo — e.g. remote points at a fork, a different org, or uses a URL form the parser cannot match.

Common situations: Fork-based workflow where origin points to your fork and upstream to the canonical repo, but you look up the canonical repo which has no local remote; remotes configured with SSH aliases in ~/.ssh/config or insteadOf rewrites the parser cannot resolve; remote removed after lookup.

Related errors


AI-assisted analysis of mislav/hub@5c547ed804 (2026-09-01). Data as JSON: /api/errors/a67c342e1ded0a65. Report an issue: GitHub.