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

Automatically add the upstream remote when cloning a fork #1977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mislav
Copy link
Owner

@mislav mislav commented Dec 21, 2018

$ hub clone my-fork
> origin:   git://github.com/MYUSER/my-fork.git
> upstream: git://github.com/OWNER/original-project.git

$ hub clone --origin=upstream my-fork
> upstream: git://github.com/MYUSER/my-fork.git
> origin:   git://github.com/OWNER/original-project.git

Ref. #348
Fixes #811

/cc @apjanke

if err != nil {
ui.Errorln(output)
}
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it should do a git fetch for the added remote at this point? The normal case for doing a git clone fetches the full history for the cloned repo; since we're basically cloning two repos at once here, maybe we should do fetch so we have both repos' history.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice that we're doing git remote add -f. The extra flag automatically initiates a fetch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Missed that.

@apjanke
Copy link
Contributor

apjanke commented Dec 21, 2018

I like it!

I'm a little confused by the --origin=upstream scenario. In this case, is MYUSER/my-fork the fork root, or are we just using different names for the remotes of GitHub repos with the same fork relationship? If it's the latter, maybe the remote names should be origin and <MYUSER> instead of origin and upstream; I think that would match hub's existing naming conventions better: for the remote naming style that doesn't use upstream, generally origin is the fork tree root, and all other repos are named using the GitHub user name, including for the current user.

@mislav
Copy link
Owner Author

mislav commented Dec 27, 2018

I'm a little confused by the --origin=upstream scenario.

Thanks for weighing in! I wasn't sure that my approach was the best solution. Primarily, I was just trying to avoid the fatal: remote upstream already exists error. Perhaps when the user does --origin=upstream explicitly, maybe we should skip adding an extra remote for the parent repo altogether?

Example:

    $ hub clone my-fork
    > origin:   git://github.com/MYUSER/my-fork.git
    > upstream: git://github.com/OWNER/original-project.git

    $ hub clone --origin=upstream my-fork
    > (no origin remote)
    > upstream: git://github.com/OWNER/original-project.git
Copy link
Contributor

@kbrock kbrock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. this looks great

@Kampfkarren
Copy link

What's with the holdup on a merge?

@mislav
Copy link
Owner Author

mislav commented May 21, 2019

@Kampfkarren The PR is ready, but I wanted to hold off until I can ensure (by adding tests) that other commands (e.g. hub pull-request) will also handle the triangular workflow well.

@friederbluemle
Copy link
Contributor

Regarding --origin=upstream I'm also wondering if we can find a better terminology. Both origin and upstream are remote names. The only difference is that origin is the Git default, and upstream is a convention that some people prefer to use. Other than that there is nothing special about either name. At first glance it's not clear what it means to set them "equal"..

I also agree 100% with @apjanke regarding remote naming:

generally origin is the fork tree root, and all other repos are named using the GitHub user name, including for the current user.

Further, since many people have git aliased as hub it may be better to preserve the default behavior of the clone command to only create one origin remote. This of course can be changed by command line arguments or even set permanently as config options when using hub.

Have you considered something like this:

$ hub clone my-fork
> origin:   git://github.com/<myuser>/my-fork.git

$ hub clone --with-root-remote my-fork
> origin:   git://github.com/<owner>/original-project.git
> myuser:   git://github.com/<myuser>/my-fork.git

$ hub clone --with-root-remote=upstream my-fork
> origin:   git://github.com/<myuser>/my-fork.git
> upstream: git://github.com/<owner>/original-project.git

@mislav
Copy link
Owner Author

mislav commented May 22, 2019

generally origin is the fork tree root, and all other repos are named using the GitHub user name, including for the current user.

This was the main convention for as long as hub existed, but I feel the community has outgrown that convention. GitHub's own help docs now suggest a triangular git workflow where the canonical remote is called "upstream" and your own fork called "origin".

I want to adopt this in hub because it would help with scripting: it's way easier for everyone if hub fork sets up an "origin" remote that points to your newly created fork by default rather than naming it by your username, since then you can continue to interact with that remote right away in the script. If the remote name is username-based, then you first need to read the newly created remote name before you can start using it, which can be awkward to do.

Of course, no convention will ever handle everyone's use-case. Hub doesn't and will not try to support different popular git workflows out there; it will only handle the one we (GitHub) generally recommend and believe in.

@nt4f04uNd
Copy link

@mislav can you revive this PR, this would be very useful

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

Successfully merging this pull request may close these issues.

FR: set upstream remote when cloning a forked repo.
6 participants