简体   繁体   中英

Using GitHub, how can I restore my local repository to the state it was in before my last push to the origin?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM