Merge conflicts - GameLabGraz/Maroon GitHub Wiki


General project configuration (already configured)

.meta files must be visible to avoid broken object references:

Project Settings > Version Control > Mode: “Visible Meta Files”

Plain text serialization must be active to avoid unresolvable merge conflicts:

Project Settings > Editor > Asset Serialization > Mode: “Force Text”

Git does not understand the semantics of scenes and prefab files, therefore it must be configured to not automatically merge these files and treat them like binary files in the .gitattributes file:

*.unity binary
*.prefab binary
*.asset binary

Merge process

Here are the steps of a merge process for scenes in order to understand why the config is needed:

  1. Git merge is initiated from the command line or graphical application
  2. A merge conflict occurs
  3. Developer starts Unity yaml merge tool, the tool structures files for merge
  4. Unity yaml merge tool automatically opens another merge tool if it cannot resolve all conflicts
  5. Developer resolves conflicts and saves merged file
  6. Results can be inspected and the merge can be completed

Local Configuration (manual configuration for each installation of Unity)

Config for initiation from command line (for step 3)

In the file .git/config it can (optionally, recommended) be configured that the merge tool does not store backup files because there can be many of them:

[mergetool]
    keepBackup = false

For being able to merge with git from the command line the Unity yaml merge tool needs to be defined in the .git/config file:

[mergetool "unityyamlmerge"]
    trustExitCode = false
    cmd = "C:\<path-to-unity-install-folder>\Editor\Data\Tools\UnityYAMLMerge.exe" merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"

Config for initiation from tortoise git (for steps 1-3)

In TortoiseGit merge tools can be defined per file type, under TortoiseGit Settings > Diff Viewer > Merge Tool > Advanced one can Add... tools for the file type .unity, .prefab and .asset. In the program field the path to the Unity yaml merge tool has to be added:

C:\<path-to-unity-install-folder>\Editor\Data\Tools\UnityYAMLMerge.exe merge -p %base %theirs %mine %merged

Config for merge tool (for step 4)

The above configuration does not work if the Unity yaml merge tool does not succeed in automatically merging the files, which seems to happen often. Therefore it needs to be configured to open another merge tool the developer can use to manually review the changes. It is possible to add any tool that supports three way merges (base, local, upstream) with a separate output file, here is the configuration for TortoiseGit:

In the file C:\<path-to-unity-install-folder>\Editor\Data\Tools\mergespecfile.txt the following lines can be found:

#
# Default fallbacks for unknown files. First tool found is used.
#

Directly underneath them the merge tool to open can be specified:

# Tortoise Git Merge
* use "%programs%\TortoiseGit\bin\TortoiseGitMerge.exe" -base:"%b" -mine:"%l" -theirs:"%r" -merged:"%d"

Handling a merge conflict

  1. Git merge is initiated from the command line or graphical application:
    git merge develop or start merge with TortoiseGit and jump to step 4
  2. A merge conflict occurs
  3. Developer starts Unity yaml merge tool, the tool structures files for merge:
    git mergetool --tool=unityyamlmerge
  4. Unity yaml merge tool automatically opens another merge tool if it cannot resolve all conflicts
  5. Developer resolves conflicts and saves merged file:
    Choose what version to use, save and close the merge tool
  6. Results can be inspected and the merge can be completed:
    Check if Unity scene/prefab works, resolve the merge by accepting the new file, commit the changes.