How to Sync Forked Repositories Using Git or Github

Soham Biswas
Level Up Coding
Published in
4 min readJan 26, 2020

--

General Git/Github Workflow

There have been times when we are about to initiate a pull request and then we realise that the fork that we were working on gets outdated as the original codebase has been modified by others by adding new features or by merging their pull requests. Thus, merging your pull request might lead to various timeline issues and makes it difficult for the maintainers to review your changes.

To overcome this problem we must update our fork before contributing to it or before initiating a pull request. In this article, we will be discussing on how to do so using Git’s Command Line Interface and Github’s Browser Interface.

For this article, we will be using our ICCV19-Paper-Review repository as an example for the original repo and my fork to demonstrate the process. In this repo we publish reviews on Computer Vision Research Papers which were presented at International Conference on Computer Vision 2019. If interested, do visit our website.

My Forked Repository

As you can see in the above image, my fork repo is currently 66 commits behind the original/upstream repository. Now, let’s go through the steps to sync my fork.

Updating forks using Git’s Command Line Interface:

In this section, I will be focusing on Git’s command line interface to sync my fork. The steps to do so are as follows:

  1. Open up your terminal and navigate to the working directory of your local repository. Mine’s at D:\Github\ICCV19-Paper-Review>
  2. Navigate to the branch that contains all the changes that you need to merge with your fork repository. Generally, it’s called the master branch. To navigate to master branch, execute the following command.
git checkout master

3. Before we can sync our fork repository with the original/upstream repository, we need to make sure that Git knows about the original/upstream repository. To do this, execute the following command to get the list of all tracked repositories.

git remote -v

Output:

D:\Github\ICCV19-Paper-Review>git remote -v
origin https://github.com/Nibba2018/ICCV19-Paper-Review.git (fetch)
origin https://github.com/Nibba2018/ICCV19-Paper-Review.git (push)

As you can see, only my forked repositories are listed, hence we need to add our original repo as a remote repository.

4. To add the original Repo, execute the following command:

git remote add upstream [https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git]

Here, replace the link of your original repo without the ‘[]’. Now executing git remote -v we get:

D:\Github\ICCV19-Paper-Review>git remote -v
origin https://github.com/Nibba2018/ICCV19-Paper-Review.git (fetch)
origin https://github.com/Nibba2018/ICCV19-Paper-Review.git (push)
upstream https://github.com/IEM-Computer-Vision/ICCV19-Paper-Review.git (fetch)
upstream https://github.com/IEM-Computer-Vision/ICCV19-Paper-Review.git (push)

5. Once you have added your upstream repo, we need to fetch changes done to it. To do so we execute this command:

git fetch upstream

Doing so displays all the new changes that have happened since you forked it.

D:\Github\ICCV19-Paper-Review>git fetch upstream
remote: Enumerating objects: 102, done.
remote: Counting objects: 100% (102/102), done.
remote: Compressing objects: 100% (47/47), done.
remote: Total 182 (delta 73), reused 70 (delta 55), pack-reused 80
Receiving objects: 100% (182/182), 3.13 MiB | 763.00 KiB/s, done.
Resolving deltas: 100% (90/90), completed with 5 local objects.
From https://github.com/IEM-Computer-Vision/ICCV19-Paper-Review
* [new branch] master -> upstream/master
* [new branch] revert-27-master -> upstream/revert-27-master

6. Once changes are fetched it’s time to merge them to our local repository. Executing this command will do so:

git merge upstream/master

This command will merge all the changes done to ‘master’ branch.

D:\Github\ICCV19-Paper-Review>git merge upstream/master
Updating b8206e4..bd03928
Fast-forward
Adversarial Robustness Model Compression.md | 0
...rial Robustness vs Model Compression or both.md | 50 +++++++++++++
Black-Box Adversarial Examples.md | 1 -
...g_With_Stacked_3D_and_2D_Regression_Networks.md | 40 +++++++++++
...Training_for_Weakly_Supervised_Cloud_Matting.md | 80 +++++++++++++++++++++
...tection_via_Hierarchical_Structured_Ensemble.md | 42 +++++++++++
(contd.)

7. After merging changes, its time to upload the same to your remote fork repository i.e your GitHub profile.

git push

Output:

D:\Github\ICCV19-Paper-Review>git push
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/Nibba2018/ICCV19-Paper-Review.git
b8206e4..bd03928 master -> master
My fork after Syncing

As we can see in the above image, our fork repository is now in sync with the original/upstream repository.

Updating Forks using Github’s Browser Interface:

Sayantan Das, the maintainer of the original repository has made a youtube tutorial regarding this:

In this article, we talked about how to merge original/upstream repository changes with our fork repository using Git’s command line interface and Github’s Browser interface. I hope you enjoyed reading the article. Happy Coding!!

--

--