Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

Git lost commits

I was trying to fix a problem in git and accidentally used git reset --hard to some previous commit. So now I cannot get back to the old head.

However I did clone the repository before I did this, and so I pushed the missing commits back to the original. This seemed to work yesterday, but today I see that the original is still stuck on an old commit and the new ones seemingly don't exist. Trying to push the new commits from the clone again don't work as git tells me everything is up to date.

How do I fix this?

question from:https://stackoverflow.com/questions/6718672/git-lost-commits

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To get your HEAD back in the right place:

  1. git reflog to get a list of where HEAD has been lately.
  2. git show sha1 to find the spot you want your HEAD to be.
  3. Once you find the commit you want, git merge to get your master back into the right spot.

Some explanation: In a git commit there is nothing pointing one commit to the one that happend after it. When you reset the HEAD, you pointed it to an older commit. Your previous head is now dangling without anything pointing to it.

We use reflog to see where HEAD has been lately. Once it is set back to where you want it, you point the master, or some other, branch back to that spot and all is well!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...