Today we are going to learn about how we can synchronize our GitHub local repository master with an upstream repository branch of the GitHub project. When we made any PULL for the first time this is not required as we do fork the repository for the first time all the content in the both will be same.
Now maybe you think that way to sync the local repository with here is the reason –
“Certain files will differ in both repository as there are many other developers are merging the code the upstream and your branch will not have that changes unless you sync the repository with current upstream. Synching will override the local repository with a master remote repository. And if there are files in the local repository that do not exist in the remote repo, local files get removed .”
Now in the below, I have explained how we will achieve this – Here I have the taken example of the Opencart GitHub project. First, change your working directory to the to file location where you want to clone the project with the command
$ cd your/local/storage/path
Opencart Company ? Find out More
Step 1: Clone your project with the command.
After the clone is completed then move to Opencart folder
This will move to the current working directory as “opencart”
Step 2: List the current configured remote repository for your fork.
$ git remote -v
This will list your forked repository response will be as below :
This is our repository branch content now we have to get a project from the upstream repository
Now Specify a new remote upstream repository that will be synced with the fork. In Opencart Project this https://github.com/opencart/opencart.git is my upstream repository. Run the following command.
$ git remote add upstream https://github.com/opencart/opencart.git
Now you cross check that the new upstream repository created for your for your fork repo. to do this run the same command again as below.
$ git remote -v
Now it will show response as below and also include your upstream repository from your master project.
Step 3: Make you origin repository same as an upstream repository.
Now we have created upstream branches for our local repository. Now we will match the content from the upstream repository to the local repository. In this, we will make all the code which are not in our
repository from the upstream repository
Run the commands as mentioned in the below.
$ git fetch upstream
This command will response by fetching the content from the upstream for your Master repo. All the new branches and files, contents will the fetched from the upstream repository. And response for the command as below.
Now checkout to your master branch with command if not on the master branch.
$ git checkout master
And run the command.
$ git merge upstream/master
This will sync all the changes to your local repository if any. You can see in the above screenshot that i have feathed all the changed in my locval repo. Now your local repository is synched with the upstream repository and you make changes to your local repo and pull to the upstream.
That’s All. Happy Reading!!