Git - 我们可以恢复已删除的提交吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34751837/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:51:37  来源:igfitidea点击:

Git - Can we recover deleted commits?

gitrestoregit-commit

提问by Atri

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

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

Can we recover/restore deleted commits in git?

我们可以在 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

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

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

回答by Jonathan.Brink

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

要返回该提交,您可以使用reflog来查找它的 ref。

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

引用日志或“reflogs”记录了本地存储库中分支提示和其他引用的更新时间。

Run this command:

运行此命令:

git reflog

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".

扫描前几个条目,并找到丢失的提交。跟踪该提交的标识符(您可以使用第一列或第二列)。我们将标识符称为“ID”。

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

如果您在执行 reset --hard 后没有做任何额外的工作,您可以执行以下操作:

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

回答by Konrad Krakowiak

Yes, You can find your commit in refloguse:

是的,您可以找到正在reflog使用的提交:

git reflog

to display all commits which are/were created in your repository - after this you should checkout to removed commit by checkout command

显示在您的存储库中创建/创建的所有提交 - 在此之后,您应该通过 checkout 命令检出删除提交

git checkout <your commit-SHA>

or cherry-pick it by:

或通过以下方式挑选它:

git cherry-pick <your commit-SHA>

回答by Chaoyu

Try git reflog, aka Reference logs, it allows you to go back to history in your local repo.

Try git reflog,又名参考日志,它允许您返回本地存储库中的历史记录。

https://git-scm.com/docs/git-reflog

https://git-scm.com/docs/git-reflog