11

I have a relatively big feature branch that I want to merge with master, and the merge produces quite a lot of conflicts. Is there a way to merge those branches one commit at a time so I could resolve one conflict at a time and check if everything's working?

One solution I can think of is to use rebase instead of merge, but then the history will become flat and I don't want that. I'm ok with creating additional "merge fix" commits, though, if it helps.

Dunno
  • 3,632
  • 3
  • 28
  • 43
  • 3
    You might consider git-imerge. See https://stackoverflow.com/questions/18162930/how-can-i-find-out-which-git-commits-cause-conflicts and http://stackoverflow.com/questions/21875061/is-it-better-to-have-as-many-commits-as-possible-in-git-or-as-few-as-possible – torek Sep 26 '16 at 09:45
  • @torek thanks, that looks like exactly what I need – Dunno Sep 26 '16 at 09:50
  • For what it's worth, I should add that I once tried to use imerge on a massive code base and it was just too slow. While burning several days on this we also tried an alternate approach and finished that much earlier. I had a third alternative in mind (manual merges at chosen points) as another strategy but never had time to try it. The alternative that did work was basically an en-masse "re-do what we did, starting from new code base", i.e., the equivalent of roughly 400 cherry-pick operations. – torek Sep 26 '16 at 09:58

2 Answers2

6

You could merge "one commit at a time", but it would create a merge commit for each commit you have on the other branch, which is quite heavy.

To do that, just look at where you diverged from master, and while on the master branch, issue git merge <commit_id> for each commit starting from that.

Otherwise, just resolve the big conflicts once and for all, and everyone reading your commit history will thank you.

A good way to address this problem in the feature would be the regulary merge master in your feature branch.

blue112
  • 52,634
  • 3
  • 45
  • 54
  • 1
    Would squashing all the merge commits work, or will that break something? – Dunno Sep 26 '16 at 09:47
  • I wouldn't recommend squashing merge commits, since it will make the history weird, having octopuses merges everywhere. – blue112 Sep 26 '16 at 09:48
  • I think this is an ideal place to squash, as your combining the various stages of the merge into one commit. – tpower Nov 20 '18 at 09:27
6

Instead of rebasing the big feature branch, you can instead rebase a copy of that. With all the conflicts resolved after the rebase you can then pull back the difference:

$ git checkout -b big_feature_branch.rebased big_feature_branch
$ git rebase master big_feature_branch.rebased
# Resolve all conflicts
$ git checkout big_feature_branch
$ git diff big_feature_branch big_feature_branch.rebased | git apply -
$ git commit -am "merge fix"
$ git branch -D big_feature_branch.rebased
KyotoFox
  • 40
  • 5
hlovdal
  • 26,565
  • 10
  • 94
  • 165