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
References
Github's help page
Top comments (3)
Nice article! Always handy to have a guide like this when the time comes :) There's also a Github Probot App called Pull that helps to automatically keep your forks up to date on Github itself. I've installed it recently on my forked repos and it removes the manual fetching!
Thanks! Was not aware of this before. This will reduce the manual work needed to keep them up-to-date.
This just saved me right now!