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开发者_如何学C 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?
To get your HEAD back in the right place:
git reflog
to get a list of where HEAD has been lately.git show sha1
to find the spot you want your HEAD to be.- 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!
I did it a little differently. I did...
git reflog
3bd79d2 HEAD@{2}: checkout: moving from edbfb06528c43586a0e0e10a73051e06980b9281 to master
edbfb06 HEAD@{3}: commit: added general comments for rubric
f8ca172 HEAD@{4}: checkout: moving from 904d63bf08f6f6b1494bfa473b158b9509b18423 to
904d63b HEAD@{10}: commit: updated results page and csv
933f2a6 HEAD@{11}: commit: updates
f56e6cd HEAD@{12}: clone: from git@heroku.com:xxxx.git
...in this case my "added general comments for rubric" was the commit that I lost. Now that I have the commit ID I used cherry-pick to get it back...
git cherry-pick edbfb06
You shouldn't need to push anything from another repo after a git reset --hard
.
git reflog
should allow you to locate the lost commits (which are actually unreferenced, but still there in your Git repo, by default until 90 days: see git config gc.reflogexpire
. You can even make sure it never expires if you really want that).
See for instance Undoing a git reset --hard HEAD~1
or restore - git reset --hard HEAD^
as examples.
Try to type git fetch
to get the updated repository
精彩评论