Git-我们可以恢复已删除的提交吗? [英] Git - Can we recover deleted commits?

查看:390
本文介绍了Git-我们可以恢复已删除的提交吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,我在SO上找不到答案.

I am surprised, I couldn't find the answer to this on SO.

我们可以在git中恢复/恢复已删除的提交吗?

Can we recover/restore deleted commits in git?

例如,这就是我所做的:

For example, this is what I did:

# Remove the last commit from my local branch
$ git reset --hard HEAD~1

# Force push the delete
$ git push --force

现在,有没有办法找回已删除的提交? git是否在内部记录(记录)删除?

Now, is there a way to get back the commit which was deleted? Does git record(log) the delete internally?

推荐答案

要返回到该提交,可以使用参考来查找它的参考.

To get back to that commit you can use the reflog to look up it's ref.

参考日志或"reflogs"记录分支的提示和 其他参考已在本地存储库中更新.

Reference logs, or "reflogs", record when the tips of branches and other references were updated in the local repository.

运行此命令:

git reflog

扫描前几项,并找到丢失的提交.跟踪该提交的标识符(可以使用第一列或第二列).我们将标识符称为"ID".

Scan the first few entries, and find the commit that was lost. Keep track of the identifier to that commit (you can use either the 1st or 2nd columns). Let's call the identifier "ID".

如果自从进行重设以来没有做任何额外的工作,那么您可以执行以下操作:

If you have not made any extra work since you did the reset --hard you can do:

git reset --hard ID
git push -f origin master

如果自重设以来您还进行了其他工作,则可以像下面这样回到您的分支中进行挑选:

If you have made other work since the reset, you could cherry-pick if back onto your branch like this:

git cherry-pick ID
git push origin master

这篇关于Git-我们可以恢复已删除的提交吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆