Search code examples
gitmergerebase

How to repair after git rebase was interrupted


I am trying to merge branch feedback with master, using "git checkout feedback" and "git rebase master". While performing the rebase, the computer power turned off, interrupting the process. Now the git bash screen prompt contains: (feedback | REBASE 1/241). Git status command shows

$ 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 shows

$ 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 reports

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

There was a large number of modifications made to the feedback branch. A colleague recently updated the master branch with a variation of the feedback branch. What do I need to do to safely merge my version of feedback branch into master?

Following the suggestion in the comment

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

Solution

  • For the computer to lose power during a rebase is probably one of the worse things that can happen to your repo. It sounds like the database is in an inconsistent state so that the tools don't really know how to move forward or backward.

    The biggest safety net with git, in a situation like this, is the remote repo... if, that is, everything has been pushed at some point. If so, there's always the option to just nuke the local repo and clone from origin again. But if there's too much data that would be lost that way - i.e. if maybe your version of feedback has commits that weren't pushed - there are other things you can try.

    From here forward, you're counting on the fact that rebase is a non-destructive operation; it adds new objects to the database, but does not delete existing ones. (We could also say, it doesn't edit any objects, but that's redundant; git objects cannot be edited.) Of course losing power still makes it hard to be sure, but it helps to realize that mid-rebase it was probably writing loose objects rather than updating packfiles, etc. And also, I would expect it hadn't updated the feedback ref yet.

    So the next thing I would try is

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

    Now you should have your feedback branch in a new repo that shouldn't have picked up any of the corruption from the interrupted rebase operation.

    You can poke around and make sure everything's there; and once it's good then you can scrap your broken repo, re-clone from origin, add the new feedback repo as a temporary remote to the new clone, fetch the feedback branch, then get rid of the feedback repo.

    Then you're ready to re-start the rebase (maybe with a battery backup this time).

    I can't guarantee that will work, but I think it should. If it doesn't, the next idea would be to try to repair the local repo you have. I wouldn't know everything to do off the top of my head. Again, I'd aim for getting git out of the rebasing state and backing off to the pre-rebase state of the feedback branch. Since --abort isn't working, you'd have to manually update the git metadata, and that's always kind of a desperate last resort.