DEV Community

loading...
Cover image for How to sync your local git and remote git repository changes?
For Community

How to sync your local git and remote git repository changes?

stephin007 profile image stephin007 ・3 min read

Today we will focus on how we can synchronize our GitHub local repository master with an upstream repository branch of the GitHub project.
Now, you might think what is an upstream branch,
Upstream branches define the branch tracked on the remote repository by your local remote branch (also called the remote-tracking branch), the below figure will help ya'll understand.

upstream-1-768x527.png

** NOTE : When we made any CLONE/PULL for the first time this is not required as we do fork the repository for the first time all the content in both will be the same.**

So, Why do we need this process to be done?


This is very much necessary if you are working in a team and the teammates are working on different aspects of the project and you yourselves are working on something,
Certain files will differ in both repositories as there are many other developers are merging the code upstream and your branch will not have that changes unless you sync the repository with the current upstream, so you might need to keep up with their code changes as well, this is called syncing your changes with your local and remote repo.

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

If you don't, this will cause conflicts while you raise a PULL REQUEST.

Now, let me walk you through how to sync your changes with other changes by other developers.
Here I have the taken example of the Opencart GitHub project. First, change your working directory to the file location where you want to clone the project with the command.

$ cd your/local/storage/path

STEP 1: Clone your project with the command.

clone.png

After the clone is completed then move to the 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 :

remote.png

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 forked repo. To do this run the same command again as below.

$ git remote -v

Now it will show the response as below and it will also include your upstream repository from your master project.

upstream.png

STEP 3: Make your origin repository the same as your 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 have all the code which is not in our local repository from the upstream repository

Run the commands as mentioned below.

$ git fetch upstream

This command will respond 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 the response for the command as below.

fetchupstream.png

Now checkout to your master branch with the command (if not on the master branch)

$ git checkout master

And run the command.

$ git merge upstream/master

mergemaster.png

This will sync all the changes to your local repository if any, Now your local repository is synced with the upstream repository.

And that's it, If you have any queries let me know in the comments and it would be great if you leave a reaction as well and follow our community

Discussion (1)

Collapse
justinnn07 profile image
Justin Varghese

🔥🔥🔥🔥🔥

Forem Open with the Forem app