Keeping your fork up to date with a github repository

I'm going to assume you've set up your github account, forked from a repository and cloned it to your local machine. You may now be wondering how you now keep your github fork and clone up to date with any check ins from the main repository.  It's certainly a problem we came across in the last project we set up.  Rather than using the single repository model and having everyone check in to that repository we chose to go with the approach of everyone having their own fork and sending pull requests to merge into the main line.  This is mostly because this team is distributed so it allows us to use the pull system as a way of performing code reviews on remote workers code.

Fortunately, after a couple of false starts, it turns out that keeping up to date is relatively easy.  It's all done from your local clone of the fork you created and so is all done on your local machine.  This may seem a little strange at first as you would expect to update your github fork and then pull those changes locally.  First step is to set up the remote repositories that your local repository will reference. By default git creates the origin remote, which points to the fork you created on github. In order to stay up to date with the changes to the parent repository you'll want to setup the upstream remote as well.

git remote add upstream
git fetch upstream

Now whenever you want to update it's a simple process. Just change to the branch you want to update from the upstream ( for example, the master branch ) and then issue the commands below:

git checkout master
git fetch upstream
git merge upstream/master
git push origin master

These commands will pull down the latest changes to the upstream repo to your local repo, then merge the changes into your local clone's master branch, and finally push those changes back up to your fork's repository on Github.

It's as simple as that.