unity3d Using Git source control with Unity Setting up a Git repository for Unity


Example

When initializing a Git repository for Unity development, there are a couple of things that need to be done.

Unity Ignore Folders

Not everything should be versioned in the repository. You can add the template below to your .gitignore file in the root of your repository. Or alternatively, you can check the open source Unity .gitignore on GitHub and alternatively generate one using gitignore.io for unity.

# Unity Generated
[Tt]emp/
[Ll]ibrary/
[Oo]bj/

# Unity3D Generated File On Crash Reports
sysinfo.txt

# Visual Studio / MonoDevelop Generated
ExportedObj/
obj/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd

# OS Generated
desktop.ini
.DS_Store
.DS_Store?
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

To learn more about how to setup a .gitignore file, check out here.

Unity Project Settings

By default Unity projects aren't setup to support versioning correctly.

  1. (Skip this step in v4.5 and up) Enable External option in Unity → Preferences → Packages → Repository.
  2. Switch to Visible Meta Files in Edit → Project Settings → Editor → Version Control Mode.
  3. Switch to Force Text in Edit → Project Settings → Editor → Asset Serialization Mode.
  4. Save the scene and project from File menu.

Additional Configuration

One of the few major annoyances one has with using Git with Unity projects is that Git doesn't care about directories and will happily leave empty directories around after removing files from them. Unity will make *.meta files for these directories and can cause a bit of a battle between team members when Git commits keep adding and removing these meta files.

Add this Git post-merge hook to the /.git/hooks/ folder for repositories with Unity projects in them. After any Git pull/merge, it will look at what files have been removed, check if the directory it existed in is empty, and if so delete it.