Git conflicts with JSON files Git conflicts with JSON files json json

Git conflicts with JSON files


we get lots of conflicts even though people are changing different lines

This shouldn't be the case, you only get conflicts if same line is modified by different people, committed and then later merged.

Oh, I actually tried this out and encountered some odd problems.

Commit 1 (master):

{    "a": "1",    "b": "2",    "c": "3",    "d": "4",    "e": "5",    "f": "6",    "g": "7"}

Commit 2 (tmp)

{    "A": "1",    "B": "2",    "C": "3",    "d": "4",    "e": "5",    "f": "6",    "g": "7"}

Commit 3 (master):

{    "a": "1",    "b": "2",    "c": "3",    "d": "4",    "E": "5",    "F": "6",    "G": "7"}

git merge tmp: correct result

{    "A": "1",    "B": "2",    "C": "3",    "d": "4",    "E": "5",    "F": "6",    "G": "7"}

However I get conflicts if also row "d" was modified, maybe git wasn't able to establish diff boundaries. My stupid suggestion to avoid this stupid git behavior is to add "padding" to the JSON file (ugly, isn't it? But no more conflicts):

{    "a": "1",    "b": "2",    "c": "3",    "d": "4",    "e": "5",    "f": "6",    "g": "7"}


One thing I would do in such a scenario would be to maintain the configurations in a database table instead of a JSON file - if they change all that frequently. As others have already pointed out, there is not much you can do to avoid conflicts if you have that high number of changes happening to the config all the time. Your example anyway looks more like a mapping between word in English and some other language, so a three column table should suffice.

The JSON file, if needed could be generated either on the fly every time or generated once during deployment for each server from the database table.


Another reason, why you see so many conflicts could be that your developers are using different line ending configurations. See How to change line-ending settings in Git. In order to find out, you can open a json file with a Hex editor and see if all line endings are consistent across the whole file.