78

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?

2
  • git push -f did the trick. Thanks. Mar 21, 2011 at 18:31
  • The fast-forwarding message is just a hint, not really an error. It's the normal message you get if you don't push after each commit.
    – eckes
    Mar 21, 2011 at 19:45

5 Answers 5

75

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):

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

git revert dd61ab32
1
  • wth this discards the changes
    – Merlin
    May 18 at 18:07
43

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

2
  • 5
    Generally, I say rewriting history is not a good idea, but particularly in a shared environment. Go with the git revert May 22, 2012 at 20:38
  • 5
    NOTE: git revert <commit id> deletes newly added files from disk. luckily I had them open in vscode and did cmd+s
    – valem
    Nov 21, 2018 at 6:50
19

Or you can try using git revert http://www.kernel.org/pub/software/scm/git/docs/git-revert.html. I think something like git revert HEAD~1 -m 1 will revert your last commit (if it's still the last commit).

13

Unable to comment on others answers, I'll provide a bit of extra information.

If you want to revert the last commit, you can use git revert head. head refers to the most recent commit in your branch.

The reason you use head~1 when using reset is that you are telling Git to "remove all changes in the commits after" (reset --hard) "the commit one before head" (head~1).

reset is to a commit, revert is on a commit.

As AmpT pointed out, you can also use the commit SHA to identify it, rather than counting how far away from head it is. The SHA can be found in the logs (git log) and a variety of other ways.

You can also always use any other pointers in Git. e.g. a tag or branch. And can also use all of these fun other ways to reference commits https://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html#_specifying_revisions

1
  • "reset is to a commit, revert is on a commit." <= thanks for that sentence, the light bulb just went on
    – brichins
    Jun 8, 2018 at 14:58
2

I think you need to push a revert commit. So pull from github again, including the commit you want to revert, then use git revert and push the result.

If you don't care about other people's clones of your github repository being broken, you can also delete and recreate the master branch on github after your reset: git push origin :master.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.