Batch Resolving Merge Conflicts

Sometimes when you run git merge you will be confronted with a huge load of merge conflicts. However, if you are lucky there might be a clear rule which you can apply to each of those conflicts, either

  • accept current change (change on current branch or ours) or
  • accept incoming change (incoming change from other branch or theirs).

In this case you can save yourself a lot of time and effort by specifying a particular merge strategy option.

Setup

Suppose that you have two branches: main and feature. You are currently on the feature branch.

git checkout feature

Just merge in the changes from main.

git merge main

😱 You get an insane load of merge conflicts. Nobody has the time or patience to wade through all of those. Luckily for you though there’s a clear rule that you can apply and that will save you a lot of time.

First let’s abort the current merge.

git merge abort

Merge Strategies

Git offers a few different merge strategies:

  • ort (the default)
  • recursive
  • resolve
  • octopus
  • ours and
  • subtree.

We’ll be using the default ort strategy.

Merge Strategy Options

The ort strategy supports a number of options. The -X or --strategy-option arguments are used to specify a merge strategy option. The relevant options are:

  • ours and
  • theirs.

🚨 The ours strategy option should not be confused with the ours strategy.

Accept All Current Changes

If you want to select changes from the current branch, feature, over those from the other (incoming) branch, main, then use the ours option:

git merge -X ours main

Whenever there is a conflict between changes in the feature branch and in the main branch, the changes from the feature branch will be preferred.

Accept All Incoming Changes

If you want to select changes from the other (incoming) branch, main, over those from the current branch, feature, then use the theirs option:

git merge -X theirs main

Whenever there is a conflict between changes in the feature branch and in the main branch, the changes from the main branch will be preferred.

Conclusion

Using a merge strategy option can save you a lot of time and frustration. Rather than wading through a swamp of merge conflicts, if there’s a clear rule which can be applied (accept all current changes or accept all incoming changes) then a merge strategy option can resolve all conflicts automatically.