UP | HOME

Use Diff Conflict Style 3

Table of Contents

Please, just turn on diff style 3. Thank me later. I do not know the historical reason this setting is not the default, but if you ask me to help you with some crazy merge conflict, I am going to immediately ask you to configure this setting.

Context

When dealing with conflicts, merge or otherwise, one of the options for your VCS is to modify the files with "conflict markers". These are those crazy alligators that infect the code when receiving conflicts.

long fib(long n) {
<<<<<<< HEAD
                long sol = 1;
                for (; n > 0; n--) {
                                sol *= n;
                }
                return sol;
=======
                if (n <= 0) {
                                return 1;
                } else {
                                return n * fib(n - 1);
                }
>>>>>>> MERGE_HEAD
}

While this may correctly point out the differences between the two branches, this output says nothing of the shared context from whence they came.

diff3

Instead, by using diff3, the base context is provided along with the conflicting differences:

long fib(long n) {
<<<<<<< HEAD
                long sol = 1;
                for (; n > 0; n--) {
                                sol *= n;
                }
                return sol;
||||||| Parent
                return -1;
=======
                if (n <= 0) {
                                return 1;
                } else {
                                return n * fib(n - 1);
                }
>>>>>>> MERGE_HEAD
}

Now, in this contrived example, we can tell that two different developers implemented a version of Fibonacci function, replacing the stub of -1.

Notice, if they each individually arrived at the same solution, it would still result in a conflict.

Using this conflict resolution style in Git

Use the following command to use this style of conflict resolution within Git:

git config --global set merge.conflictstyle diff3

Or copy the following into your ~/.git/config:

[merge]
        conflictstyle = diff3

Zealous Diff3

Since Git Version 2.35.0, there exists a new conflict style, zdiff3 for Zealous diff3 style. This style attempts to create better/easier resolutions, which I may adopt going forward.

There are a number of blog posts that explain it, check those out to learn more.

More options to consider

Julia Evans has an excellent post summarizing popular configuration options for Git which you should check out.