How to reset your git branch to a previous commit (both local and remote)

John Szabo
Coder Nomad
Published in
2 min readJul 10, 2020
Photo by Yancy Min on Unsplash

Sometimes you can get into a situation when you realise that you pushed something wrong, and you want to erase it from your commit history. Maybe you misunderstood the specs, and you just want to go back to a point where your code was still good, so you can start again.

This is where git reset comes in, which is effectively the opposite of git add.

To figure out which commit you want to go back to, you can type:

git whatchanged

This will give you a nice list of your commit history, which you can escape by pressing q. You will need the commit id you find to the right of the word commit.

When you have your commit id, you can type:

git reset --hard <commit id>

It’s important that you use the --hard option, because this way your files will also be reset locally. Obviously, only use this option if you are sure you want to erase everything after your chosen commit.

After you are done, you can push your changes to the remote repo. A simple push will not do however, you will have to force it:

git push -f

That’s it! I hope you found it useful, and good luck on your project.

--

--

John Szabo
Coder Nomad

Programmer, Buddhist blogger and lay Dharma teacher, Philosophy & Religious Studies major.