Skip to content

Instantly share code, notes, and snippets.

@kevinnls
Last active September 30, 2020 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinnls/c4050c208723427c45ef3924d6d3bc5a to your computer and use it in GitHub Desktop.
Save kevinnls/c4050c208723427c45ef3924d6d3bc5a to your computer and use it in GitHub Desktop.

setting up your forked repo to sync with upstream

what?

common workflow in oss contributions is fork → edit → pr
when making repeated contributions, the master branch of the fork needs to be in sync with the original – the upstream
and here's one way to do that

the workings

we want local master to :

  • fetch and pull from upstream/master
  • push to fork/master

we can skip having to specify the remote each time by setting up a remote as follows:

  • <remote shortname>
    • fetch url: <upstream url>
    • push  url: <fork url>

the steps

these steps will setup a remote from scratch

  1. set up a remote
git remote add <name> <upstream-url>
git remote set-url <name> --push <fork-url>
  1. [optional] checking your settings
git remote show <name>

# this should return something like:
# ----
# remote <name>
# Fetch URL: <upstream url>
# Push  URL: <fork url>
# etc...
  1. set your master branch to track <name>/master
# if you aren't already at master
git checkout master

# set master to track your new remote
git branch --set-upstream-to=<name>/master

et voilà~ !

what happens now?

as far as git is concerned, your master branch should be in sync with the remote master branch at <name>. so when you run git pull at master, it pulls from upstream when you run git push it pushes to fork but treats both as the same remote.

caveat

this applies for a configuration where it is just a linear relationship between one local, one fork and one upstream. when there are multiple locals with the fork being centralised, pulls will need to be made from the fork as well. likewise for multiple remotes in the event of collaboration outside of the upstream.

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