0

I have the following scenario that is blocking me to merge my branch back to master:

  • feature branch created from master
  • done with the feature branch and it's time to merge it back to master
  • the merge back to master has conflicts
  • fixed all conflicts and pushed to gerrit
  • in the meantime master is still moving and new code on master conflicted with my merge commit

The question is how can I update my merge commit with latest master, fix conflicts and amend to my commit on gerrit?

Tried the following and failed:

  • merge master back into that commit locally but that would create two merge commits which is bad
  • can't rebase since you can't rebase merges
uncletall
  • 6,609
  • 1
  • 27
  • 52
Karim Fikani
  • 356
  • 3
  • 23
  • Rebase only the "meaningful" revisions, without the merges. Say your branch is called X..... last revision of X is the merge with master..... but the _real_ work of X is 4 revisions (not counting the merge revision)... so it's X~5..X~1. So.... `git checkout --detach master; git cherry-pick X~5..X~1`. Make sure everything is fine and move the pointers around: `git branch -f X; git push whatever-remote -f X`. This way you are updating.... but without merging.... you could also try squash/rebase in a single shot: https://stackoverflow.com/questions/54814888/how-to-squash-rebase-in-a-single-shot – eftshift0 Mar 26 '19 at 21:28
  • @eftshift0 I did try the merge --squash and didn't fix my 'cannot merge' on gerrit. As for your first suggestion I'm still wrapping my head around it. Thx! – Karim Fikani Mar 27 '19 at 20:54

3 Answers3

0

I would really just use git pull master and then resolve conflicts manually editing necessary stuff. After that usual git add -A and git commit -m "resolved conflicts" .Then you merge it back to master. Also, you usually don't rebase on public branches.

noname
  • 227
  • 2
  • 7
  • Yes that would pull the latest on top but that would create new commits on top my merge which is bad and I won't be able to push it, gerrit wouldn't allow me. Let's say from the time I did the merge and fixed all the conflicts master got 10 new commits, so when I 'git pull master' all these 10 commits would be on top of my merge. Ideally I want to squash them into my merge. – Karim Fikani Mar 26 '19 at 23:44
0

The best way would be to merge the master to your feature branch, then push this merge to your feature branch and repeat until you have no more incoming conflicts.

Steps:

  • Merge master to feature branch
  • Resolve conflicts
  • Push for review to feature branch
  • If more incoming conflicts, repeat
  • If no incoming conflicts, merge to master and push to master for review

This way you will make sure that your hard work of resolving the merge conflicts will get smaller each time you do it. The first time it will be a lot of changes, maybe months of work. The next time it may be just one or two changesets and the faster your merges get, the less time it takes and this will result in less conflicts


Alternatively you can create a second merge commit.

  • Checkout master
  • Merge feature to master
  • Push for review
  • New conflicts are created
  • Checkout latest master again
  • Merge your merge commit into the master
  • Push for review
  • Now submit both changes together

This will work nicely if you submit the changes together from the second merge

uncletall
  • 6,609
  • 1
  • 27
  • 52
  • True and I thought about that. The only thing is that that branch is a release branch so code is frozen on that branch. Easiest to make a copy of that release branch and do what you suggested. I just wanted to see if there are easier ways with git to do what I wanted to do but it doesn't seem like it. Thx! – Karim Fikani Mar 27 '19 at 20:53
  • I have elaborated my answer, you will have multiple changeset but this works fine – uncletall Mar 28 '19 at 06:38
0

In gerrit you do the following to resolve this: ( in headless state)

  • git fetch
  • checkout origin/master
  • git merge origin/your/branch
  • resolve conflicts
  • finish merge
  • update new merge commit with your previous "outdated merge" change-id
  • git review master

This will result in your merge CL to have the updated merge as the latest PatchSet

GreatWiz
  • 735
  • 4
  • 8