Reset a branch from origin with Git

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Stuck with a Git branch in an unwanted state? Here are the three steps to reset your branch to its initial state!

Article published on 15/02/2023, last updated on 10/08/2026

Just merged two branches together without paying attention? Your working branch needs to be reset because you made a mistake, but you don't know how to do it?

Here's the solution!

It's important to note that by following these steps, all the work done on your current branch and not present on the remote repository's branch will be entirely deleted.

So follow this advice with full knowledge of the facts!

The first step is to properly check that you are currently on the branch you want to reset/resynchronize with the remote repository.

Here the branch in question is called "dev":

$> git checkout dev

Next, we're going to go back to the exact state that our remote branch is in:

$> git reset --hard origin/dev

The --hard argument ensures that file changes are not kept as pending changes (like a classic reset). However, if you had created new files, they will still be present, ready to be added to the history.

If that's the case, you can delete (careful) all these files, using the clean command:

# To simulate the cleanup
$> git clean -xfn

# To run the cleanup
$> git clean -xf

And there you go, your local branch is now perfectly identical to the remote branch, and all changes have been deleted!


Annie Spratt sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet