How to Synchronize a Local Repository With a Remote Repository in Git

John Wachira Feb 02, 2024
How to Synchronize a Local Repository With a Remote Repository in Git

This article outlines the process of syncing your local repository with a remote one. We will also see how you can sync your GitHub fork with the remote repo on the command line.

Synchronize a Local Repository With a Remote Repository in Git

Let’s assume we have a local repository with an upstream repository and want to sync the two. How do we go about this?

If you are unsure how to add an upstream repository to your local one, follow these steps.

You can use the command below to check configured remote repositories.

$ git remote -v
origin https://github.com/Wachira11ke/Delftscopetech.git (fetch)
origin https://github.com/Wachira11ke/Delftscopetech.git (push)

To configure an upstream, run:

$ git add upstream <URL for the original upstream repo>

We can now pull from the upstream using the command below.

$ git pull upstream --prune

The --prune option will remove remote-tracking branches that do not exist in the remote anymore. This will merge the changes to your current branch.

If you are not syncing with an upstream but instead with your GitHub repository, run:

git fetch origin
git reset --hard origin/master
git clean -f -d

Your local branch should be the exact copy of your remote branch (commits and files).

If you want to merge to your local main branch, make sure you are checked out before pulling.

We can now update our GitHub fork by running:

$ git push

In conclusion, you can easily sync your local repository with a remote repository. Add the --prune flag to remove remote-tracking branches that do not exist in the remote anymore from your local repository.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Remote