Keeping up with Upstream Git Repo

If you have forked a repository in GitHub that you want to work on, you do so, make your changes locally and once you are confident that everything works, you issue a pull request to the upstream so that your changes can be reviewed and merged. Here is a small snippet that I use to keep my fork up-to-date with the upstream.

upstream - The original repository from which you fork

Step 1: Add the remote upstream repository

1git remote add upstream https://github.com/ORIGINAL_OWNER_NAME/ORIGINAL_REPO_NAME.git

Step 2: Check if upstream is referenced

1git remote -v

should print out the following:

1origin https://github.com/FORKED_OWNER_NAME/ORIGINAL_REPO_NAME.git (fetch)
2origin https://github.com/FORKED_OWNER_NAME/ORIGINAL_REPO_NAME.git (push)
3upstream https://github.com/ORIGINAL_OWNER_NAME/ORIGINAL_REPO_NAME.git (fetch)
4upstream https://github.com/ORIGINAL_OWNER_NAME/ORIGINAL_REPO_NAME.git (push)

Step 3: Run the following commands to update forked repo with changes from original repo

1git fetch upstream

Step 4: Checkout master branch from the local fork

1git checkout master

Step 5: Merge changes from upstream/master into local master form

1git merge upstream/master

Step 6: To check if your local forked master is up-to-date with upstream master, run the following

1git fetch upstream && git diff remotes/upstream/master master > changes.diff

If there are no changes, then the diff file should be empty. You could combine all that in a script. That's it!