CodeHubOrg / discussions

Discussion board for CodeHub Bristol
2 stars 2 forks source link

How to track the official GitHub repo once you’ve forked #9

Open katjad opened 8 years ago

katjad commented 8 years ago

[This was originally posted by Michael Gray Feb/March 2015 after he had given an introduction to Git at JS101]

Today I described how to fork the JS101-single-page-app on GitHub, from the CodeHubOrg 'official' repository, to give you your own GitHub copy. The next step was to clone this onto your local machine. Several of us have done this, and some of us have created individual profiles in JSON, pushed them back to GitHub, and created pull requests. Excellent!

There wasn't time for me to describe another important step: how to keep your fork in sync with the CodeHubOrg repo. As things stand, your local repo on your machine is tracking your GitHub fork, ut what happens when Katja accepts pull requests from others to incorporate their JSON profiles, which will be merged into the CodeHubOrg GitHub repo?

To connect your local repo back to the CodeHubOrg using the command line, first make sure you are in your JS101-single-page-app directory. Then: $ git remote add upstream https://github.com/CodeHubOrg/JS101-single-page-app.git upstream is just a name you've chosen for this connection to a remote repo. To see which remotes you have set up: $ git remote -v
origin git@github.com:mjg17/JS101-single-page-app.git (fetch) origin git@github.com:mjg17/JS101-single-page-app.git (push) upstream https://github.com/CodeHubOrg/JS101-single-page-app.git (fetch) upstream https://github.com/CodeHubOrg/JS101-single-page-app.git (push)

origin is the name git gives to the remote repo which you originally cloned your local copy from. (I used ssh rather than https to clone mine.)

This is described here: https://help.github.com/articles/fork-a-repo/#keep-your-fork-synced

To fetch changes from the upstream repo and merge them into your local repo: $ git fetch upstream # fetches changes from upstream but doesn't change your version $ git merge upstream/master # merges changes from upstream's master branch into yours

This is described here: https://help.github.com/articles/syncing-a-fork/

The fetch and merge commands are such a frequent combo that git provides the equivalent pull command:

$ git pull upstream master

Next time: how to avoid merge grief by using branches.

All the best Michael