How to Reset a Local Git Branch to a Remote Branch

Learn how to reset your local git branch to a remote branch using the git reset command. Also in this article, see how the git pull command works.

Git is a free and open-source distributed version control system that makes collaboration seamless. The Git feature that really makes it stand out is its branching model. Git allows and encourages you to have multiple local branches that can be entirely independent of each other.

Keeping your local branches in sync with the remote is a common problem faced by developers, and the obvious solution is $ git pull <remote> <branch> which incorporates changes from the remote branch into the local branch. However, sometimes you don't want to merge the remote changes with your local changes, because, for one reason or the other, you want to have a copy of the remote changes in an unaltered state, without interference from your own local changes.

In this article, you will learn how to reset a local branch to a remote branch using git fetch and git reset. If you’re confused by some of the terminology (e.g. remote), read this.

Prerequisite

  • Basic knowledge of git

Understanding the Problem

Picture this: you're contributing to a group project and you've made some changes locally. Contributor B refactors the codebase and merges the changes to the upstream/main branch. You want to make sure this new update works before you integrate it into your local main branch, so you create a branch $ git checkout -b test and pull from upstream $ git pull upstream main.

What happens? You either get merge conflicts or an updated version of your code with both local and upstream changes merged. But this wasn't what you were hoping to achieve, so how can you pull the upstream changes while discarding everything that currently exists on the test branch?

First, let's take a look at how git pull works.

Git Pull

Git pull incorporates changes from a remote repository into the current branch. Git pull does two things- fetching and merging. More precisely, git pull runs git fetch and then depending on configuration options or command line flags, will call either git rebase or git merge.

  • Git Fetch: downloads objects and refs from another repository. In this context, it downloads the changes from our upstream/main branch.

  • Git Merge: incorporates changes from the latest commits into the current branch. This is the step we want to avoid since we don't actually want to merge the two histories, we only want to keep one.

In this example, the current branch is "test", and by running git push, we are merging the changes from upstream/main into our local test branch. git pull

TLDR: The Solution

We can fetch the remote changes from upstream/main using git fetch, and reset the local branch using git reset.

git fetch and git reset.png

Git Reset: resets the current branch to the specified state. There are several configurations but we'll be using the --hard flag. --hard resets the index and working tree. Any changes to tracked files in the working tree are discarded. Any untracked files or directories in the way of writing any tracked files are simply deleted.

Reset Local Branch to Remote Using Git Reset

  • On your local repository, checkout to a new branch

    $ git checkout new-branch

  • Fetch changes from remote (in this example, we are fetching from the upstream/main branch but you can change this to suit your use case)

    $ git fetch upstream main

  • Reset local branch to remote branch

    $ git reset --hard upstream/main

  • clean up any untracked changes

    $ git clean -xdf

The -x flag removes ignored files.

The -d flag removes untracked folders.

The -f flag removes untracked files.

Conclusion

There are other methods you can use to solve this problem, and this article by Brian Dordevic talks about them. Another great resource you can look at is this post on freecodecamp.

Now that you know the basics about git pull, git fetch, git merge and git reset, you can find out more about how they really work and all the different ways you can use them in the git documentation.

Using git can be overwhelming, but you can take comfort in the fact that nobody has all the git commands memorized. It's okay to have issues occasionally, what matters is that you can figure them out and learn something in the process. You can always read the documentation, ask questions or find answers on Stackoverflow. We all do. 😉

Credits