0

I'm sure this must be a dup, but I cannot find the right words for search...

On the git remote we have:

branch ABC A--B--C
branch XYZ X--Y--Z

I locally merge (yes, I should have started a new branch, but I didn't):

branch ABC A--B--C-+-ABC_XYZ
branch XYZ X--Y--Z-/

I test out locally and go to push, but while I've been testing someone updates the remote ABC:

branch ABC A--B--C--D
branch XYZ X--Y--Z

How to I get to:

branch ABC A--B--C--D-+-ABC_XYZ
branch XYZ X--Y--Z----/

with the minimal amount of pain?

I tried git pull --rebase, but it tries redoing the whole merge, which is very time-consuming and will probably reintroduce merge errors I've previously fixed. There must be a simple way to achieve this!


UPDATE: Following hints in this answer, I got this partial solution:

git rebase -p ABC --onto D
git merge ABC_XYZ

Which gives the good enough for now but not perfect:

branch ABC A--B--C--D----------+-ABCD_XYZ
                 |             |
                 +--+-ABC_XYZ--+
                    |
branch XYZ X--Y--Z--+

I would still like to know how to do it perfectly!

BTW, I have git 2.7.4, but 2.18 seems to improve this process.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • In general, you don't have much other option than to redo the entire merge, because by default git rebase won't auto merge (and even if it tries, it may not get it right). In general, you should not mix merge and rebase workflows. If you rebase, everyone should do it everywhere for that branch, and vice-versa for merging. – Tim Biegeleisen Oct 16 '19 at 06:53

1 Answers1

1

You could use the --preserve-merges (short -p) option of git rebase:

git rebase -p ABC ABC_XYZ

Or create a new branch and recreate the merge. Then clean up the old ABC_XYZ branch later at your discretion.

git checkout -b ABC_XYZ_new Z
git merge D
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • Here `ABC` is the name of the (local? remote?) branch, and `ABC_XYZ` the hash of the merged commit? – Ken Y-N Oct 16 '19 at 07:06
  • OK, I did `git rebase -p ABC --onto D` then `git merge ABC_XYZ` and it worked, although with two merge points instead of one. – Ken Y-N Oct 16 '19 at 07:20
  • I assumed `ABC_XYZ` was the local branch name. If `XYZ` was the local branch name then change the rebase command to `git rebase -p ABC XYZ`. If you want to be explicit, use `git rebase -p --onto ABC C XYZ`. – mkrieger1 Oct 16 '19 at 07:41