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 · 12 comments
Open

Comments

@gibfahn
Copy link

@gibfahn 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 label Jan 2, 2021
@mislav
Copy link
Member

@mislav 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.

@gibfahn
Copy link
Author

@gibfahn 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 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
Member

@mislav 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 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
Member

@mislav 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 has been minimized.

@SONVICHETH

This comment was marked as spam.

@voanhcung

This comment has been minimized.

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

@chemotaxis chemotaxis commented Apr 28, 2021

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

@mislav
Copy link
Member

@mislav 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 has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
8 participants
@mislav @chemotaxis @gibfahn @Brazuca228 @SONVICHETH @voanhcung @Somnn and others