Skip to content

Instantly share code, notes, and snippets.

@miredirex
Created September 1, 2023 22:53
Show Gist options
  • Select an option

  • Save miredirex/f51c7e2eaa938656034c1bbe57da92f0 to your computer and use it in GitHub Desktop.

Select an option

Save miredirex/f51c7e2eaa938656034c1bbe57da92f0 to your computer and use it in GitHub Desktop.
Unity's Smart Merge tool with Git for collaborative development

 Configure Unity's Smart Merge tool with Git for collaborative development

...without hardcoding the Editor path anywhere
Windows only

1. Create .gitconfig file in the root of your project's directory

touch .gitconfig

2. About the .gitconfig file

Since this .gitconfig you've just created is not automatically seen and used by Git, we need to tell Git to include it (for the current repository only):

git config --local include.path ../.gitconfig

Important
(Unfortunately) all collaborators in your repository have to run this command as well. I recommend adding the note to the README.md.

3. Create the Bash script

File run_unity_merge_tool.sh:

BASE=$1
REMOTE=$2
LOCAL=$3
MERGED=$4
# Find UnityYAMLMerge.exe tool automatically
find /c/Program\ Files/Unity/ -name 'UnityYAMLMerge.exe' -exec '{}' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED" ';'

Or the .bat equivalent (run_unity_merge_tool.bat):

@echo off

rem Find UnityYAMLMerge.exe tool automatically
for /R "C:\Program Files\Unity" %%f in (UnityYAMLMerge.exe) do if exist "%%f" set "unityyamlmerge=%%f"

set BASE=%1
set REMOTE=%2
set LOCAL=%3
set MERGED=%4
"%unityyamlmerge%" merge -p "%BASE%" "%REMOTE%" "%LOCAL%" "%MERGED%"

4. Update .gitconfig:

[merge]
	tool = unityyamlmerge
[mergetool "unityyamlmerge"]
	trustExitCode = false
	cmd = ./run_unity_merge_tool.sh "$BASE" "$REMOTE" "$LOCAL" "$MERGED"

If you decided to use the .bat version, simply change to ./run_unity_merge_tool.bat

5. Using it

When you encounter merge conflicts in YAML assets (scenes, prefabs, materials etc.), run:

git mergetool

And it should try to automatically resolve the conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment