Answers:
只要您没有完成git gc
,就不会丢失任何东西。您需要做的就是再次找到它:)您会得到什么:
git reflog show
那应该向您显示发生了什么,以及缺少的节点的ID。
上面的答案是正确的。这是我所做的:
$ git reflog
5b35f6d HEAD@{1}: pull github master: Fast forward
ca92d15 HEAD@{2}: checkout: moving from 759dab1b15731ce7680c26839ca470d20e709e36 to master
759dab1 HEAD@{3}: commit (merge): Merge branch 'master' of github.com:gonzojive/IODB-ui into HEAD
065e269 HEAD@{4}: commit: added fieldsets to snazzy form
f357606 HEAD@{5}: commit: preliminary support for google maps.
ca92d15 HEAD@{6}: checkout: moving from master to ca92d15d272867b63d54f96d4aa57f8ecc479cd0
$ git checkout ca92d15d272867b63d54f96d4aa57f8ecc479cd0
“哦,不!” 片刻是这样的:
checkout: moving from master to ca92d15d272867b63d54f96d4aa57f8ecc479cd0
ca92d15d272867b63d54f96d4aa57f8ecc479cd0是显示为(无分支)的匿名分支。要恢复到原来的状态,只需执行git checkout即可恢复旧的伪分支。
我建议您在不小心gc之前备份git存储库,只是为了省心。
git reset --hard <commit-id>
。提交ID是的第一列中的字母数字代码git reflog
。参见effectif.com/git/recovering-lost-git-commits。
# if you have already checked out to master,
# you won't know the commit-ish of your "no branch":
git fsck --lost-found # (to find your <commit-ish>)
git merge <commit-ish>
# if you are still on your "no branch" commit:
git log # (the commit-ish will be on the first line)
git checkout master
git merge <commit-ish>
# or
git log | head -n 1 | cut -d ' ' -f 2 | pbcopy
git checkout master
git merge <commit-ish>