Git 丢失提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6718672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git lost commits
提问by ggg
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.
我试图解决 git 中的一个问题,但不小心使用了 git reset --hard 来处理之前的某个提交。所以现在我无法回到原来的头脑。
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.
但是,我在这样做之前确实克隆了存储库,因此我将丢失的提交推回了原始提交。这似乎在昨天有效,但今天我看到原始提交仍然停留在旧提交上,而新提交似乎不存在。尝试再次从克隆中推送新提交不起作用,因为 git 告诉我一切都是最新的。
How do I fix this?
我该如何解决?
回答by Andy
To get your HEAD back in the right place:
要让您的 HEAD 回到正确的位置:
git reflog
to get a list of where HEAD has beenlately.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.
git reflog
得到的头部有一个地方名单已经最近。git show sha1
找到您希望 HEAD 所在的位置。- 一旦你找到你想要的提交,
git merge
让你的主人回到正确的位置。
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 danglingwithout anything pointing to it.
一些解释:在一次git
提交中,没有任何内容将一次提交指向之后发生的提交。当您重置 HEAD 时,您将其指向一个较旧的提交。你以前的头现在悬着,没有任何东西指向它。
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!
我们reflog
过去常常看到 HEAD 最近去了哪里。一旦它被设置回你想要的位置,你将主节点或其他分支指向那个位置,一切都很好!
回答by Mark Locklear
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 [email protected]: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...
...在这种情况下,我的“为 rubric 添加了一般性评论”是我丢失的提交。现在我有了提交 ID,我用cherry-pick 来取回它......
git cherry-pick edbfb06
回答by VonC
You shouldn't need to push anything from another repo after a git reset --hard
.
在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 expiresif you really want that).
See for instance Undoing a git reset --hard HEAD~1
or restore - git reset --hard HEAD^
as examples.
git reflog
应该允许您找到丢失的提交(实际上未引用,但在您的 Git 存储库中仍然存在,默认情况下直到 90 天:请参阅git config gc.reflogexpire
。如果您真的想要它,您甚至可以确保它永不过期)。
例如,参见Undoing agit reset --hard HEAD~1
或restore -git reset --hard HEAD^
作为示例。
回答by Kit Ho
Try to type git fetch
to get the updated repository
尝试键入git fetch
以获取更新的存储库