中止旧的 git rebase 并自 rebase 开始后丢失提交

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

Aborted old git rebase and lost commits since the rebase started

gitgit-rebase

提问by Dan Breen

Crap! About a week ago, I was rebasing some commits while trying to clean up my repository, and apparently I didn't actually finish it. Today, a week and several commits later, I went to rebase to reorder a few commits from today, and it told me I was already in the middle of a rebase.

废话!大约一周前,我在尝试清理我的存储库时重新设置了一些提交,显然我并没有真正完成它。今天,一周和几次提交后,我去 rebase 重新排序今天的一些提交,它告诉我我已经在 rebase 中了。

That should have been a cue to copy my repo just in case. But I did not...instead I ran git rebase --abortwhich sounded right at the time. Well, that was not right. It aborted the rebase from a week ago and reset master's HEAD to the old one. Dummy!

这应该是复制我的 repo 以防万一的提示。但我没有......相反,我跑了git rebase --abort,当时听起来不错。好吧,那是不对的。它中止了一周前的 rebase,并将 master 的 HEAD 重置为旧的。假的!

I've got several other branches that are fairly recent, and I've pushed to remote several times, but the most recent changes appear to be gone forever. I don't possess the appropriate level of git-fu to know if there's any way to recover my changes.

我还有其他几个相当新的分支,并且我已经多次推送到远程,但是最近的更改似乎永远消失了。我没有适当级别的 git-fu 来知道是否有任何方法可以恢复我的更改。

Am I screwed?

我搞砸了吗?

EDIT- WOW! Thanks guys! git reflogis awesome! I'm fully recovered...lesson learned. Marking Tchalvak's answer accepted for being the first to post.

编辑- 哇!谢谢你们!git reflog太棒了!我完全康复了……吸取了教训。标记 Tchalvak 的回答是第一个发布的。

回答by Kzqai

Check git reflog. You can walk back in time using those commit hashes as a reference in almost all cases.

检查git reflog。在几乎所有情况下,您都可以使用这些提交哈希作为参考来回到过去。

I'd also physically copy the git repo directory elsewhere as a place to do preliminary testing to see what will work, that way you can mess with whatever you want without losing untracked files or getting things into a state that you can'tcome back from.

我还会在其他地方物理复制 git repo 目录作为进行初步测试的地方,看看什么会起作用,这样你就可以随意处理你想要的任何东西而不会丢失未跟踪的文件或让事情进入你无法回来的状态从。

回答by VonC

You should be able to get the SHA1 of your most recent commits (that disappeared after the rebase --abort) with a git reflog.

您应该能够使用git reflog.

You will be able then to reset your current branch to those SHA1

然后,您将能够将当前分支重置为那些 SHA1

# Suppose the old commit was HEAD@{2} in the ref log
git reset --hard HEAD@{2}

It is a bit like "Undoing a git reset --hard HEAD~1".

这有点像“撤销一个git reset --hard HEAD~1”。

See also the "illustrated guide to recovering lost commits with Git", for other examples of recovery.

另请参阅“使用 Git 恢复丢失提交的插图指南”,了解其他恢复示例。