Skip to content

Instantly share code, notes, and snippets.

Embed
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 http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html - Thank you.

@ahampriyanshu
Copy link

ahampriyanshu commented Dec 22, 2020

thank you

@Melvin-Abraham
Copy link

Melvin-Abraham commented Jan 17, 2021

Thanks a ton!!

@RomellPineda
Copy link

RomellPineda commented Feb 13, 2021

This is cash money $$$

@abhinav-singh-vineti
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.

@VictorHueni
Copy link

VictorHueni commented May 19, 2021

Thanks you !

@edouard-claude
Copy link

edouard-claude commented May 28, 2021

thanks!

@OzzyTheGiant
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

@abrahamuchos
Copy link

abrahamuchos commented Jul 12, 2021

Thanks !!!

@Rados51
Copy link

Rados51 commented Jul 18, 2021

Thanks!!!!!!

@NirajanMahara
Copy link

NirajanMahara commented Jul 26, 2021

This is a upvote !!

@zakaria-chahboun
Copy link

zakaria-chahboun commented Sep 7, 2021

Thanks!

@craftmaster1231
Copy link

craftmaster1231 commented Oct 23, 2021

TY

@Davidson015
Copy link

Davidson015 commented Oct 28, 2021

Thank You Much

@nNabakhteveli
Copy link

nNabakhteveli commented Nov 6, 2021

Thank you!

@GrigoriyMo
Copy link

GrigoriyMo commented Nov 13, 2021

Thank You!!!

@mattiaerre
Copy link

mattiaerre commented Nov 29, 2021

thank you thank you thank you

@farizbaiquni
Copy link

farizbaiquni commented Dec 29, 2021

Thank you

@cmokbel1
Copy link

cmokbel1 commented Jan 24, 2022

You make me wanna dance.

@3kh0
Copy link

3kh0 commented Jan 25, 2022

how can you revert a commit if you only have access to github.com?

@bosunski
Copy link

bosunski commented Feb 15, 2022

Thanks!

@RoyZhang7
Copy link

RoyZhang7 commented Mar 9, 2022

Saved my day! Thank you!

@TieWay59
Copy link

TieWay59 commented Mar 29, 2022

Thanks!

@sethbarrett50
Copy link

sethbarrett50 commented Apr 18, 2022

Thank you!

@Formasitchijoh
Copy link

Formasitchijoh commented May 2, 2022

Very useful , thank you!

@AradhanaChhabra
Copy link

AradhanaChhabra commented May 5, 2022

Thank you!

@a-dri-an-S
Copy link

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

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

@tuanluu0511
Copy link

tuanluu0511 commented May 20, 2022

Thank a lot!

@Mohit8561
Copy link

Mohit8561 commented Jun 29, 2022

thanks

@ankur0101
Copy link

ankur0101 commented Jul 9, 2022

Thanks mate

@vahidalvandi
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