Lesson 107 +10 XP

Version Control and Smart Merge

Version Control and Smart Merge

Unity projects require specific setup to prevent binary conflicts and track project modifications cleanly in Git or other version control tools.

Required Settings for Version Control

Ensure Unity saves all asset files in a human-readable text format for readable diffs:

  1. Open Edit > Project Settings > Editor.
  2. Set Asset Serialization Mode to Force Text.
  3. Set Version Control Mode to Visible Meta Files.

The Crucial Role of Meta Files

Every file in your Assets folder has a corresponding .meta file. The meta file stores:

  • A unique Global Identifier (GUID) that Unity uses to resolve references between assets.
  • Import settings specific to that asset.

Important Rule: Always commit .meta files alongside their assets. Deleting or ignoring them breaks references, resulting in missing scripts and missing textures.

Essential .gitignore Rules

Do not track volatile generated files. A typical Unity .gitignore includes:

/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/
*.csproj
*.sln

UnityYAMLMerge (Smart Merge)

Unity contains a tool called UnityYAMLMerge that automatically resolves merge conflicts in scene (.unity) and prefab (.prefab) files.

Configure your Git configuration file (.gitconfig) to run it:

[merge]
    tool = unityyamlmerge

[mergetool "unityyamlmerge"]
    trustExitCode = false
    cmd = 'C:/Program Files/Unity/Hub/Editor/<version>/Editor/Data/Tools/UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"

TL;DR

  • Set Asset Serialization to Force Text for readable text diffs.
  • Always commit .meta files alongside their assets to preserve GUIDs.
  • Ignore the Library, Temp, and Obj folders in your version control repository.
  • Use UnityYAMLMerge to resolve scene and prefab merge conflicts.