1. Overview
In this quick tutorial, we’ll learn how to sync a GitHub repository with the original repository that it was forked from.
2. Syncing a Repository
There are two methods for syncing a repository:
2.1. Using GitHub Web UI
The first method is using GitHub’s web UI. If we enter the URL of the branch that we’ve been working on, we get the following:
To sync the branch with the original repository, we should hit “Sync fork”:
The branch starts to sync with the original repository. After that, the branch will be up to date:
We’ve successfully synced the branch with the original repository.
2.2. Using the Command Line (CLI)
Another method is using the command line. To sync our fork using the command line, we should first clone it:
$ git clone https://github.com/[username]/[repository_name]
After cloning our fork, we should add a remote to the original repository. We can call it whatever we want. Let’s call it upstream:
$ git remote add upstream https://github.com/[original_username]/[original_repository_name]
Now, we should fetch down all the branches from the upstream remote:
$ git fetch upstream
After that, we should switch to the branch that we want to update:
$ git checkout branch3
Branch 'branch3' set up to track remote branch 'branch3' from 'origin'.
Switched to a new branch 'branch3'
Now, we should change the base of our branch so that our new commits appear on top of the upstream repository’s master branch:
$ git rebase upstream/master
First, rewinding head to replay your work on top of it...
Fast-forwarded branch3 to upstream/master.
Finally, we should push the changes to our origin repository on GitHub:
$ git push -f origin branch3
We used the -f flag to force the push to our remote repository.
3. Summary
In this quick tutorial, we learned how to sync a fork with its original repository using both GitHub’s web UI and the command line.