Search code examples
gitgit-mergegit-pull

Pull a change from remote to below a local merge


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.


Solution

  • 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