Keeping forked repos up to date with upstream

The usual workflow for contributing to open-source code is to fork the original repository to your account, create a feature branch, write the code and submit a pull request to the original repository. One thing to take note is to keep your forked repository up to date with the original repository. I always forget how to do it and end up spending time Googling for it, so just putting this snippet here for easy reference next time 🙂

# Clone your repo forked from the original repo
git clone git@github.com/myaccount/repo.git

# Add the original repo as a remote
git remote add upstream git@github.com:originalaccount/repo.git

# Checkout the branch that you want to sync with the original repo
git checkout master

# Pull the changes from the same branch in the original repo
# The rebase option will keep your tree clean and avoid merge commits
git pull --rebase upstream master

# Check the changes
git log

# Push the changes from your local machine to your remote repo online
git push