This is useful when you have forked a repository (repo), cloned it to your local machine and want to keep it in sync with the original repo.
Adding the remote repo
We can list the remote repositories for our repo with
git remote -v and add the original repo as follows:
git remote add upstream LINK_TO_ORIGINAL_REPO
Note that it is merely a convention to call it
upstream. You can give any name you want.
Check that the repo is added to your remote using
git remote -v.
Sync with remote
- Check if there are any changes in remote not on your fork using
git fetch upstream
- Checkout whichever branch you are interested (
git checkout $BRANCH)
- Merge with upstream using:
git merge upstream/$BRANCH
- Push your changes to origin if needed:
git push origin $BRANCH
Removing the remote repo
If you no longer want to get changes from the remote repo, it is easy to remove it using
git remote remove upstream
Github's help page