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).
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.
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).
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)
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)
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)
6. 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.
Output of above command is as shown below (F: 6)
7. Now let`s go back to our fork repository on GitHub (F: 7)
As we can see in above image, our fork repository is now in sync with original/upstream repository.
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.