Git Reset Local Branch to Remote

Git is the most popular version control system for modern developers. It helps facilitate collaborative development on multiple projects.

However, it is fundamental for developers to keep their local work synced with the one in a remote repository. Doing so can help prevent the loss of updates from multiple developers.

In this tutorial, we will learn how to reset a local branch to match the one in a remote repository. We will also discuss how to preserve our local changes and discard them if necessary.

Why Reset a Local Branch?

Although the circumstances of resetting a local branch may vary depending on the developer and the project, there are two main reasons why:

  1. You have changes in your local branch that are no longer relevant or needed in the repository.
  2. You need to fetch the changes merged on the remote repository. These changes can include new features, bug fixes, etc.

Hence, we need to reset our local branch to synchronize the source code in both the local and remote repository.

Reset a Local Branch with Git Reset Command

To reset a local branch, we use the git reset command followed by the current head of the remote branch.

Remember that using the git reset command will discard all your local changes and merge them with the ones in the remote repository.

Hence, before doing the git reset command, save the state of your current local branch as shown in the commands below:

$ git commit -a -m "minor backup, in case."
$ git branch my_backup

In the command above, we start by committing the changes we wish to save into another branch.

Feel free to modify the commit message and branch name to match your specific needs.

Once we have our work backed up into a separate branch, we can reset the local branch into the one on the remote repository.

We can accomplish this using two commands:

$ git fetch origin
$ git reset --hard origin/master

The first command lets us fetch the objects and refs from the origin.

NOTE that the origin refers to an alias for the URL of the remote repository.

The second command will then reset the current head of your local branch to the one on the remote repository.

With that, you have successfully reset your local branch to match the one on the remote repository.

In other cases, we can remove the untracked changes from the repository by using the git clean command:

$ git clean -f -d


In this post, we presented a simple and easy-to-follow method of resetting the local branch of a git repo to match the one on the remote repository.

We also illustrated saving your local changes before resetting to avoid losing your work.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list