How To Merge Upstream Repository Changes With Your Fork Repository Using Git

In this article, we will see how to merge original/upstream repository changes with our fork repository using Git.

I have forked an open-source project called nopCommerce (F: 1).

nopCommerce

As you can see in the above image, my fork repository is 15 commits behind the original/upstream repository. Now, let's go through the steps which will help us to sync my fork repository with the original/upstream repository and get those changes.

Steps

  1. Open up Git bash and navigate to the working directory for this project on your local machine.
  2. We need to navigate to a branch which contains all the changes that we need to merge with our fork repository. Generally, it's called the master branch. To navigate to master branch, execute the following command.

    git checkout master

    (In my example, the changes that I need to merge with my fork repository are stored in a branch called develop. So, I need to navigate to develop branch.)

  1. 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 of the above command is as shown below (F: 2).

    nopCommerce

    In the above image, origin points to my fork repository and nopSolutions are upstream points to original/upstream repository. If your output only contains your forked repository link (origin), then we can add the original/upstream repository link using the following command.

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

    In the above command, the word upstream is like an alias for that URL, so that we can easily access it. We will be using this alias name to pull down the changes from original/upstream repository.

    Also, replace the link in the above command with original/upstream repository link of your forked repository. To get this URL, go to the original/upstream repository page on GitHub. Click on "Clone or download" button and copy the https web URL as shown below (F: 3)

    nopCommerce

    In my case original/upstream repository link is https://github.com/nopSolutions/nopCommerce.git. This is the same link that we use to clone this repository on our local machine. After adding the remote repository link, execute git remote –v command again to make sure the original/upstream repository link is available in output.

  1. Execute the following command to pull down the changes from original/upstream repository on your local.

    git fetch upstream

    In above command, upstream is the same alias name that we have used while adding the original/upstream repository link in step 2.

    Output of above command is as shown below (F: 4)

    nopCommerce
  1. Execute the following command to merge these changes with our local fork repository.

    git merge upstream/master

    Above command will merge the changes that we pulled down in step 3 to local forked master branch. In my case, above command will be git merge upstream/develop. So that it will merge the changes to my local forked develop branch.

    Output of above command is as shown below (F: 5)

    nopCommerce
  1. Now, our local fork repository is in sync with original/upstream repository. We need to push these changes to our remote fork repository on GitHub, so that it will also get updated. To do that, execute the following command.

    git push 

    Output of above command is as shown below (F: 6)

    nopCommerce
  1. Now, let's go back to our fork repository on GitHub (F: 7)

    nopCommerce

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

Conclusion

In this article, we talked about how to merge original/upstream repository changes with our fork repository using Git. I hope you enjoyed reading the article.

Happy Coding!


Similar Articles