TExcelTAD
Home › Unity with C# › Git LFS & Unity Version Control

Git LFS & Unity Version Control

Unity projects mix small text files (scripts, scenes, prefabs) with large binaries (textures, models, audio). Version control must handle both, and it must be set up before the first commit. Fixing a repository that already holds gigabytes of binaries in plain Git history is painful. The general ideas are in Git, Git LFS & .gitignore; this lesson is the Unity-specific setup.

Editor settings first

In Project Settings > Editor (Version Control section, defaults in new projects):

  1. Mode: Visible Meta Files, so every asset's .meta file (with its GUID) is a normal file you commit.
  2. Asset Serialization Mode: Force Text, so scenes, prefabs and ScriptableObjects are saved as YAML text that Git can diff and merge, instead of binary blobs.

.gitignore

gitignore
# Unity generated
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/
/[Mm]emoryCaptures/
/[Rr]ecordings/

# IDE files (regenerated by Unity)
.vs/
.idea/
.vscode/
*.csproj
*.sln
*.slnx
*.user
*.suo

# OS junk
.DS_Store
Thumbs.db

# Builds and crash dumps
*.apk
*.aab
*.unitypackage
sysinfo.txt
crashlytics-build.properties

Keep Assets/, Packages/ and ProjectSettings/ (including every .meta file). GitHub's maintained Unity.gitignore template is a good starting point.

.gitattributes and Git LFS

Git LFS stores large binaries outside the normal Git history and keeps only small pointer files in the repository. Install it once per machine (git lfs install), then mark binary types in .gitattributes:

gitattributes
# Unity YAML: text, LF line endings, merged by Unity's Smart Merge
*.unity       merge=unityyamlmerge eol=lf
*.prefab      merge=unityyamlmerge eol=lf
*.asset       merge=unityyamlmerge eol=lf
*.mat         merge=unityyamlmerge eol=lf
*.anim        merge=unityyamlmerge eol=lf
*.controller  merge=unityyamlmerge eol=lf
*.meta        merge=unityyamlmerge eol=lf
*.cs          text diff=csharp

# Large binaries in LFS
*.png   filter=lfs diff=lfs merge=lfs -text
*.jpg   filter=lfs diff=lfs merge=lfs -text
*.psd   filter=lfs diff=lfs merge=lfs -text
*.tga   filter=lfs diff=lfs merge=lfs -text
*.exr   filter=lfs diff=lfs merge=lfs -text
*.fbx   filter=lfs diff=lfs merge=lfs -text
*.blend filter=lfs diff=lfs merge=lfs -text
*.wav   filter=lfs diff=lfs merge=lfs -text
*.mp3   filter=lfs diff=lfs merge=lfs -text
*.ogg   filter=lfs diff=lfs merge=lfs -text
*.mp4   filter=lfs diff=lfs merge=lfs -text
*.ttf   filter=lfs diff=lfs merge=lfs -text
*.otf   filter=lfs diff=lfs merge=lfs -text
*.dll   filter=lfs diff=lfs merge=lfs -text

Unity ships UnityYAMLMerge (Smart Merge) with the editor, which understands scene and prefab structure. Register it in your Git config (the path depends on your editor install, e.g. .../Editor/Data/Tools/UnityYAMLMerge):

bash
git config merge.unityyamlmerge.name "Unity SmartMerge"
git config merge.unityyamlmerge.driver "'<path-to>/UnityYAMLMerge' merge -p %O %B %A %A"
git config merge.unityyamlmerge.recursive binary
git lfs install
git lfs track "*.psd"          # adds a line to .gitattributes, if you prefer the command

Which files does a given .gitattributes send to LFS? The model applies the last matching pattern, as Git does:

csharp · try it yourself
using System;
using System.Collections.Generic;

var rules = new List<(string Pattern, string Attrs)>
{
    ("*.unity",  "merge=unityyamlmerge"),
    ("*.prefab", "merge=unityyamlmerge"),
    ("*.meta",   "merge=unityyamlmerge"),
    ("*.png",    "filter=lfs"),
    ("*.fbx",    "filter=lfs"),
    ("*.wav",    "filter=lfs"),
    ("Assets/UI/Icons/*.png", "-filter"),     // later rule overrides: tiny icons stay in Git
};

string[] files =
{
    "Assets/Scenes/Level1.unity", "Assets/Art/rock.fbx", "Assets/Art/rock.fbx.meta",
    "Assets/UI/Icons/coin.png", "Assets/Art/cliff_albedo.png", "Assets/Audio/step.wav",
    "Assets/Scripts/Player.cs", "Assets/Art/hero.blend",
};

foreach (var f in files)
{
    string result = "plain Git";
    foreach (var (pattern, attrs) in rules)                  // last match wins
        if (Matches(pattern, f))
            result = attrs == "filter=lfs" ? "LFS" : attrs == "-filter" ? "plain Git (override)" : "Git + Smart Merge";
    Console.WriteLine(
quot;{f,-30} {result}"); } Console.WriteLine("-> hero.blend is in plain Git: add *.blend to LFS before the first commit!"); static bool Matches(string pattern, string path) { if (!pattern.Contains('/')) // no slash: match the file name anywhere return Glob(pattern, path[(path.LastIndexOf('/') + 1)..]); return Glob(pattern, path); } static bool Glob(string p, string s) // '*' matches anything except '/' { if (p.Length == 0) return s.Length == 0; if (p[0] == '*') { for (int i = 0; i <= s.Length; i++) { if (Glob(p[1..], s[i..])) return true; if (i < s.Length && s[i] == '/') break; } return false; } return s.Length > 0 && p[0] == s[0] && Glob(p[1..], s[1..]); }
Assets/Scenes/Level1.unity     Git + Smart Merge
Assets/Art/rock.fbx            LFS
Assets/Art/rock.fbx.meta       Git + Smart Merge
Assets/UI/Icons/coin.png       plain Git (override)
Assets/Art/cliff_albedo.png    LFS
Assets/Audio/step.wav          LFS
Assets/Scripts/Player.cs       plain Git
Assets/Art/hero.blend          plain Git
-> hero.blend is in plain Git: add *.blend to LFS before the first commit!

Working as a team with Git

  • Scenes and prefabs merge badly even with Smart Merge. Split work: one person per scene, use prefabs and additive scenes so people edit different files.
  • File locking: Git LFS supports git lfs lock Assets/Scenes/Level1.unity on hosts that implement it (GitHub, GitLab). Mark lockable types with lockable in .gitattributes.
  • Commit .meta files with their assets, and never commit Library/. "Missing script" or "Missing prefab" after a pull usually means someone forgot a .meta.
  • Check hosting quotas: LFS storage and bandwidth are limited and billed on most hosts.

Unity Version Control

Unity Version Control (formerly Plastic SCM) is Unity's own system, built for large binary projects and artists:

  • integrated in the editor and in Unity Hub when creating a project;
  • handles large files without LFS, with exclusive checkouts (locks) for binaries;
  • branch explorer and visual tools that non-programmers can use (Gluon mode for artists who only need some files);
  • free tier and paid seats; check current pricing.
Choose Git + LFS when Choose Unity Version Control (or Perforce) when
mostly programmers, open source, existing Git tooling and CI many artists, very large binary assets, need for locking
small to medium assets AAA-sized content

Perforce (Helix Core) remains the standard in large studios; Unity integrates with it too.

Test yourself

Which Asset Serialization Mode lets Git diff and merge scenes?

  1. Mixed
  2. Force Binary
  3. Force Text
  4. Compressed

What goes in a Unity project's .gitignore?

  1. Assets/ and ProjectSettings/
  2. Library/, Temp/, Logs/, generated .csproj/.sln files
  3. All .meta files
  4. Packages/manifest.json

Two .gitattributes lines match the same file. Which one applies?

  1. The first
  2. The last
  3. Both are merged randomly
  4. Neither