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

nopCommerce, GitHub, Open Source

0 ( 0 votes) | 298

In this article, we will talk about how to merge original/upstream repository changes with your fork repository using Git.

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

Fork Of nopCommerce Repository

As you can see in 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 original/upstream repository and get those changes.

Steps:

1. Open up Git bash and navigate to 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 master branch. To navigate to master branch, execute the following command.

git checkout master

(In my example, 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.)

3. Before we can sync our fork repository with 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 above command is as shown below (F: 2).
List of Remote Tracked Repositories

In above image, origin points to my fork repository and nopSolutions, 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 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 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)

Upstream Repository URL

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.

4. 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 3. Output of above command is as shown below (F: 4)

Git Fetch

5. 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)

Git Merge6. 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)

Git Push

7. Now let`s go back to our fork repository on GitHub (F: 7)

Updated Fork Repository

As we can see in above image, our fork repository is now in sync with 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!

Manoj Kulkarni - Dotnetcontext

Manoj Kulkarni

I am programmer, passionate blogger and foodie. I live in Nashik. I am a .Net developer. I like to learn new technologies to keep my self-updated and share that knowledge with my blog readers, friends and colleague.

Add a new comment