You have commited and pushed changes to git and want to go back to the previous commit. In this article, you learn how to execute a series of commands to do roll back a pushed commit.

Git undo a pushed commit.

Execute 'git log' to find the commit you want to rollback to. The terminal should show something like this.

$ git log
commit 4bc90d5e397fc13f086f76132206be6de8e42a (HEAD -> main, origin/main, origin/HEAD)
Author: Pietje <pietje@tostie.com>
Date:   Tue Aug 2 14:29:54 2022 +0200

    "bad commit"     <--- last push to the server

commit 7d880ea2eabe5454437d44427c3eea5822897a
Author: Pietje <pietje@tostie.com>
Date:   Thu Jun 23 10:19:57 2022 +0200

    "good commit"    <--- previous push to the server

Rollback to the previous commit (undo pushed commit)

$ git reset --hard 7d880ea2eabe5454437d44427c3eea5822897a
$ git reset HEAD~1
$ git stash
$ git add -A
$ git commit -m "I promise not to do it again"
$ git push --force

That should undo (rollback) the last pushed commit and bring client and server in sync again. Use it at your own risk!

Written by Loek van den Ouweland on 2022-08-02.
Questions regarding this artice? You can send them to the address below.