0

[EDIT: The answers to this question throw away history, if that is not what you want then ignore this question].

[EDIT: The answer that CodeWizard pointed to as a duplicate is not a duplicate because this question makes it clear that I want to throw away history. Also, none of the answers in that question are nearly as good as the one that this question received]

I just did a commit and a push of some code that was a complete mistake.

I want to completely back out that commit and push as tho it never happened.

I do not want the code left in the commit history.

No one has done any other commit and push.

This question is different because I am asking people not to post alternatives unless they achieve exactly the same thing (throw away all history). The reason is: I have tried to google this and I get questions that are cluttered with answers that provide alternatives but it is not clear if those alternatives throw away the code or keep the code in the git commit history.

This question is constrained to: 1) Throw away last commit/push only. 2) All history is of that commit/push is gone from the repo.

I am putting those constraints on it so we get the simplest answers possible.

I am using git command line and tortoise git against gitlab.

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125

1 Answers1

3

You acknowledge the dangers of rewriting history. Soo... this will move master back one commit.

git push -f origin master~:master 

Other method would be to reset HEAD

git checkout master
git reset --hard HEAD~
git push origin -f master
EncryptedWatermelon
  • 4,788
  • 1
  • 12
  • 28
  • 1
    It'd be useful to break the command down so that the OP or others can adjust it as needed. The `-f` makes it a forced push, tells the remote repo (`origin` in this case) to take what you give it no matter what. `origin` is of course the local name of the remote you're pushing to. Also explain the `master~:master` -- obviously you'd put something else there if you were pushing from/to a different branch. – Caleb Sep 17 '19 at 16:41