Unity 5 comes with a built-in tool that handles prefab and scene merge conflicts. The documentation on how to set it up is scarce and confusing (specially for first time git users). This is a guide on how to set up the tool with Git.
Note: you can use this with any Git client, as far as I know (ex: GitHub for Windows).
-
Download a fallback merge tool of your prefference (some fallback tools are listed in the mergespecfile.txt, mentioned bellow). I personally recommend Diff Merge.
The fallback tool is only used for conflicts that need human input and cannot otherwise be resolved, so you can always just resolve conflicts in the old fashioned way, if ever need be, by using the code editor of your choice! No need to install anything else! -
Go to the Unity installation folder in Editor/Data/Tools and find the mergespecfile.txt file, the path may vary according to your OS, on Windows it's normally in
C:\Program Files\Unity\Editor\Data\Tools\mergespecfile.txt
orC:\Program Files (x86)\Unity\Editor\Data\Tools\mergespecfile.txt
.
In OSX you can find it in/Applications/Unity/Unity.app/Contents/Tools/
-
This file tells you how to set up the merge tool for various third party applications. You can notice how the only uncommented lines are
unity use etcetcetc
andprefab use etcetcetc
, these lines indicate the calls to the fallback tool to solve scene (unity use) and prefab (prefab use) conflicts when the tool can't. If we had Perforce installed, for example, all we had to do is replace these 2 lines with:
unity use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
prefab use "C:\Program Files\Perforce\p4merge.exe" "$BASE" "$LOCAL" "$REMOTE" "$MERGED"
After that, save and close.
If you choose to not use any of those tools, leave it empty, insert your editor's path and/or search how to integrate it with a diff tool. However, for some programs (like DiffMerge) there's no need to insert the path, it'll automatically work. If you don't want to use any tool Leaving it empty also works.
Note: these next steps need to be done whenever you 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\\Editor\\Data\\Tools\\UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"
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.
Whenever you're merging or rebasing your project and a conflict appears, instead of manually fixing it you open Git Shell/Bash and type the following command: git mergetool
.
The tool will then resolve those conflicts for you automatically.
If there is a conflict that needs human input, the fallback tool (i.e: Preforce) will run and show the conflicts that need resolving (don't forget to save your changes and then close). If you don't have a fallback tool, you'll just get a message stating this, but you'll still be able to resolve the conflitcs in your editor.
You will need then to run the command git add --all
or git add .
in order to save the changes made, and then git rebase --continue
when rebasing or git merge --continue
when merging.
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?
Feel free to contact me @anacatte if you have any questions or just wanna chat!