-2

I did some work, committed this and pushed it to the origin. Now I realize I want to go back to the point before I did the work.

I would like to restore my local and my origin repository to this earlier commit to the origin and I don't need to keep any of the changes I just made either locally or on the origin.

I really want to be sure I don't make a mistake. How can I do this? Do I need to first restore my local repo and then do another push to the origin?

Here's the command I think I need to do:

git reset --hard HEAD~1

followed by a commit and push to the origin

But it's a bit risky for me so I would like to get some confirmation if I'm correct.

halfer
  • 19,824
  • 17
  • 99
  • 186
Alan2
  • 23,493
  • 79
  • 256
  • 450

1 Answers1

1

Let's assume a recent history like this, where you'd want to return to commit C, since commits D and E are bad. I'll also assume this happens on master branch, but feel free to adapt.

A--B--C--D--E
            ^
          master <<< HEAD

How to locate which is the ref of the "good" commit to return to?

git log --oneline -15

should be enough to spot it (store its hash and use it in next command)


Now let's proceed on your local :

git checkout master
git reset --hard C

Then to reflect your new situation on the remote end (github)

git push --force origin HEAD

And the final situation will be

A--B--C                   D--E (dangling, will be garbage collected later)
      ^
    master <<< HEAD
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61