Accidentally you (or a colleague) have committed and pushed a bad commit, which corrupts everything on the branch. Now you are wondering: how to revert the bad commit?

In this tutorial we will explain how to do a git revert of a commit. Continue reading to check out examples of how to handle it.

1. Deleting or reverting already pushed (remote) commit

Here is an example from a real project. Let's say we have the following commits in the repository on our branch:

0a972b55 good commit
ab156a91 bad commit
100924d2 2nd commit
6cc8a1d1 1st commit

Now you want to do something like this (note: this will NOT work!!!):

  • git revert commit ab156a91
  • git undo commit ab156a91
  • git remove remote ab156a91
  • git revert last commit ab156a91
  • git delete commit ab156a91
  • git remove commit ab156a91

But let us go through this process step by step.

1.1. Ensure your workspace is clean

Check the workspace status with

git status

It should show you something like this:

On branch develop
Your branch is up-to-date with 'origin/develop'.
nothing to commit, working directory clean

1.2. Create a backup of your workspace

At this point we recommend you to create a backup (e.g. with ZIP or tar.gz) of your workspace and follow the next steps.

1.3. Drop all your changes

Backup done? Now execute the following command, which will drop all your changes:

git reset --hard

1.4. Undo the bad commit

At this point we can continue with the initial git undo of the commit. This will be done by taking a commit at least 2 commits older than your bad commit. Looking at our example, this means we'll take the commit 6cc8a1d1.

git rebase -i 6cc8a1d1

1.5. Pick or drop your commits

Git will open an editor, where we can select commits which we want to pick or drop. In our example we will enter:

pick 0a972b55 good commit
drop ab156a91 bad commit
pick 100924d2 2nd commit

After saving the changes we should not have any conflicts in the workspace. Recheck with

git status

1.6. Push your changes

In the best case scenario you only need to push the changes. This is done with:

git push -f

In the worst case scenario you need to fix the conflicts locally to continue with the above mentioned push.

Finally, go to the repository website (Gitlab or Github) and check the commits there. You should now see that the dropped commit as been evicted.

2. Changing the last local commit which is not pushed with git commit --amend

Let's assume you have accidentially commited a local change but you recognize you need to change the last commit. This means:

  1. You want to modify your last commit
  2. The last commit is not pushed, thus it is only locally commited
  3. You want to integrate some additional changes from stage level

Git developers have created a nice feature for Git users to amend (synonym for change) the last commit.

git commit --amend

This command does the magic to append local changes to the last commit.

3. Conclusion

In this article we learned how to revert a commit in Git using the command line. If you'd like to learn more about Git make sure to check out our article about pushing identical code to two remote repositories in GitLab with IntelliJ.