Skip to content

Unwanted remote naming after gh repo fork #847

Closed

Description

@jglick

Describe the bug

gh version 0.7.0 (2020-04-22)

gh repo fork renames origin to upstream and sets my fork as origin. I want to retain origin untouched and add an origin named fork.

Steps to reproduce the behavior

I had a clone of an organization repo

$ git remote -v
origin	ssh://[email protected]/org/repo.git (fetch)
origin	ssh://[email protected]/org/repo.git (push)

I forked it

$ gh repo fork
- Forking org/repo...
✓ Created fork jglick/repo
? Would you like to add a remote for the fork? Yes
✓ Renamed origin remote to upstream
✓ Added remote origin
$ git remote -v
origin	[email protected]:jglick/repo.git (fetch)
origin	[email protected]:jglick/repo.git (push)
upstream	ssh://[email protected]/org/repo.git (fetch)
upstream	ssh://[email protected]/org/repo.git (push)

Expected vs actual behavior

I wanted to see

$ gh repo fork
- Forking org/repo...
✓ Created fork jglick/repo
? Would you like to add a remote for the fork? Yes
✓ Added remote fork
$ git remote -v
fork	[email protected]:jglick/repo.git (fetch)
fork	[email protected]:jglick/repo.git (push)
origin	ssh://[email protected]/org/repo.git (fetch)
origin	ssh://[email protected]/org/repo.git (push)

or be prompted.

Workaround

git remote rename origin fork
git remote rename upstream origin

Activity

  1. added
    bug
    on Apr 29, 2020
  2. AliabbasMerchant commented on May 1, 2020

    @AliabbasMerchant
    Contributor

    Can we introduce a flag for asking the remote name for the fork?
    If the user specifies the remote name, well and good.
    If not:

    1. If the 'origin' remote already exists, show an error and suggest using the flag.
    2. If the 'origin' remote does not exist, set that.
  3. waldyrious commented on May 10, 2020

    @waldyrious
    Contributor

    For context and discussion leading to the current behavior, see #582 (/cc @cben).

    I do agree that renaming the remotes is undesirable, but I think the main problem here is that the user is not told about the upcoming renaming in advance.

    So instead of being told of the renaming only after the fact, as is the current behavior:

    $ gh repo fork
    - Forking someorg/foobar...
    ✓ Created fork myusername/foobar
    ? Would you like to add a remote for the fork? Yes
    ✓ Renamed origin remote to upstream
    ✓ Added remote origin

    ...I'd like to at least be informed of the impending rename; something like:

    $ gh repo fork
    - Forking someorg/foobar...
    ✓ Created fork myusername/foobar
    ? Would you like to add a remote for the fork? Yes
    ? Would you like to rename the current 'origin' remote to 'upstream'? Yes
    ✓ Renamed origin remote to upstream
    ✓ Added remote origin

    ...with the following alternative outcome, should I pick "no" in the second prompt:

    $ gh repo fork
    - Forking someorg/foobar...
    ✓ Created fork myusername/foobar
    ? Would you like to add a remote for the fork? Yes
    ? Would you like to rename the current 'origin' remote to 'upstream'? No
    ✓ Added remote fork # Or myusername
  4. stephenc commented on Sep 4, 2020

    @stephenc

    Can we please have a config option to control this behaviour.

    I want the behaviour I currently have with hub fork, i.e. it leaves origin unchanged and adds the remote using the name of the organization/user that the fork was created in

  5. stephenc commented on Sep 4, 2020

    @stephenc

    Aside: Ha! Just noticed the issue creator is a fellow ☁️ 🐝

    🤣

  6. jglick commented on Sep 8, 2020

    @jglick
    Author

    Nicer workaround:

    gh alias set --shell fork2 'git remote -v && gh repo fork --remote && git remote rename origin fork && git remote rename upstream origin && git remote -v'

    Use as:

    gh fork2
  7. dfireBird commented on Sep 20, 2020

    @dfireBird
    Contributor

    I was using a offline fork with aforementioned feature with a prompt for sometime. If it's okay can I open a PR?

  8. mislav commented on Sep 21, 2020

    @mislav
    Contributor

    @dfireBird Sure, I'd like us to address this is the next feature release! A PR that adds a prompt would be welcome. I'm also thinking that something like a --remote-name flag for automation would also be great.

  9. dfireBird commented on Sep 21, 2020

    @dfireBird
    Contributor

    I don't know go yet! So I just copied and pasted previous code from the repository for the prompt and I had some itty hack I did! I will make sure to correct it and open a PR.

  10. added
    priority-2
    on Sep 28, 2020
  11. deleted a comment from on Nov 3, 2020
  12. deleted a comment from on Nov 3, 2020
  13. deleted a comment from on Nov 3, 2020
  14. waldyrious commented on Jan 21, 2021

    @waldyrious
    Contributor

    @vilmibm thanks for adopting and merging #1882! It's definitely a large step in the right direction, but I actually find the UX implemented there a little cumbersome (requiring the user to repeat the command, this time passing the --remote-name="fork"):

    Couldn't the first invocation offer the option to provide the fork name interactively, like this?

    • Scenario where the user accepts renaming origin to upstream:

      $ gh repo fork
      - Forking someorg/foobar...
      ✓ Created fork myusername/foobar
      ? Would you like to add a remote for the fork? Yes
      ! A remote called 'origin' already exists
      ? Would you like to rename the current 'origin' remote to 'upstream'? Yes
      ✓ Renamed 'origin' remote to 'upstream'
      ✓ Added remote 'origin'
    • Scenario where the user rejects renaming origin to upstream (same sequence except for the last 3 lines):

      $ gh repo fork
      - Forking someorg/foobar...
      ✓ Created fork myusername/foobar
      ? Would you like to add a remote for the fork? Yes
      ! A remote called 'origin' already exists
      ? Would you like to rename the current 'origin' remote to 'upstream'? No
      ? What should be the remote name for the fork? fork
      ✓ Added remote 'fork'

    Is this something that could be tracked in a separate issue, or are you against that kind of behavior in principle?

  15. cben commented on Jan 21, 2021

    @cben

    I actually like it being a flag, because now I can

    alias fork='gh repo fork --remote --remote-name=cben'
    

    and not deal with any prompts. ❤️

  16. waldyrious commented on Jan 21, 2021

    @waldyrious
    Contributor

    Sure, I am not advocating for removal of the flag :) In fact, I will probably also do something similar to your alias, since I'm used to that workflow thanks to hub :) but for people who don't yet have strong preferences, the default experience could be more ergonomic. In other words, we could have both a flag and interactive prompts if the flag isn't passed, rather than an error requesting to run the command again.

    (On a related note, it might be a good idea to allow configuring a default remote name for forks via gh config, so that one doesn't have to override the default fork subcommand with an alias.)

  17. vilmibm commented on Jan 21, 2021

    @vilmibm
    Contributor

    @waldyrious thanks for the feedback! In general we're trying to find the correct balance of prompting + magic + maintainability. We felt that implicit renaming was too much magic but more prompting == more difficult tests and less maintainable code. It's possible we'll want prompts back, here, but for now wanted to err on the side of simpler code.

    I'll say that while friendly prompts and a major goal of gh we are trying to be more careful with them until we have a better framework for testing them and, possibly, new primitives for prompts that we're more comfortable maintaining.

  18. waldyrious commented on Jan 21, 2021

    @waldyrious
    Contributor

    Thanks for the response. I'll be looking forward to improved maintainability of prompt-based code, as those are one of the key aspects that make gh work so well IMO, and would definitely improve this flow, in particular.

  19. jglick commented on Feb 22, 2021

    @jglick
    Author

    After #2982 revised my workaround #847 (comment) to be similar to #847 (comment):

    gh alias set fork 'repo fork --remote --remote-name fork'
  20. waldyrious commented on Sep 3, 2026

    @waldyrious
    Contributor

    In case this helps others who come across this thread: until #8659 is addressed (which would allow gh fork to set the default repository), I have modified my fork alias in ~/.config/gh/config.yml to run gh repo set-default in addition to gh repo fork:

    aliases:
        fork: '!gh repo fork --remote --remote-name fork && gh repo set-default origin'
    

    This way I don't need to be pestered by the "No default remote repository has been set for this directory" error whenever I try to open a PR in a new repository :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority-2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions