How to Reset a Local Git Branch to Remote

Brian Bojan Dordevic
About The Author

Brian Decoded

President at Alpha Efficiency

Join me at the forefront of web design and digital marketing innovation. I am obsessed with web design, business philosophy and marketing performance.
I write Conversion Insider newsletter.

Git is a free, open-source version control system that allows developers to take numerous complex actions in order to streamline their workflow and seamlessly collaborate on various projects. One of the usual issues that developers come across while getting familiar with all the features that Git has to offer is how to keep their local and remote branches in sync throughout the entire project.

git reset local branch to remote

While following the workflow that you and your team members had put in place, a situation may occur when you need to completely overwrite your local branch with remote versions and ignore all the local changes to files and folders. In order to shed some light on this common issue, our web development agency Chicago explores ways to reset a local Git branch to remote.

What is a branch in Git?

Branching is a core concept in Git. Due to this concept, developers are able to set up a distributed workflow for team collaboration making the development process more efficient. 

Branches allow you to manage different versions of your project. The main branch is the default branch in the Git repository. It is considered to be a deployable, bug-free code ready to go to the end-user. A remote branch is a reference to the state of the branches in a remote repository. Each time you clone a repository, you pull the data from a repository on the internet, or an internal server called the remote.

Using git reset to reset local branch to remote

Let’s say that you have a local branch that you worked on and made some changes to it. However, those changes are no longer needed, or they could be out of date. On the other hand, one of your team members has made some changes such as fixes or feature development and pushed those into the remote branch. Now you need to fetch those changes that are in the remote repository. Since the changes you made to your local branch are no longer required, all you need to do is use Git commands to reset your local branch to remote. Here is how to do that.

Method #1 (hard reset local branch)

Save your current work (optional step)

By resetting your local Git branch to remote, you lose all the changes you made locally after the last commit of the remote repository. In case you have any ambiguities, you may want to save the work done on your local branch to a separate branch before resetting it. Use the following commands to update the commit message and branch name:

    git commit -a -m “Your message”

    git branch <new branch name>

Fetch origin

Now we need to download the objects and refs from origin. The origin is the term that Git uses to specify the remote URL of the remote repository. To fetch the remote repository, all you need to do is enter the following command:

    git fetch origin

Reset local repository

Next, we’ll use the git reset command to reset the current local repository to the repository on the remote branch:

    git reset –hard origin/master

This way, you have also discarded all of the local changes. As a result, all of the changes and commits made to the remote branch in the remote repository are now also present in your local branch of the local repository.

Delete untracked files (optional step)

Previous steps may result in a few untracked files. We can check the list of these files without actually deleting them:

    git clean -n -f

Our next step would be to delete these untracked files and folders. We can do so by running the following command:

    git clean -f

Alternatively, we can clean up the untracked changes without previously listing them by executing this command:

    git clean -d -f -x

Note that:

  • -d stands for untracked directories
  • -f stands for untracked files
  • -x stands for ignored files

Method #2 (hard reset local branch using the alternative command line)

First, we need to checkout to the local branch we wish to override:

    git checkout -b your-branch

Now that we are on our local branch, let’s use the following command to tell Git to reset the local branch to remote:

    git reset –hard @{u}

The @{u} command is shorthand for the upstream branch that your current branch is tracking. You’ll find this command very useful in cases when you simply don’t remember the name of your remote branch but want to ensure that you are resetting to the current branch. For example, let’s say you are on a branch named your-branch, which is set up to track the remote origin/your-branch. In this case, @{u} serves as the equivalent of your remote branch.

There is also the option to reset to a specific branch using:

    git reset –hard origin/your-branch

Method #3 (delete and rebuild your local branch)

Another way of using Git to reset the local branch to remote is to delete the remote copy of your branch entirely and then fetch the remote origin.

Let’s start by deleting your local branch:

    git branch -D local_branch

Now, let’s fetch the latest copy of your remote branch from the origin. Simply type:

    git fetch origin remote_branch

Moving on, let’s rebuild your local branch upon the remote branch we have fetched. Here is how to do that:

    git checkout -b local_branch origin/remote_branch

Final thoughts

This concludes our article. In today’s post, we have presented three methods that you can use to command Git to reset your local branch to remote. As you can see, this is quite a useful option in those cases when you had local changes and edits to files that you wish to discard and reset to the latest remote commit. Due to Git’s very steep learning curve, we understand that many development companies are struggling to meet deadlines when it comes to delivering finished products. This can often negatively reflect on their online representation efforts with way too little time for website management. If you feel like your day is not long enough to achieve everything you have planned and you need a helping hand, schedule a call with our experts and let Alpha Efficiency build and maintain custom-coded websites that can back up your goals and ambitions.


Fresh inspiration is a fingertip away,
Download Our Portfolio.

Download Our Portfolio