Skip to content

Instantly share code, notes, and snippets.

@gunjanpatel
Last active May 7, 2024 22:00
Show Gist options
  • Save gunjanpatel/18f9e4d1eb609597c50c2118e416e6a6 to your computer and use it in GitHub Desktop.
Save gunjanpatel/18f9e4d1eb609597c50c2118e416e6a6 to your computer and use it in GitHub Desktop.
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.

@Donotavio
Copy link

Thanks!

@gunjanpatel
Copy link
Author

Thanks a lot for your kind comments. Seeing your comments keeping me motivated. I am very happy that my small notes are helping a lot of people around the world. 😄 🎉

@sathishkumar294
Copy link

Thanks a lot

@TheJazzDev
Copy link

Thanks a lot man, you save the day...

@saniya44
Copy link

THANK YOUU

@jonangeles-sanchez
Copy link

Thank you

@tisufa
Copy link

tisufa commented Feb 4, 2023

Thank you

@miltonmaleiane
Copy link

Thank you

@Ale-Dat
Copy link

Ale-Dat commented Feb 13, 2023

You make me wanna dance.

:)

@DamiJJJ
Copy link

DamiJJJ commented Feb 20, 2023

Cool

@GunjanSurti
Copy link

Thank you!

Solve the problem

@SamAddy
Copy link

SamAddy commented Mar 19, 2023

Thanks a lot. This helped me !:)

@hassan-selise
Copy link

Thanks a lot, @gunjanpatel. Thanks for saving my day

@vinser
Copy link

vinser commented Mar 24, 2023

@gunjanpatel It helped me a lot. Much thanks to you!

@puneet-goel
Copy link

Thanks alot

@tarikulwebx
Copy link

tarikulwebx commented May 18, 2023

This is so helpful! Exactly, what I was looking for...
Thank you, man!

@Javadyakuza
Copy link

god bless your soul , tthanks

@prr21
Copy link

prr21 commented Jun 24, 2023

bruh, where u've been all this time, ty so much <3

@dotKa
Copy link

dotKa commented Aug 11, 2023

Thanks a lot
git push origin +dd61ab32^:master

@JanBaig
Copy link

JanBaig commented Aug 12, 2023

Thank you!

@HackinwaleDev
Copy link

Thank you! This just saved my company from bad code merged. How did you even thought of this cuz it is no where in their documentation

@SteveTuttle
Copy link

This worked perfectly! Thank you so much.

@yatinhiwarkar
Copy link

Thanks a lot Gunjan! I'm learning git and testing out protected branches and accidentally was able to send a commit as I was on the same machine with both accounts!
You saved my sanity...

@deboleksky
Copy link

This is so helpful because i deleted some yaml file and it was a configuration of 20 channels on preprod but i was able to revert it back today. Thanks

@mishns
Copy link

mishns commented Oct 19, 2023

so cool man, so cool

@grentank
Copy link

Thanks a lot!

@rlimapro
Copy link

God bless you!

@SGZ-Creations
Copy link

So how would u run this? I'm only getting error.

@ufuomaisaac
Copy link

Thanks

@tadeeo
Copy link

tadeeo commented May 3, 2024

thank you

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