撤销 cherry-pick --abort?

3

我在过去的一周中,使用以下命令从我的主分支中挑选了超过70个提交并将它们转移到另一个分支:

git cherry-pick -x -n <commit-id>
(made some modifications and then)
git commit

状态显示:你的分支比 'origin/another-branch' 领先 76 个提交。

刚才我还以为可以继续我的任务并挑选一些提交。但今天第一个提交是错误的,我想撤销这个操作,于是使用了以下命令:

git cherry-pick --abort

突然间,似乎所有的70多次提交都不见了。状态显示您的分支领先于 'origin/another-branch' 2个提交。

Reflog 显示以下最后两行:

c398477f HEAD@{0}: reset: moving to c398477fa2b2e0e78cb628c75df81b2c1ec411cd
8369312d HEAD@{1}: checkout: moving from master to another-branch

请问是否有一种方法可以撤销中止操作?这些提交仅存在于我的本地分支。而且,即使我明确地提交了每一个提交,它怎么会擦除所有的cherry-pick提交呢?

以下是我一周前开始cherry-pick时的引用日志:

d8a71aca HEAD@{52}: checkout: moving from another-branch to dev
8369312d HEAD@{53}: commit: xxx
...
3bb1ff07 HEAD@{127}: commit: xxx
2b9b6542 HEAD@{128}: commit: xxx
c398477f HEAD@{129}: reset: moving to HEAD^
b373db60 HEAD@{130}: commit: xxx
c398477f HEAD@{131}: commit: xxx
8fb419aa HEAD@{132}: commit: xxx
844cbe24 HEAD@{133}: reset: moving to 844cbe2499aadcd0d014999ddb6f847c1d940440
844cbe24 HEAD@{134}: reset: moving to 844cbe24
41e7dbed HEAD@{135}: checkout: moving from 844cbe2499aadcd0d014999ddb6f847c1d940440 to aller-dev
844cbe24 HEAD@{136}: checkout: moving from another-branch to 844cbe24
41e7dbed HEAD@{137}: reset: moving to HEAD^
844cbe24 HEAD@{138}: reset: moving to HEAD^
81bf86ac HEAD@{139}: cherry-pick: xxx
844cbe24 HEAD@{140}: checkout: moving from dev to another-branch

同理,reflog --all 也是一样的:

c398477f refs/heads/another-branch@{0}: reset: moving to c398477fa2b2e0e78cb628c75df81b2c1ec411cd
8369312d refs/heads/another-branch@{1}: commit: xxx
6a4da110 refs/heads/another-branch@{2}: commit: xxx
...
2b9b6542 refs/heads/another-branch@{75}: commit: xxx
c398477f refs/heads/another-branch@{76}: reset: moving to HEAD^
b373db60 refs/heads/another-branch@{77}: commit: xxx
c398477f refs/heads/another-branch@{78}: commit: xxx
8fb419aa refs/heads/another-branch@{79}: commit: xxx

我能不能直接检出8369312d这个提交,它在之前的reset: moving to c398477fa2b2e0e78cb628c75df81b2c1ec411cd之前?


当你使用 -n 时,你告诉 git cherry-pick 不要 提交它自己的提交。当你运行 git cherry-pick --abort 时,你告诉 Git 把一切都恢复到你开始之前的状态。任何你 明确 提交的内容都会被保存并在一段时间内保持安全(默认至少30天);而你没有提交的内容可能很难或者不可能恢复。尽管这样,展示一下你的命令行历史记录可能会有所帮助。特别是我怀疑你之前可能已经运行了 git cherry-pick -x <commit-range> - torek
@torek更新了问题并附带了更多的引用日志。 - micadelli
1个回答

2

我认为(但无法从上述文本证明),你必须使用多个提交来启动最初的挑选操作,例如使用以下命令:

git cherry-pick -x 1234567..fedcba9   # possibly with -n too

这将会触发Git的"序列器"。如果某个单独的挑选操作失败,那么剩余的挑选操作就会留下来,并完全退出该命令。这将导致后续的git cherry-pick --abort将事情恢复到保存的ORIG_HEAD,使您在之间进行的70多次提交似乎消失了。

我可以直接检出到8369312d,它是先前的reset: moving to c398477fa2b2e0e78cb628c75df81b2c1ec411cd吗?

我相信可以。请注意,这将为您提供一个 "分离的 HEAD",这是可以的。如果git log然后显示您想要的内容,随后的git checkout -b <newbranch>将创建一个新的分支名称,您可以使用它来处理提交。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接