How to Reset a Local Branch to the Remote Branch in Git

Published Sep 25, 2021  ∙  Updated Sep 3, 2022

Suppose we made some funky changes and need to reset our local branch to the state of the remote branch.

Backup Our Work

Hard resets cannot be undone easily. Just in case, I would suggest saving current changes to a branch.

Let’s backup our work. To save our work onto another branch, we can commit everything and set it onto another branch.

git commit -a -m "Saving changes, just in case"
git branch saved-work

Now, our work is on branch saved-work, just in case we want to reference it later on.

1. Using git reset and @{u}

We can use git reset and (optionally) git fetch to reset our local branch to where the snapshot of the remote is.

git fetch --prune # Update the local snapshot of the remote repo (optional)
git reset --hard @{u} # Point local branch to that snapshot

@{u} is the verbose form of @{upstream}. For Windows or PowerShell, use double quotes "@{u}".

Specifying @{u} allows us to avoid including the remote repo and branch. As such, we can create a simple alias for this.

alias resetbranch="git reset --hard @{u}"

After running the reset, we’ll need to use git clean to remove untracked files and clear the staging area (so we can get nothing to commit, working directory clean. upon git status).

git clean -df

To perform a dry run and see what files would be removed without removing them, run git clean -dfn (with the -n flag).

Then, if needed, pull the most recent changes.

git pull

2. Using git reset and branch name

Suppose our remote repo’s name is origin, the branch name in the remote repo is master, and our currently checked-out branch in our local repo is also master.

We can set our branch to exactly match the remote branch by fetching the remote and performing a hard reset.

git fetch origin
git reset --hard origin/master

Unlike the first method, this one requires us to explicitly specify the remote repo and branch name.