cli icon indicating copy to clipboard operation
cli copied to clipboard

How to create a PR when only forking a branch?

Open System233 opened this issue 1 year ago • 7 comments

CLI Feedback

I added a remote and checked out a remote branch, then after modifying the branch, I pushed it to my existing repository. What should I do next to create a PR from that branch to the remote repository?

Key points:

  • The two repositories do not have a fork relationship.
  • A repository has copied a branch from B repository, such as patch-A.
  • I want to create a PR from A/patch-A to B/main.
  • I don't have write permission on B
  • I don't want to fork repository B

Purpose

  • I want to centralize PR commits to multiple repositories, and centralize all the branches to be modified in different repositories into one repository.

Current

$ GH_REPO=OWN/REPO gh pr create 

Creating pull request for System233:XXX into main in OWN/REPO

pull request create failed: GraphQL: Head sha can't be blank, Base sha can't be blank, Head repository can't be blank, No commits between OWN:main and System233:XXX Head ref must be a branch, not all refs are readable (createPullRequest)

$ gh pr create 
X No default remote repository has been set for this directory.

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

$ gh repo set-default OWN/REPO
$ gh repo view
X No default remote repository has been set for this directory.

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

System233 avatar Oct 15 '24 15:10 System233

Hey @System233, interesting use case!

How would you do this in the Web UI? Is it even possible? I thought GitHub only allowed PRs from forks.

williammartin avatar Oct 15 '24 15:10 williammartin

I've tried doing it in the WEB UI, as well as changing the URL, and can't achieve this. But just now I thought of another roundabout way to do it, maybe create an organization and fork repositories in the organization, thus avoiding a large number of forked repositories polluting individual accounts.

However, there is another problem here, that is, if don't set the GH_REPO environment variable, gh pr create will prompt the need to set the default repository, but after successfully setting upstream as the default repository with gh repo set-default, you will still be prompted for the need to set the default repository when use commands such as gh pr create or gh repo view.

I don't quite understand why gh pr create needs to set the default repository additionally, can't it be solved in the command line parameter?

System233 avatar Oct 15 '24 16:10 System233

I've tried doing it in the WEB UI, as well as changing the URL, and can't achieve this.

Yeh I just asked some colleagues and they agree that GitHub doesn't allow this flow at all.

But just now I thought of another roundabout way to do it, maybe create an organization and fork repositories in the organization, thus avoiding a large number of forked repositories polluting individual accounts.

Maybe you could describe the problem that you're actually trying to solve with the original approach, and I can offer some advice? What's the outcome you're trying to achieve?

but after successfully setting upstream as the default repository with gh repo set-default, you will still be prompted for the need to set the default repository when use commands such as gh pr create or gh repo view.

Are you saying that you run gh repo set-default and later when you run gh pr create it is erroring saying you need to run gh repo set-default? Can you provide a terminal snippet showing this? It shouldn't happen in any flow I can think of.

I don't quite understand why gh pr create needs to set the default repository additionally, can't it be solved in the command line parameter?

It might be inferable from --base if it were fully qualified like cli/cli:trunk but I'm not sure if that's implemented. Let's figure out the previous parts before spending time on this.

williammartin avatar Oct 15 '24 16:10 williammartin

Steps

  1. Prepare two repositories A and B
  2. run
$ git clone URL-TO-A
$ cd A
$ git remote add repo-b URL-TO-B
$ git fetch repo-b
$ git checkout -B main-b repo-b/main

$ echo test>test.log
$ git add .
$ git commit -m “Test”

$ git push origin

$ GH_REPO=OWN/B gh pr create 

Creating pull request for You:A into main in OWN/B

pull request create failed: GraphQL: Head sha can't be blank, Base sha can't be blank, Head repository can't be blank, No commits between OWN:main and You: main-b Head ref must be a branch. main-b Head ref must be a branch, not all refs are readable (createPullRequest)

$ gh pr create 
X No default remote repository has been set for this directory.

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

$ gh repo set-default OWN/B
$ gh repo view
X No default remote repository has been set for this directory.

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

System233 avatar Oct 15 '24 18:10 System233

Thanks. In this example do A and B have a fork relationship or are they unrelated repositories?

williammartin avatar Oct 15 '24 18:10 williammartin

Thanks. In this example do A and B have a fork relationship or are they unrelated repositories?

They're unrelated.

System233 avatar Oct 15 '24 19:10 System233

$ GH_REPO=OWN/REPO gh pr create

Creating pull request for System233:XXX into main in OWN/REPO

pull request create failed: GraphQL: Head sha can't be blank, Base sha can't be blank, Head repository can't be blank, No commits between OWN:main and System233:XXX Head ref must be a branch, not all refs are readable (createPullRequest)

$ gh pr create X No default remote repository has been set for this directory.

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

$ gh repo set-default OWN/REPO $ gh repo view X No default remote repository has been set for this directory.

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

ericmurdock avatar Oct 18 '24 20:10 ericmurdock

I'm currently not able to reproduce your issue with repo set-default. Even with two remotes that don't have a fork relationship it works correctly:

➜ git remote -v
cli     https://github.com/cli/cli (fetch)
cli     https://github.com/cli/cli (push)
origin  https://github.com/williammartin/illuminator.git (fetch)
origin  https://github.com/williammartin/illuminator.git (push)

➜ gh repo set-default --view
williammartin/illuminator

➜ gh repo set-default
This command sets the default remote repository to use when querying the
GitHub API for the locally cloned repository.

gh uses the default repository for things like:

 - viewing and creating pull requests
 - viewing and creating issues
 - viewing and creating releases
 - working with GitHub Actions
 - adding repository and environment secrets

? Which repository should be the default? cli/cli
✓ Set cli/cli as the default repository for the current directory

➜ gh repo set-default --view
cli/cli

➜ gh repo view | cat
name:   cli/cli
description:    GitHub’s official command line tool
--
# GitHub CLI
...

The way gh resolves the default repo is by looking into the .git/config file and looking for a gh-resolved entry like:

[remote "cli"]
        url = https://github.com/cli/cli
        fetch = +refs/heads/*:refs/remotes/cli/*
        gh-resolved = base

or

[remote "cli"]
        url = https://github.com/cli/cli
        fetch = +refs/heads/*:refs/remotes/cli/*
        gh-resolved = upstream/cli

Perhaps you could have a look in your git config after running set-default and having a look at the state.

williammartin avatar Oct 21 '24 10:10 williammartin

👋 @System233 - a polite bump, wondering if you have had a chance to review @williammartin's comment above?

BagToad avatar Nov 15 '24 18:11 BagToad

I will try to upload a complete command log tomorrow. This use case itself may not make sense at the moment, it is just to troubleshoot the problem of gh repo set-default not working.

System233 avatar Nov 16 '24 14:11 System233

Sorry, I tried to pull a new repository to reproduce the problem, but the problem did not reproduce, so I will close this issue now.

System233 avatar Nov 23 '24 14:11 System233