Unity comes with a built-in tool that handles prefab and scene merge conflicts. This is a guide on how to set up and use the tool with Sourcetree and Git.
Download Perforce's Helix Visual Merge Tool and install it. Unity merge tool will use it automatically.
Note: these next steps need to be done whenever you check out a repository containing a Unity project or make a new Unity project.
-
Go to the project folder and make sure you can see the hidden files and folders, go to the .git folder and open the config file with a text editor of your choice.
-
Add the following lines to the file and save it:
[merge]
tool = unityyamlmerge
[mergetool "unityyamlmerge"]
trustExitCode = false
cmd = 'C:\\Program Files\\Unity\\Hub\\Editor\\UNITY_VERSION\\Data\\Tools\\UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"
Note: replace UNITY_VERSION with the Unity version that your project is currently on.
Note: these next steps need to be done whenever you make a new Unity project.
-
Open the project in Unity, go to the top bar and select Edit and go to Project Settings>Editor;
-
Change the Version Control mode to Visible Meta Files and the Asset Serialization mode to Force Text, like shown in the picture.
-
If the project is already in git, commit ProjectSettings/EditorSettings.asset file.
-
Whenever you're merging or rebasing your project and a conflict appears, right click the file that needs resolving, select "Resolve Conflicts", then select "Launch External Merge Tool". The tool will then resolve those conflicts for you automatically.
If there is a conflict that needs human input, Perforce Merge will run and show the conflicts that need resolving (don't forget to save your changes and then close). When merging using the default settings, P4Merge's left panel will show local file ("mine"), right panel will show the incoming remote version ("theirs"), and the middle panel will show the common base version. -
After merging, open the project in Unity to ensure everything has been merged properly.
-
After checking the merge results, switch back to Sourcetree, and remove all the backup files that was created by merge tool, along with their metafiles.
Unity Manual - Smart Merge
Reddit - How to solve scene conflicts with Unity's Smart Merge (5.1 fixes)
How to use Git for Unity source control?