11

I'm looking for a good description of what happens if one commits during rebase and how this could be 'reverted' in an easy way.

Let's consider a scenario, where a large commit is rebased. During rebase a conflict appears and user begins merging changes. Now, imagine a scenario where you were almost done, but you didn't call git rebase --continue - for whatever reason (be it long weekend or such). The next week you just resumed working, stil during rebase. Finally, you call git commit --amend to append the changes to the last commit and... they end up in the commit you were rebasing into.

Naturally, you can always checkout the commit you started rebasing from and "hack your way through" - say, for example, by trying to copy all the files from your amend, but that may drop the changes that were introduced in the meantime.

Is there a clean, good way to fix this? This is one particular state I should be careful about and I never want to end up in it, but it still happens occasionally - and I end up spending one whole day trying to get things straight.

I would really appreciate all help and suggestions. Thank you!

5
  • 1
    Not sure it'll work in every case like that, but have you tried rebasing your final result (with amended base commit) onto the original base commit? I tried to model the same situation and it worked. This another rebase introduces a new conflict, but if you resolve it and do git rebase --continue, then you end up with two commit: original base and your rebased changes. They both have the same commit message (as the original base commit), but it's easy to fix with a git --amend.
    – KL-7
    Jun 20, 2012 at 9:41
  • i'll have a look, that sounds quite sane. thanks!
    – Tomasz W
    Jun 20, 2012 at 9:49
  • 1
    Use git_ps1 to put the repository status into your prompt. You'll always notice you have an unfinished rebase.
    – camh
    Jun 20, 2012 at 10:28
  • it didn't quite work for me, i'm afraid. it turned out that it's not really different from reverting the commit and going through rebase one more time; even better, once the rebase is complete with the unfortunate commit, it's best to switch to the rebase head, start off new branch and merge changes with --no-commit --no-ff --strategy=theirs and (in case of gerrit) copy changeid.
    – Tomasz W
    Aug 2, 2012 at 11:50
  • This worked for me. Not "happy path", but easy to understand what's going on: 1) copy the entire workspace to some folder outside version control. 2) abort the rebase and delete the local branch. 3) fetch and pull from the remote branch 4) start the rebase over. For any conflicts, copy over the version of the file from step 1.
    – Max Heiber
    Mar 31, 2016 at 19:50

1 Answer 1

5

There are two proposed solutions to this situation.

  • The first solution is to rebase final result back onto original base commit. this would require you to resolve similar merge conflicts once again, but when you're done your commit should be back on track.

  • Alternative solution, that worked for me, was to branch off the same point as the commit you ammended to (it carries the SHA, which should be used as a checkout base). Then create a new branch and invoke git merge --no-ff --no-commit --strategy=theirs other_branch, where *other_branch* is the one with unfortunate commit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.