Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

git

How to synchronize your local repository with Git VC

Amaaz Ahmad

Git is one of the most renowned version control systems on Earth. It is now an essential skill that everyone related to the world of IT needs to learn.

One of the most common questions regarding Git is: “How can I synchronize my local project with the remote repository?”

We will try to answer this question as simply as possible.

Pull new changes from the remote repository

The pull command is used to download the new content from the remote repository and update those changes in the local repository.

It will download all the new data from the remote repository that has been pushed since we last synced our local repository with the remote. However, it will not delete any data present in the local repository but not in the remote repository.

If there are any conflicts in our local commits and the content being downloaded from remote, the developer must resolve those conflicts.

This is how to use the pull command with default origin and remote:

git pull

Make the local repository exactly like the remote repository

Sometimes, we need to synchronize the local repository with the remote repository so that our local repository looks the same as the remote repository.

There are two methods to do this:

1. Cloning a new repository

One way of doing this is to use the clone command and create a new local repository.

This is how to use the clone command:

git clone git-repository-URL

In the above snippet, git-repository-URL is replaced by the URL of the remote repository.

2. Changing the existing repository to look like the remote repository

The following commands will remove all untracked files, delete the files present in the local repository but on the remote, and make the local repository exactly like the remote repository.

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

The fetch command downloads all the new content from the remote repository without updating the local repository. If there are any branches present in the local repository but not on the remote repository, using the --prune option deletes those branches from the local repository.

The reset command is used to change the current status of our data to a specific commit. Using the --hard flag also removes all uncommitted changes in all the files.

The clean command is used to remove untracked files or folders. Using -f and -d options means that we want to delete all the untracked files and directories, respectively.

After successfully executing these three commands, our local repository will look the same as the remote repository.

RELATED TAGS

git

CONTRIBUTOR

Amaaz Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring