Javaexercise.com

How To Update Or Sync A Forked Repository On GitHub?

A Forked Repository(or Fork, in short) is a copy of a remote repository that exists on a Git hosting website like GitHub or BitBucket.

We fork a repository to understand a project and experiment with new changes without affecting the original project. Forking allows us to modify repositories by creating its remote clone.

After we fork a repository, the original remote repository may change. However, these changes are not reflected in our forked repository.

Let's learn how to update or sync our forked repository with the changes of the original repository.

We will discuss two methods in this tutorial. One is exclusively for GitHub. The other method uses the command line and can be used with repositories hosted on other platforms.

Update or Sync a Fork using the GitHub UI

GitHub is a Git hosting website. It allows us to upload and share our work with other developers and collaborate. GitHub allows us to sync or update a forked repository directly from their website. GitHub shows a message on our forked repository page when the original repository changes. In the image below, GitHub tells us that the original repository has one new commit on the master branch, and that commit is missing from our forked repository.

Update or Sync a Fork using the GitHub UI

GitHub allows us to compare the changes and fetch and merge them into our fork. Select the "Fetch upstream" drop-down menu and click on "Fetch and merge" to update your forked repository.

Update or Sync a Fork using the GitHub UI

Update or Sync a Fork using Command Line

Not all repositories are hosted on GitHub. We can update a forked repository from the command line as well. Follow the steps below to sync a forked repository from the command line.

Step 1:

Clone the forked repository to your local machine if you haven't done it yet. Use the Git Clone command and pass the URL of your forked repository.

git clone <forked-repository-URL>

Step 2:

Add a new remote connection with the original repository. Use the Git Remote Add command and give a name to the remote repo and pass the URL of the original repository. Upstream is an apt name to give to the original remote repository.

git remote add <remote-name> <original-repository-URL>
git remote add upstream <original-repository-URL> # use upstream as the remote name

To verify whether the new remote was created, use the Git Remote -v command.

git remote -v

In the output below, the remote called origin is the for our forked repository, and the upstream is the original repository.

Output:
​
origin  https://github.com/alice/Demo (fetch)   # forked repo
origin  https://github.com/alice/Demo (push)    # forked repo
upstream https://github.com/bob/Demo (fetch)    # original repo
upstream https://github.com/bob/Demo (push)     # original repo

Step 3:

Next, we need to fetch all the branches from the original remote repository. Use the Git Fetch command, and pass the remote name(upstream).

git fetch upstream

Step 4:

Now, let's merge the changes in our local repository. First, make sure to check out the default branch in your repository(master or main, in most cases)

git checkout master

Next, use the Git Merge command to merge the changes of the original repository to your local repository. Pass the remote name(upstream) and the default branch of the original remote repository(master, in our case).

git merge <remote-name>/<default-branch>
git merge upstream/master

Step 5:

Now, our local repository is up-to-date with the original remote repository. But our forked repository(on some Git hosting platforms) is still not synced or updated. We need to push the local changes to the forked repository. Use the Git Push command with the --force option. We need to pass the remote name of the forked repository(origin) and not the original repository(upstream). Also pass the default branch name.

git push --force origin master

Conclusion

Forking allows us to create a server-side copy of a remote repository and work on projects that we don't have access to. However, sometimes the original repository may change, and we may want these changes in the forked repository. Platforms like GitHub provide an easy way to sync our forked repository and update it with changes from the original repository.

We can also use some Git commands to update the forked copy. First, clone the forked repo to your local machine and add a remote connection with the original repository. Next, fetch the changes from the original repository and merge them in your local default branch. Finally, push these changes to your forked repository.