How to Sync Your Fork with Original Git Repository
Subscribe to my newsletter and never miss my upcoming articles
If you are contributing to a project on a regular basis, you would run across a very common issue, that is your fork might get outdated to the original repository.
In this article, we will be looking at how to update your fork with the changes made in the original project. I will be using the freeCodeCamp repository as an example of the original repo and my fork to demonstrate the process.
So, the very simple solution is to 👇
❌ Delete that Fork and Re-create the Fork 😎
My fork repo is currently 655 commits behind the original/upstream repository. Now, let’s go through the steps to how to sync fork.
First, you will need to add a remote for the upstream repository to the local repository of your fork present on your system. To do this, enter the following into your terminal:
git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
Now, You can check If the remote repo is added using
git remote -v.
$ git remote -v origin https://github.com/jatin2003/freeCodeCamp.git (fetch) origin https://github.com/jatin2003/freeCodeCamp.git (push) upstream https://github.com/freeCodeCamp/freeCodeCamp.git (fetch) upstream https://github.com/freeCodeCamp/freeCodeCamp.git (push)
If you've made any changes to your local repository, you will either need to commit them first or you can stash them.
It looks like you have added your upstream repo, now we need to fetch changes from the repo. To do it, enter the following command into your terminal:
git fetch upstream
It displays all the changes that have happened since you forked it.
Once changes are fetched it’s time to merge them to our local repository. Just enter the following command:
git merge upstream/master
This command will merge all the changes done to ‘master’ branch.
That's it, now your repository is up to date with the changes in the original project 🥳
Syncing your fork only updates your local copy of the repository, it does not update your repository on GitHub. So, Let's update our fork on Github now using the following command:
git push origin master
And now, your fork is also up-to-date. with the original/upstream repository.