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.

@aviggiano

This comment has been minimized.

Copy link

commented Jun 11, 2017

Thanks

@dongwooklee9

This comment has been minimized.

Copy link

commented Jun 22, 2017

thanks!

@higee

This comment has been minimized.

Copy link

commented Jun 22, 2017

thanks!

@imjaroiswebdev

This comment has been minimized.

Copy link

commented Aug 14, 2017

thanks!

@daniele-pecora

This comment has been minimized.

Copy link

commented Aug 18, 2017

THX

@Prakashja

This comment has been minimized.

Copy link

commented Sep 15, 2017

Thanks

@Triskae

This comment has been minimized.

Copy link

commented Sep 20, 2017

Thanks a lot !

@NinoSkopac

This comment has been minimized.

Copy link

commented Sep 21, 2017

fu

@bbwstar

This comment has been minimized.

Copy link

commented Sep 21, 2017

Thanks for your posting!!! :)

@sshelake25

This comment has been minimized.

Copy link

commented Sep 29, 2017

Thank you, worked for me.

@tonytee50

This comment has been minimized.

Copy link

commented Sep 29, 2017

Thanks!

@emrehayirci

This comment has been minimized.

Copy link

commented Oct 9, 2017

Thanks!

@JeffBship

This comment has been minimized.

Copy link

commented Oct 11, 2017

Is there a method to do this from Github.com without using the command line?

@VaibhavPal

This comment has been minimized.

Copy link

commented Nov 14, 2017

I am stuck in a situation where I need to push these reverts to master from my own branch? master does not recognize any change to be merged as it already has these commits. Since no one is allowed to directly work on master these reverts are stuck in my branch. Any suggestion how to achieve this?

@diegoalejogm

This comment has been minimized.

Copy link

commented Nov 21, 2017

Thanks!

@teckel12

This comment has been minimized.

Copy link

commented Dec 21, 2017

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.

@buluma

This comment has been minimized.

Copy link

commented Dec 29, 2017

amazing. thank you!

@abidra

This comment has been minimized.

Copy link

commented Jan 4, 2018

Thanks!

@yanlee26

This comment has been minimized.

Copy link

commented Jan 29, 2018

Thanks!

@ThakurBallary

This comment has been minimized.

Copy link

commented Jan 30, 2018

helpful, thanks bro!

@TuckerWatts

This comment has been minimized.

Copy link

commented Feb 14, 2018

Thank you!

@rashideescis

This comment has been minimized.

Copy link

commented Feb 21, 2018

Thanks!

@yaandroid

This comment has been minimized.

Copy link

commented Mar 6, 2018

thxs

@AppleInside

This comment has been minimized.

Copy link

commented Mar 9, 2018

I love you!

@daveseco7

This comment has been minimized.

Copy link

commented Mar 27, 2018

Thanks a lot 👍

@manuel-alcocer

This comment has been minimized.

Copy link

commented Apr 2, 2018

git commit -am "thanks!"

@sahilbhatt92

This comment has been minimized.

Copy link

commented Apr 10, 2018

Thanks a lot 👍 Saved my Life.

@MRamonLeon

This comment has been minimized.

Copy link

commented Apr 11, 2018

thanks, It happens in the best families :-D 💯

@kinchungwong

This comment has been minimized.

Copy link

commented Apr 12, 2018

As other comments have pointed out, this has saved my life too.

This is safe to do so if the user follows the GitHub recommended workflow. The extra safety is made possible because of the recommended workflow.

In particular, creating a forked repository, branching (creating a topic branch), and then committing changes to it, means that any "potential danger" from using "dangerous" git commands (such as reset hard, force push) are limited to one's forked repository.

The safety doesn't exist for GitHub moderators, users with push rights, or teams that use a single repository model. These power-users must know what they are doing, and accept whatever consequences that may happen.

Beginning git users (myself included) are recommended to follow the GitHub recommended workflow as much as possible, until a certain level of git maturity is reached.

Teams typically have their own git workflows. When working on a team (i.e. everyone is given the same access like a power-user), the team's git workflow must be followed strictly. These git workflows are sometimes different from the GitHub ones.

@firemaples

This comment has been minimized.

Copy link

commented Apr 26, 2018

Thanks a lot!

@Mchristos

This comment has been minimized.

Copy link

commented May 3, 2018

changed my life

@markwang1971

This comment has been minimized.

Copy link

commented May 5, 2018

"git reset HEAD^ --hard" is success, "git push origin -f" is failed. Why?

the below is logs of my operations:

$ git log
commit 4ea0a388f9134c9b162484b49dcaaf28ff146f0a (HEAD -> master, origin/master, origin/HEAD)
Author: Yuhou Wang wangyh@wwwscn.com
Date: Fri May 4 18:33:35 2018 +0800

add test.txt for testing

commit c8b5933df6b0dad5e5724b2949a388dc636a0a92
Author: Yuhou Wang wangyh@wwwscn.com
Date: Fri May 4 18:30:24 2018 +0800

add project filters file.(all)

commit b42a0ceb5607e67814239d178f2e7b672edeb19c
Author: dushixiong
Date: Mon Apr 23 17:31:30 2018 +0800

jie jue liu zai ci ye went ti

commit 6d566eab5150d79ca7371d2829ab6bc6e3f9e3da
Author: dushixiong
Date: Mon Apr 23 16:25:53 2018 +0800

original browser code

mwang@DESKTOP-31GQFE1 MINGW64 /e/vs2013/BrowserChrome (master)
$ git reset HEAD^ --hard
HEAD is now at c8b5933df6 add project filters file.(all)

mwang@DESKTOP-31GQFE1 MINGW64 /e/vs2013/BrowserChrome (master)
$ git log
commit c8b5933df6b0dad5e5724b2949a388dc636a0a92 (HEAD -> master)
Author: Yuhou Wang wangyh@wwwscn.com
Date: Fri May 4 18:30:24 2018 +0800

add project filters file.(all)

commit b42a0ceb5607e67814239d178f2e7b672edeb19c
Author: dushixiong
Date: Mon Apr 23 17:31:30 2018 +0800

jie jue liu zai ci ye went ti

commit 6d566eab5150d79ca7371d2829ab6bc6e3f9e3da
Author: dushixiong
Date: Mon Apr 23 16:25:53 2018 +0800

original browser code

mwang@DESKTOP-31GQFE1 MINGW64 /e/vs2013/BrowserChrome (master)
$ git push origin -f
Total 0 (delta 0), reused 0 (delta 0)
remote: error: denying non-fast-forward refs/heads/master (you should pull first)
To http://192.168.199.158:80/BrowserChrome.git
! [remote rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'http://192.168.199.158:80/BrowserChrome.git'

mwang@DESKTOP-31GQFE1 MINGW64 /e/vs2013/BrowserChrome (master)

@ltj-tebengan

This comment has been minimized.

Copy link

commented May 7, 2018

Thankyou verymuch.

@ujjwalagrawal17

This comment has been minimized.

Copy link

commented May 18, 2018

Thanks !!

@DaseinUXD

This comment has been minimized.

Copy link

commented Jun 5, 2018

Danke

@zairiel243

This comment has been minimized.

Copy link

commented Jun 9, 2018

thanks fam

@ammark1

This comment has been minimized.

Copy link

commented Jun 12, 2018

Hi,

Suppose I reverted a commit using 'git revert {commit_id}'. But the commit I reverted wasn't actually a bad commit, it was just that I wanted to hold it for now. After some time(and after many other commits), I again want to push the reverted commit changes.

How can I do that? Because reverting a commit is not deleting it, it's still there. How do I push that reverted commit again?

@RockChild

This comment has been minimized.

Copy link

commented Jun 19, 2018

@ammark1 just simple: revert your reverted commit :)

@maustyle

This comment has been minimized.

Copy link

commented Jun 20, 2018

grazie

@bbwong23

This comment has been minimized.

Copy link

commented Jun 26, 2018

@RockChild it doesn't seem like github allows you to revert a reverted commit. I get the following error message when I try to do so "Sorry, this pull request couldn’t be reverted automatically. It may have already been reverted, or the content may have changed since it was merged"

@animesharma

This comment has been minimized.

Copy link

commented Jul 10, 2018

Thanks!

@adeutscher

This comment has been minimized.

Copy link

commented Jul 14, 2018

A note on this: If you are looking to outright delete a commit because you accidentally pushed up sensitive information (e.g. password, private keys, etc), some traces of it can still be found and you should still consider it compromised. Reset any credentials so that the pushed information is invalid even if somebody does stumble across it. The nuclear option would be to completely re-make the repository and push back up from a clone without your bad commit.

For an example of commit information sticking around, I went and did a test along the lines of the following:

date > good.txt
git commit -m "Good Commit" good.txt
# For ease, assume that this good commit's SHA starts with 12345678.
date > bad.txt
git commit -m "Bad Commit" bad.txt
# For ease, assume that this good commit's SHA starts with abcdefgh. The commit is so bad that it does not even have a valid hex value.
git push -u origin master
# "Oh no, I didn't want to push that!"
git push origin +abcdefgh^:master

An example of this can be found here: adeutscher/commit-delete-example@1c6de87 . I can no longer see the "bad" commit in a fresh clone, but anyone crafty enough to find the link in GitHub could still see it.

When I do a fresh clone, I'm unable to see this commit. However, it still exists in GitHub. A GitLab instance will behave the same way. If I find a way to fully purge this I'll edit this comment or make another reply.

@Haseo97

This comment has been minimized.

Copy link

commented Jul 17, 2018

Thanks! Helped too much!

@kenanduman1988

This comment has been minimized.

Copy link

commented Jul 19, 2018

Thanks !

@sujitpk-perennial

This comment has been minimized.

Copy link

commented Jul 20, 2018

Hello, If I delete a top most commit which I already pushed, is it going to reduce my repo size? I have raised this question because currently my repo crossed it's 2 GB limit.

@fr3fou

This comment has been minimized.

Copy link

commented Jul 20, 2018

thank,, sir

@meherett

This comment has been minimized.

Copy link

commented Jul 23, 2018

Thanks!

@vorotech

This comment has been minimized.

Copy link

commented Aug 10, 2018

Thanks!

@JohnHake

This comment has been minimized.

Copy link

commented Aug 22, 2018

Thank you!

@SwiftiSwift

This comment has been minimized.

Copy link

commented Aug 26, 2018

THANK YOU <3

@Brambor

This comment has been minimized.

Copy link

commented Aug 27, 2018

Thanks!

@francopesce

This comment has been minimized.

Copy link

commented Sep 7, 2018

thanks!

@Boolean-Operator

This comment has been minimized.

Copy link

commented Sep 14, 2018

Thanks, two step version worked great.

@16point7

This comment has been minimized.

Copy link

commented Dec 8, 2018

Your post helped to solve my issue and also motivated me to learn about HEAD, branches, and tags. Thanks!

@srvikram13

This comment has been minimized.

Copy link

commented Dec 18, 2018

Thanks

@MUNMO

This comment has been minimized.

Copy link

commented Dec 19, 2018

hi everyone.

It's URGENT any help is highly appreciated.

I'm trying to git reset --hard commit-id and git push origin -f but it does not allow me so it says

"remote: error: hook declined to update refs/heads". I'm in origin branch trying to push the changes but whenever i push the changes it asks me to take pull. Can anyone help me it's urgent.Thanks in advance.

@Mynziak

This comment has been minimized.

Copy link

commented Feb 20, 2019

great!

@polluks

This comment has been minimized.

Copy link

commented Mar 19, 2019

Please remove the trailing apostroph.

@Ecommerce-App

This comment has been minimized.

Copy link

commented Apr 12, 2019

Thanks so much!
Love u :D

@ziyitony

This comment has been minimized.

Copy link

commented Apr 17, 2019

Thank you very much!

@latambavarun

This comment has been minimized.

Copy link

commented May 1, 2019

Thanks

@mrgumorist

This comment has been minimized.

Copy link

commented May 4, 2019

Thanks

@manezaf8

This comment has been minimized.

Copy link

commented May 28, 2019

Fix the wrong commit and push

$ git log --graph --oneline --all
$ git checkout HEAD^ /../../file.php
$ git commit -am ".. message"
$ git push origin HEAD --force

$ git rm -rf --cached ../../file.php
$ git reset --soft HEAD^1

$ git rm --cached ../../../file
$ echo $FILE >> .gitignore
$ git add .gitignore
$ git commit --amend --no-edit
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

remove the whole repo

$ git reset HEAD^ --hard
$ git push origin -f
@ganesh-sadanala

This comment has been minimized.

Copy link

commented Jun 22, 2019

Really amazing!

@sebastianjonasson

This comment has been minimized.

Copy link

commented Jul 8, 2019

git revert worked for me. Had to revert the last commit waiting for a another module to get published. Used git revert to revert the last commit. When the other module got published, I used git cherry-pick to reapply the reverted commit.

@goldy28

This comment has been minimized.

Copy link

commented Jul 13, 2019

thanks !

@garg-saurav

This comment has been minimized.

Copy link

commented Jul 17, 2019

Really helpful! Clear and concise. Thanks!

@soujanyat

This comment has been minimized.

Copy link

commented Jul 18, 2019

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.