Skip to content

Update remote naming when using gh repo fork to use origin & upstream #582

Closed

Description

@waldyrious

When forking a repo with gh repo fork, the remote is named "fork".

Instead I'd prefer this to use the upstream's namespace (username under which the fork lives), which, for a fork created by me, would be "waldyrious".

That would make the list of remotes more self-descriptive, and would allow cloning multiple forks of the same repo without naming conflicts or using e.g. counters like "fork-1", etc.

Note: the proposed solution is what hub does.

Activity

  1. added
    enhancement
    on Mar 4, 2020
  2. changed the title [-]gh repo fork should use the fork's username as remote rather than "fork"[/-] [+]`gh repo fork` should use the fork's username as remote[/+] on Mar 4, 2020
  3. mislav commented on Mar 4, 2020

    @mislav
    Contributor

    Thanks for adding your thoughts!

    I agree that simply "fork" isn't the best name, partly because it's ambiguous (as you point out, there may be multiple remotes each pointing to a fork). But I was also not perfectly happy with hub's default either, mainly for scripting: after issuing a hub fork operation, you would have to know your username to be able to generate a git push <remote> <branch> command. Later hub received the --remote-name option, which made it better for scripting so that you can pick a static name instead of having the remote be a moving target, but I still don't think the default is all that great.

    Upon forking a repo, I'm leaning to naming your fork "origin" while renaming the other remote to "upstream". This goes along with "triangular workflow" that some in the git community are following and that GitHub generally encourages.

    In the end, any default we choose will likely disappoint someone, and we're not yet ready to add a ton of configuration options for this either. So we're going to give this a lot of thought and update with new ideas/approaches.

  4. changed the title [-]`gh repo fork` should use the fork's username as remote[/-] [+]Update remote naming when using `gh repo fork` to use origin & upstream[/+] on Mar 9, 2020
  5. billygriffin commented on Mar 9, 2020

    @billygriffin
    Contributor

    We discussed this quite a bit this morning, and decided that we're going to follow the documented GitHub model here for remote naming of fork vs. parent.

    When someone creates a fork, we'll use the following naming scheme:

    Fork: origin
    Parent: upstream

    This sometimes means that the parent repo's remote will be renamed, many times from origin, to the new name, upstream.

    As @mislav mentioned, we know this isn't going to satisfy everyone, but we think it's a reasonable default and anything we do will likely not fit with everyone's preference.

  6. waldyrious commented on Mar 10, 2020

    @waldyrious
    ContributorAuthor

    😢

    Thanks for considering this. I wish you had picked hub's approach, rather than one that will require renaming remotes and produce ambiguous remote names, but I trust that you weighted pros and cons of each approach.

  7. cben commented on Mar 23, 2020

    @cben

    Interoperability angle:

    I have maybe hundreds of clone directories (on several machines) using hub's naming. As gh doesn't cover (and doesn't plan to cover?) all of hub's functionality, I was thinking to use both, gradually leaning on gh more and more.
    But if they use different naming, I'll get a mixture of conventions, which is a recipe for me making mistakes.
    If gh starts renaming existing remotes, it's (1) surprising 👎 — in a sequence of git push origin branch1; gh pr create; git push origin branch2 I don't expect origin to change meaning mid-way! (2) accelerates me getting a mixture of conventions.

    • When renaming existing remotes, please make it print an attention-grabbing message.

    • Consider a command for explictily normalizing remotes (and do nothing else). Good for massive switch-over to new convention.


    I really loved hub's naming, despite most my coworkers preferring upstream + origin. Let me try to see why, and how new scheme compares.

    History dependence

    There are three and a half common scenarios:

    1. Cloned upstream project as origin, never modified it.
    2. Cloned upstream project as origin. Later decided to contribute, created a fork, adding a remote.
    3. Directly cloned my fork as origin. Later adding a remote for upstream.
    4. Push / send PR to my own repo. Only 1 remote, probably called origin.

    It's is nasty that (1), (2) vs (3) result in origin meaning different things :-(

    I felt that hub solved this problem, but did it really? Not exactly.
    It's mainly that any tool-supported convention encourages consistency! I just learnt to avoid (2) EDIT: avoid (3) 😄.
    However, hub fork did actively help, in several ways! First, the habit of hub fork rather than doing it in the UI, gave me a "correctly named" remote. Second, hub fork is idempotent, effectively alias to hub remote add cben if fork already exists. This helped me avoid (3) by making it just-as-easy to always clone upstream, then add myself. (Also removes any need to remember whether I'd already forked 😎)

    #620 will be innovative and maybe even better 👍

    Symmetry with naming of other's forks

    For repeated contributors, I like hub add remote foo adding their fork as foo.
    I can have remotes origin (main repo), cben (me), foo, bar. This is symmetric and nice :-)
    Having remotes upstream, fork (me), foo, bar would be less symmetric :-\

    (and as I said I enjoyed hub remote add cben for myself, although hub fork is shorter)

    Scripting / shell safety

    I work on projects where I have commit access to main repo, and sometimes collaborators' forks ("allow commits by maintainers" is nice!), but we want to always make PRs.
    I taught myself to type git push REMOTE, not just git push. Especially with -f 😅. But is remote name unambiguous?

    • I have a script that boils down to git citool && git push cben.

      Pushing to a remote named cben is never dangerous 👷‍♂️. If it exists, in scenarios (2), (3) it's my fork. In scenario (4) I might want to be careful to open PRs and never push master directly, but my remote will be origin not cben (this relies on currently not consistently naming it!)

      Doing git push fork would be just as safe 👍

      Doing get push origin is risky! ⚠️ It might be my fork with new convention — but it risks being main repo or collaborators' fork, if I wasn't careful at git clone time...
      I especially don't want to aquire the habit of git push origin -f during normal contribution rebasing my PRs! 🙅‍♂️

    • Remote names are also important, even more, in branch refs.
      They define what origin/master means in commands!
      I type git rebase -i origin/master all the time. Also git diff origin/master etc...

      Would upstream/master work just as well? Mostly, yes.
      However, there is the case of (1) where I didn't expect to contribute, used off-the-shelf git clone and got it named origin...
      (Active renaming would mitigate this a bit 👍)


    Suggestion: upstream + fork.

    origin is inherently ambiguous. Globally it means "whatever I used in git clone". Now it might mean "whether I'd used hub or gh in this directory".

    Now you'll have a tool that knows how to query Github and [usually] figure out when it's dealing with main repo vs forks, which is an improvement 👏
    => Consider avoiding origin altogether and renaming to unambiguous upstream vs fork. (Or upstream vs username.)

    P.S. ❤️ I'll use this opportunity to say Thank you — for hub, for gh, and for GitHub at large :)

  8. mislav commented on Mar 24, 2020

    @mislav
    Contributor

    @waldyrious @cben Thank you for your input! It's a good point to consider the potential surprising factor of swapping remote names in case gh pr create creates a fork. I also agree that remote renaming would be a potentially surprising side-effect of that command and that we should try to avoid it.

    P.S. ❤️ I'll use this opportunity to say Thank you — for hub, for gh, and for GitHub at large :)

    Yay ☺️

  9. self-assigned this
    on Mar 27, 2020
  10. jglick commented on May 13, 2020

    @jglick

    we're going to follow the documented GitHub model here for remote naming of fork vs. parent

    Note that this document seems to be presuming that the workflow is something like

    • navigate to someone else’s (e.g., an org’s) repo
    • click the Fork button
    • use the suggested commands under Clone to, e.g., git clone https://github.com/myacct/repo
    • do work (preferably on a topic branch)
    • file PRs, whatever
    • optionally, at some point, add an upstream remote so you can merge in upstream master

    whereas the command under discussion here invites a workflow more like

  11. daurnimator commented on Jan 1, 2022

    @daurnimator

    Is there some way to opt-out of this behaviour by default? (e.g. in config file?)

  12. jglick commented on Jan 3, 2022

    @jglick

    @daurnimator I just use #847 (comment) FWIW.

  13. wookayin commented on Oct 10, 2023

    @wookayin

    Is there some way to opt-out of this behaviour by default? (e.g. in config file?)

    #847 (comment) says:

    $ gh alias set fork 'repo fork --remote --remote-name fork'
    

    FYI this will update the config into ~/.config/gh/config.yml

    aliases:
        fork: repo fork --remote --remote-name fork
    
  14. waldyrious commented on Sep 3, 2026

    @waldyrious
    ContributorAuthor

    #847 (comment) says:

    For completeness' sake, I'm also copying the comment I just added there:

    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

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions