Skip to content

Instantly share code, notes, and snippets.

@danielmoi
Last active February 10, 2018 00:30
Show Gist options
  • Select an option

  • Save danielmoi/6068555093197cfe59d89a3477c0b2ac to your computer and use it in GitHub Desktop.

Select an option

Save danielmoi/6068555093197cfe59d89a3477c0b2ac to your computer and use it in GitHub Desktop.
Switch a cloned repo into a fork

If you are like me you find yourself cloning a repo, making some proposed changes and then deciding to later contributing back using the GitHub Flow convention. Below is a set of instructions I've developed for myself on how to deal with this scenario and an explanation of why it matters based on jagregory's gist.

Proper GitHub flow is this:

  1. upstream = the original (forked) repo
  2. origin = my fork

Current:

git remote -v
origin	https://github.com/...some-repo (fetch)
origin	https://github.com/...some-repo (push)

Should be:

git remote -v
origin	https://github.com/...my-repo (fetch)
origin	https://github.com/...my-repo (push)
upstream	https://github.com/...some-repo (fetch)
upstream	https://github.com/...some-repo (push)

Steps

  1. Clone some repo (have already done this, instead of the forking first)

    git clone [email protected]
    
  2. Fork their repo on GitHub (use the big green button)

  3. In your local, rename your origin remote to upstream (ie. set the original repo as upstream)

    git remote rename origin upstream
    
  4. Set your newly forked repo on GitHub as the new origin

    git remote add origin [email protected]
    
  5. Set the tracking reference to your forked repo

    git push -u origin master
    
    # same
    git push --set-upstream origin master
    

We can also move our changes to a branch

  1. Create new branch

    git checkout -b my-branch
    
  2. Push our branch to our repo

    git push -u origin my-branch
    
  3. Reset our master branch to the original repo's master (which is now our UPSTREAM)

    git checkout master
    
    git reset --hard upstream/master
    

Credits

https://gist.github.com/jpierson/b6c0815e9dd7078f6b8cc3cb9076ddf4

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