3

I messed up...

How do I undo a pushed commit so that it's like it never happened?

Thanks!

fancy
  • 48,619
  • 62
  • 153
  • 231

2 Answers2

4

Though Danial Pittman has provided you with the process for 'erasing' the action you have taken, I would suggest another approach. If possible, you would be better off fixing the changes you have made and then committing those which essentially overrides the previous commit. Yes people will still be able to look in the repo history and possibly see the 'mistake', but this is a safer approach. Furthermore, in the event that you do actually ever want the changes you made back, they will still be in the repository when following this method. Hope that helps!

jbranchaud
  • 5,909
  • 9
  • 45
  • 70
  • 3
    This doesn't help if the commit contains sensitive information like passwords. That's my interpretation of his wish, not to undo what was done, but to make it like it never happened. – johnny Jan 25 '12 at 07:19
  • 2
    I understand, I just wanted to make sure that others who view this question know their options. – jbranchaud Jan 25 '12 at 15:42
2

Warning: every step here is destructive. Don't get it wrong, and take a copy of your repo before you start.

git reset --hard $what, where what is the SHA or whatever you want to go back to on that branch. (eg: HEAD^ if you just want to discard the latest commit.)

Once you have done that, git push --force ... to get that to the remote repository.

That solves everything ... except removing the dead blob, etc, from the remote repository. If that matters to you, life is harder. There isn't any protocol way to get rid of it, so you either delete the remote repo and create a new, clean copy, or do something back-end specific.

Daniel Pittman
  • 16,733
  • 4
  • 41
  • 34
  • 1
    Also note that `git push --force` generally means that if anyone else was working with you and already pulled your commit, they'll have to do a bunch of recovery on their end as well. – Amber Jan 25 '12 at 06:15
  • Thanks, worked great. No the repo is just mine and it was a flub initializing the project, really early in the process so nothing to lose. ThanksQ – fancy Jan 25 '12 at 06:27