This will do exactly what you need, assuming your feature branch was created from master:
git checkout feature
git rebase master
# fix possible conflicts
git checkout master
git merge feature
Firstly, git rebase master
on your feature branch will get Z
and move it to a temporary space. Then bring C
, D
and E
to feature
and finally apply Z
from the temporary area on top of E
.
Resulting in A B C D E Z
At this point, if there is any conflicts you could simply run git mergetool
, solve the conflicts and then git rebase --continue
.
If you run into any issues while fixing conflicts you can cancel, run git rebase --abort
and try again.
After this, the subsequent merge
back to master will be a fast-forward
(no conflicts).
I think this is a good approach because you can keep your feature branch up-to-date with master with your new code always on top of it and you can solve any potential conflicts directly on your feature branch.
Also the merges to back master are always clean and you can create a merge commit by using git merge feature --no-ff
.
Just as a note, this approach has been working for me for a long time. I use kdiff3
as mergetool
.