Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Git HowTo: revert a commit already pushed to a remote repository

Revert the full commit

Sometimes you may want to undo a whole commit with all changes. Instead of going through all the changes manually, you can simply tell git to revert a commit, which does not even have to be the last one. Reverting a commit means to create a new commit that undoes all changes that were made in the bad commit. Just like above, the bad commit remains there, but it no longer affects the the current master and any future commits on top of it.

git revert {commit_id}

About History Rewriting

Delete the last commit

Deleting the last commit is the easiest case. Let's say we have a remote origin with branch master that currently points to commit dd61ab32. We want to remove the top commit. Translated to git terminology, we want to force the master branch of the origin remote repository to the parent of dd61ab32:

git push origin +dd61ab32^:master

Where git interprets x^ as the parent of x and + as a forced non-fastforward push. If you have the master branch checked out locally, you can also do it in two simpler steps: First reset the branch to the parent of the current commit, then force-push it to the remote.

git reset HEAD^ --hard

git push origin -f

This document is inspired by - Thank you.

Copy link

ahampriyanshu commented Dec 22, 2020

thank you

Copy link

Melvin-Abraham commented Jan 17, 2021

Thanks a ton!!

Copy link

RomellPineda commented Feb 13, 2021

This is cash money $$$

Copy link

abhinav-singh-vineti commented May 11, 2021

I do the git revert {commit_id} but then I can't push, it says non-fast-forward, the top of my current branch is behind.

You need to force push.

Copy link

VictorHueni commented May 19, 2021

Thanks you !

Copy link

edouard-claude commented May 28, 2021


Copy link

OzzyTheGiant commented Jun 5, 2021

Thanks a lot. Been wondering how to perform a commit rollback after I pushed, without having to do a merge

Copy link

abrahamuchos commented Jul 12, 2021

Thanks !!!

Copy link

Rados51 commented Jul 18, 2021


Copy link

NirajanMahara commented Jul 26, 2021

This is a upvote !!

Copy link

zakaria-chahboun commented Sep 7, 2021


Copy link

craftmaster1231 commented Oct 23, 2021


Copy link

Davidson015 commented Oct 28, 2021

Thank You Much

Copy link

nNabakhteveli commented Nov 6, 2021

Thank you!

Copy link

GrigoriyMo commented Nov 13, 2021

Thank You!!!

Copy link

mattiaerre commented Nov 29, 2021

thank you thank you thank you

Copy link

farizbaiquni commented Dec 29, 2021

Thank you

Copy link

cmokbel1 commented Jan 24, 2022

You make me wanna dance.

Copy link

3kh0 commented Jan 25, 2022

how can you revert a commit if you only have access to

Copy link

bosunski commented Feb 15, 2022


Copy link

RoyZhang7 commented Mar 9, 2022

Saved my day! Thank you!

Copy link

TieWay59 commented Mar 29, 2022


Copy link

sethbarrett50 commented Apr 18, 2022

Thank you!

Copy link

Formasitchijoh commented May 2, 2022

Very useful , thank you!

Copy link

AradhanaChhabra commented May 5, 2022

Thank you!

Copy link

a-dri-an-S commented May 7, 2022

OMG! Exactly what I was looking for. Thank you!

Copy link

tuanluu0511 commented May 20, 2022

Thank a lot!

Copy link

Mohit8561 commented Jun 29, 2022


Copy link

ankur0101 commented Jul 9, 2022

Thanks mate

Copy link

vahidalvandi commented Aug 9, 2022

thank s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment