mislav/hub · error

Error creating fork: %s already exists on %s

Error message

Error creating fork: %s already exists on %s

What it means

gh's `fork` command found that a repo with the fork name already exists on the target host, but its recorded parent does not match the repo you asked to fork (or the parent URL could not be parsed). The command refuses to proceed because the existing repo is not actually a fork of the requested project, so forking would be misleading. It is a pre-flight consistency check in `fork` before any fork API call is made.

Source

Thrown at commands/fork.go:85

	} else {
		newRemoteName = forkProject.Owner
	}

	client := github.NewClient(project.Host)
	existingRepo, err := client.Repository(forkProject)
	if err == nil {
		existingProject, err := github.NewProjectFromRepo(existingRepo)
		if err == nil && !existingProject.SameAs(forkProject) {
			existingRepo = nil
		}
	}
	if err == nil && existingRepo != nil {
		var parentURL *github.URL
		if parent := existingRepo.Parent; parent != nil {
			parentURL, _ = github.ParseURL(parent.HTMLURL)
		}
		if parentURL == nil || !project.SameAs(parentURL.Project) {
			err = fmt.Errorf("Error creating fork: %s already exists on %s",
				forkProject, forkProject.Host)
			utils.Check(err)
		}
	} else {
		if !args.Noop {
			newRepo, err := client.ForkRepository(project, params)
			utils.Check(err)
			forkProject.Owner = newRepo.Owner.Login
			forkProject.Name = newRepo.Name
		}
	}

	args.NoForward()
	if !args.Flag.Bool("--no-remote") {
		originURL := project.GitURL("", "", false)
		url := forkProject.GitURL("", "", true)

		// Check to see if the remote already exists.

View on GitHub (pinned to 5c547ed804)

Solutions

  1. Delete or rename the existing same-named repo (via web UI or `gh delete-repo`) and retry the fork.
  2. Verify the existing repo is actually a fork of the intended source; if it is, no new fork is needed — add it as a remote instead.
  3. Check you are targeting the correct host (github.com vs Enterprise); a wrong host can make the parent comparison fail.
  4. If the upstream was renamed/transferred, update your local remote URLs and retry.

Example fix

// before
git fork owner/name   # fails: fork-name already exists but isn't a fork of owner/name
// after
# delete the stale repo first, then
git fork owner/name
Defensive patterns

Strategy: validation

Validate before calling

// Check the target repo state before forking
existing, _ := client.Repository(targetProject)
if existing != nil {
    isForkOfSource := existing.Parent != nil &&
        parentProjectOf(existing.Parent) == sourceProject
    if !isForkOfSource {
        fmt.Printf("%s already exists and is not a fork of %s; delete or rename it first\n",
            targetProject, sourceProject)
        os.Exit(1)
    }
}

Prevention

When it happens

Trigger: Running `gh repo fork owner/name` (or `git fork ...`) where `client.Repository(project)` returns an existing repo whose `Parent.HTMLURL` project differs from the source project, or whose Parent is nil/unparseable, while no `--noop` flag path is taken.

Common situations: A repo with the same name already exists under your account but was created independently (not forked); you forked the repo long ago and the upstream was renamed/transferred so the recorded Parent no longer matches; GitHub Enterprise vs github.com host mismatch causing project comparison to fail.

Related errors


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