Pergunta

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?

Foi útil?

Solução

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

Outras dicas

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top