Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repo fork ignores the --remote flag when specifying a repository via argument #2722

Open
gibfahn opened this issue Jan 2, 2021 · 15 comments
Labels
bug Something isn't working gh-repo relating to the gh repo command good first issue help wanted Contributions welcome p3 Affects a small number of users or is largely cosmetic

Comments

@gibfahn
Copy link

gibfahn commented Jan 2, 2021

Describe the bug

When I try to fork a repo and add a new remote, it doesn't add a new remote to my current repository.

gh version 1.4.0 (2020-12-17)

Expected vs actual behavior

I expect to see a fork remote added to my repository, but nothing is added.

Logs

gh repo fork --clone=false --remote=true $(git remote get-url up)
- Forking someorg/somerepo...
✓ Created fork gib/somerepogit remote
upgh --version
gh version 1.4.0 (2020-12-17)
https://github.com/cli/cli/releases/latest
@gibfahn gibfahn added the bug Something isn't working label Jan 2, 2021
@mislav
Copy link
Contributor

mislav commented Jan 4, 2021

Thanks for reporting! Try this workaround:

gh repo fork --remote

Explanation: most gh commands do operations on the current repository and, alternatively, they take the repository via argument or flag. You've passed a repository argument by doing $(git remote get-url up). In that case, this signals to gh that you want to ignore the current local git repository that you're in, and therefore gh avoids touching the local git repository, including setting any new git remotes.

I think the "fix" here would be to have --remote flag error out if it's used in combination with the repository argument.

@mislav mislav added good first issue help wanted Contributions welcome p3 Affects a small number of users or is largely cosmetic labels Jan 4, 2021
@gibfahn
Copy link
Author

gibfahn commented Jan 4, 2021

How does gh work out which remote to fork if you don't provide it as an argument? Does it just pick one at random?

@gibfahn
Copy link
Author

gibfahn commented Jan 4, 2021

I want to both specify the remote to fork, and also have it add a new fork remote. I'm happy to script around gh, but as it doesn't return the URL of the fork I don't think I can.

@mislav
Copy link
Contributor

mislav commented Jan 5, 2021

How does gh work out which remote to fork if you don't provide it as an argument? Does it just pick one at random?

No. For each local repo that has multiple GitHub remotes, gh tries to resolve the "base" remote by:

  1. If an interactive terminal is detected, the user is asked to select one. The choice is saved per-repository.
  2. If no interactive terminal, then the first remote is taken in this order: upstream, github, origin. If none of those remotes are found, the first remote (in alphabetical order) is taken as a "base" remote.

I want to both specify the remote to fork, and also have it add a new fork remote.

Sorry, specifying the remote to fork isn't available right now. Could you describe your use-case with several remotes?

In the meantime, you could use the api command to manually fork any repo and get its URL:

GH_HOST=my.enterprise.host gh api -XPOST repos/OWNER/REPO/forks | jq '.clone_url'

@gibfahn
Copy link
Author

gibfahn commented Jan 5, 2021

Sorry, specifying the remote to fork isn't available right now. Could you describe your use-case with several remotes?

Current remotes:

  • ghe -> GHE internal mirror
  • pub -> github.com public repo

Desired Remotes:

  • ghe -> GHE internal mirror
  • ghefork -> my GHE internal mirror's fork
  • pub -> github.com public repo
  • pubfork -> my github.com fork

I want to be able to fork repos and then clean them, so they don't have duplicate branches, this requires pushing the head ref of the "up" remote to its "fork" remote.

Current alias: https://github.com/gibfahn/dot/blob/7f16e16f1d34046e80f48fb3ce86415ddc1779c3/dotfiles/.config/git/config#L7 , it uses hub, which also doesn't have a way to specify this.

@mislav
Copy link
Contributor

mislav commented Jan 6, 2021

@gibfahn Thanks for describing your use case! We need to ponder about this. Right now we don't have good support for mixed GHE GitHub.com remotes.

It looks like you would need a way to select a remote to fork and also to choose how the new remote for the fork should be named. E.g.

$ gh repo fork --remote=ghe --remote-name=ghefork

We need to discuss with the team whether this is something that we would want to enable.

@mislav mislav changed the title Forking a GHE repo doesn't create a new remote Repo fork ignores the --remote flag when specifying a repository via argument Jan 6, 2021
@Brazuca228

This comment was marked as spam.

@SONVICHETH

This comment has been minimized.

@voanhcung

This comment was marked as spam.

camillesf added a commit to camillesf/gh that referenced this issue Apr 20, 2021
This fixes cli#2722, in which a Git remote is never added if repo fork
was given an argument.

By default, the remote is only added when no argument is given. Here,
we check if the argument is one of the existing remotes, and consider
that the remote should be added.

THIS IS BRITTLE AND A HACK AND SHOULD NOT BE USED.
@chemotaxis
Copy link
Contributor

@mislav Hasn't this issue been resolved with #1882?

@mislav
Copy link
Contributor

mislav commented May 17, 2021

No, but I can see how this is confusing. Thanks for linking that.

The --remote-name flag we added in the meantime isn't for selecting the remote to be forked: it's for selecting the name of the new git remote to be added after forking.

This thread describes a bug and a feature, none of which were yet addressed:

  • bug: specifying gh repo fork <repo> --remote=true ignores the --remote flag because the repository was explicitly passed
  • feature: there should be a way to select the base remote that a gh operation should apply to (in this case, specify the remote of the git repository to be forked)

@Somnn

This comment was marked as spam.

1 similar comment
@Davinsos
Copy link

337154

@neolace
Copy link

neolace commented Oct 10, 2021

Is there any chance of skipping any interactive shell when you interact with another?

@dvn123
Copy link

dvn123 commented Oct 18, 2021

Hi @mislav , I've been taking a look at this and tried to make a simple fix for the first item in your list . I used the comment below as a guide.

I think the "fix" here would be to have --remote flag error out if it's used in combination with the repository argument.

Regarding the second item, I saw the comment below which I believe it references it. I would like to know if a conclusion has been reached regarding this? I would be willing to have a go at this part as well (if the conclusion is to go ahead), would maybe need some pointers though.

We need to discuss with the team whether this is something that we would want to enable.

I can also open a PR with only the first part if you prefer.

@samcoe samcoe added the gh-repo relating to the gh repo command label Oct 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gh-repo relating to the gh repo command good first issue help wanted Contributions welcome p3 Affects a small number of users or is largely cosmetic
Projects
None yet
Development

No branches or pull requests

14 participants