git rebase 中断后如何修复

Posted

技术标签:

【中文标题】git rebase 中断后如何修复【英文标题】:How to repair after git rebase was interrupted 【发布时间】:2018-09-27 16:10:38 【问题描述】:

我正在尝试使用“git checkout feedback”和“git rebase master”将分支反馈与 master 合并。在执行 rebase 时,计算机电源关闭,中断了进程。现在 git bash 屏幕提示包含:(反馈 | REBASE 1/241)。 Git状态命令显示

$ git status
On branch feedback
Your branch is up-to-date with 'origin/feedback'.
You are currently rebasing branch 'feedback' on '7a20ac7'.
  (all conflicts fixed: run "git rebase --continue")

nothing to commit, working tree clean

git rebase --continue 显示

$ git rebase --continue
Applying: Not clear why feedback doesn't run now
No changes - did you forget to use 'git add'?
If there is nothing left to stage, chances are that something else
already introduced the same changes; you might want to skip this patch.

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

git reflog 报告

4bae8c8 HEAD@0: commit (merge): Merge branch 'master' into feedback
eca14e3 HEAD@1: checkout: moving from 7a20ac7e86823915a4bce205a4baeeff7a7acb7a to feedback
7a20ac7 HEAD@2: rebase: checkout master
eca14e3 HEAD@3: checkout: moving from 7a20ac7e86823915a4bce205a4baeeff7a7acb7a to feedback
7a20ac7 HEAD@4: rebase: updating HEAD
eca14e3 HEAD@5: rebase: checkout feedback
7a20ac7 HEAD@6: rebase: checkout master
eca14e3 HEAD@7: commit: trying to scan a matrix <- last change on branch feedback

对反馈分支进行了大量修改。一位同事最近用反馈分支的变体更新了主分支。我需要做什么才能将我的反馈分支版本安全地合并到 master 中?

遵循评论中的建议

john@LAPTOP-CBKOSEPA MINGW64 ~/OneDrive/Documents/GitHub/crNn (feedback|REBASE 1/241)
$ git rebase --abort

john@LAPTOP-CBKOSEPA MINGW64 ~/OneDrive/Documents/GitHub/crNn (feedback)
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: Not clear why feedback doesn't run now
Using index info to reconstruct a base tree...
M       src/rnn/rnn.py
Falling back to patching base and 3-way merge...
error: inflate: data stream error (unknown compression method)
error: unable to unpack c8d57fe6a41234079ebe597c88f33e54b3306a14 header
error: inflate: data stream error (unknown compression method)
fatal: loose object c8d57fe6a41234079ebe597c88f33e54b3306a14 (stored in .git/objects/c8/d57fe6a41234079ebe597c88f33e54b3306a14) is corrupt

【问题讨论】:

尝试git rebase --abort,然后再次运行rebase 之前,我已经完成了以下顺序:“git rebase --abort”,“git checkout feedback”,“git rebase master”,并修复了识别的单个合并文件,并进行了提交/推(从原子内部)。上面的输出是电流。我输入了“git rebase --abort”,它返回到(反馈)提示。这应该跟“git rebase master”吗?我添加了上面的命令/输出。 【参考方案1】:

在 rebase 期间计算机断电可能是您的 repo 可能发生的最糟糕的事情之一。听起来数据库处于不一致的状态,因此工具并不真正知道如何前进或后退。

在这种情况下,git 最大的安全网是远程仓库……也就是说,如果所有内容都在某个时候被推送。如果是这样,则始终可以选择仅对本地存储库进行核攻击并再次从源克隆。但是,如果有太多数据会以这种方式丢失 - 即,如果您的 feedback 版本可能有未推送的提交 - 您可以尝试其他方法。

从这里开始,您指望rebase 是非破坏性操作;它将新对象添加到数据库中,但不会删除现有对象。 (我们也可以说,它不会编辑任何对象,但这是多余的;无法编辑 git 对象。)当然,失去电源仍然很难确定 ,但它有助于意识到中期变基它可能正在编写松散的对象而不是更新包文件等。而且,我希望它还没有更新 feedback 参考。

所以我接下来要尝试的是

cd ..
git clone --single-branch --branch feedback file://localhost/path/to/broken/repo feedback

现在您应该将您的 feedback 分支放在一个新的 repo 中,该分支不应该从中断的 rebase 操作中提取任何损坏。

您可以四处寻找并确保一切都在那里;一旦它好了,你就可以废弃你损坏的仓库,从原点重新克隆,添加新的feedback repo作为新克隆的临时远程,获取feedback分支,然后摆脱feedback repo .

然后您就可以重新启动rebase(这次可能需要备用电池)。

我不能保证这会奏效,但我认为应该会。如果没有,下一个想法是尝试修复您拥有的本地存储库。我不知道该怎么做。同样,我的目标是让 git 脱离变基状态并退回到 feedback 分支的变基前状态。由于--abort 不起作用,您必须手动更新 git 元数据,这总是一种绝望的最后手段。

【讨论】:

【参考方案2】:

如果--abort 不起作用,您可以使用git rebase --quit 告诉git 它不再在rebase 中而不进行任何其他更改,然后使用reflog、git checkoutgit reset --hard origin/master 等进行清理。

【讨论】:

以上是关于git rebase 中断后如何修复的主要内容,如果未能解决你的问题,请参考以下文章

如何修复 @fontface 与默认字体大小 - 如果 @fontface 未加载,布局会中断

修复 git HTTPS 错误:macOS 12 上的“错误密钥长度”

Git rebase使用

为啥总是网络连接中断?

git merge和rebase的区别

图解 Git 基本命令 merge 和 rebase