Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reverse a commit in git?

Tags:

git

github

commit

I'm not really familiar with how git works. I pushed a commit by mistake and want to revert it. I did a

git reset --hard HEAD~1

Beware Fellow Googlers: This does not only revert the commit, but discards all file changes!

and now the project is reverted on my machine, but not on github. If I try to push this code, I get the error 'Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.' How do I remove this commit from github?

like image 451
David Mulder Avatar asked Mar 21 '11 18:03

David Mulder


People also ask

How can I reverse a commit in git?

To revert a commit with GitKraken, simply right-click on any commit from the central graph and select Revert commit from the context menu.


2 Answers

This article has an excellent explanation as to how to go about various scenarios (where a commit has been done as well as the push OR just a commit, before the push):

http://christoph.ruegg.name/blog/git-howto-revert-a-commit-already-pushed-to-a-remote-reposit.html

From the article, the easiest command I saw to revert a previous commit by its commit id, was:

git revert dd61ab32
like image 130
AmpT Avatar answered Oct 24 '22 03:10

AmpT


You can do git push --force but be aware that you are rewriting history and anyone using the repo will have issue with this.

If you want to prevent this problem, don't use reset, but instead use git revert

like image 41
Šimon Tóth Avatar answered Oct 24 '22 03:10

Šimon Tóth