简体   繁体   中英

How to undo branch push to master

Okay, so I have a forked repo on GitHub, and the upstream repo hasn't been updated since I forked it. Yesterday, I created a branch based from the very last upstream commit which was made before I forked the repo months ago.

I did a bad commit on the branch, so I tried to undo it by using git push -f origin HEAD^:master on my local instance of the branch, but as you can see, I forgot to change master to the name of the branch. So it ended up overwriting everything I've done on the master so far.

My question is, is there a way to undo this mess so I can get the master back in the state it was before this particular push?

I use TortoiseGit and TortoiseSVN a lot, and I can't see leftovers of anything I've done on the master before in any component of Git, however I can still see all my previous commits in the SVN trunk's history log. I don't have a clean local instance of the master in its previous state.

I've tried stuff like git reset --hard <last good commit hash> or git push -f origin <last good commit hash>:master , but it says that no commit with such hash exists. I can still access all the previous commits on GitHub from my browser history.

There is nothing downstream, so no worries about that.

Any tips?

Alright, here's how I fixed it from the branch's local instance:

  1. git log --graph --oneline --all to retrieve the last good commit's hash
  2. git reset --hard <last good commit hash>
  3. git push origin +master

Give git reflog a try. It remembers old commit hashes that might have been lost if you have not done a git gc .

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