Easily undo one or more commits with Git

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Git reset vs Git revert

Article published on 12/01/2021, last updated on 10/08/2026

Your commit is done, synced with your remote, and you've just realized that your code introduced a bug into the codebase, so what do you do now?

I'd say the first step would be to set up an automated testing strategy in your project, but that's another debate, for another article.

No panic, Git obviously gives you the ability to go back, otherwise what would be the point of a version control system after all?

The problem is choosing the right solution among the many possibilities, so I'm going to present two methods: revert and reset, which both have their own way of working that will influence the final state of your project.

But first, we need to find the commit we want to go back to, because these two methods don't just let you go back to the previous commit, they let you go back to any earlier commit.

Targeting the desired commit

Revert and reset use the hash of the target commit to make their changes, so you need to retrieve this hash beforehand. To do this, nothing could be simpler, you just need to use the "git log" command which gives you the full history of your current branch.

$ git log

commit 1cea038e917dfac1031f41f2eb2134cc92c65b71 <<hash
Author: Nicolas Brondin-Bernard 
Date:   Tue Jan 12 00:15:56 2021 +0100

    [FEATURE] User - Auth

commit b0f28218b6a28b02b2fc86b223849fe81e5405ae
Author: Nicolas Brondin-Bernard 
Date:   Mon Dec 14 22:10:24 2020 +0100

    [FEATURE] Projects - List

commit ceeebc74d33733a1f76f8a2dae0a24166c01a21a
Author: Nicolas Brondin-Bernard 
Date:   Mon Sep 2 14:06:37 2019 +0200

    [INITIAL] Project based on React boilerplate

As shown above, the hash is present next to the keyword "commit", so you can then copy-paste it for the next step.

git revert

The first method is to use git revert to undo one or more commits, while keeping a history of the changes.

Indeed, revert doesn't simply "erase" the commits made after the one you're targeting, it creates a new commit containing the inverse of all the changes made up to that point.

Imagine you committed the creation of 25 lines of code, using git revert, the version manager will create a commit in which it removes these same 25 lines, this allows you both to return the project to the desired state, while keeping a history of what was changed and then undone.

Let's take the example of a Git history as follows:

o commit (hash aaa)
|
o commit (hash bbb)
|
o commit (hash ccc)

By running the following command:

$ git revert bbb

The project history will then be:

o commit (hash aaa)
|
o commit (hash bbb)
|
o commit (hash ccc)
|
o commit revert (hash ddd, même état que bbb, annulation des modifications de ccc) 

Your project will be returned to a correct state, and you will therefore have the history of the changes (and the undo) in your version tree.

git reset

With this method, the history of your changes and undos will not be kept, your project will be reset to the state of the selected commit, and all subsequent commits will simply be removed from the tree.

Using the same project base as before:

o commit (hash aaa)
|
o commit (hash bbb)
|
o commit (hash ccc)

By running the following command:

$ git reset bbb

The project history will then be:

o commit (hash aaa)
|
o commit (hash bbb)

Here your project will have returned to the initial state of commit bbb, and the changes made after this commit have all been undone and are no longer visible.

However, be careful, if you had already pushed your commit ccc to remote, your local version will then find itself behind the remote version and your next push will be rejected. To fix this problem you can tell the remote version that you do indeed want to overwrite these changes by using the "--force" option:

$ git push origin --force

Thought Catalog 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