Skip to content

Instantly share code, notes, and snippets.

@djcsdy
Last active March 26, 2024 20:04
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save djcsdy/1f9cc264dc56c16bf2d9d228a1db78a5 to your computer and use it in GitHub Desktop.
Save djcsdy/1f9cc264dc56c16bf2d9d228a1db78a5 to your computer and use it in GitHub Desktop.
Unity with source control

In the Unity editor:

  1. Edit -> Project Settings -> Editor
  2. In the inspector panel, set:
  3. Version Control -> Mode: Visible Meta Files
  4. Asset Serialization -> Mode: Force Text

“Visible Meta Files” causes Unity to place .meta files next to each of your assets. It’s important to check these files into version control because they contain the settings associated with those assets that you set in the Unity editor.

“Asset Serialization: Force Text” causes Unity to write its .meta and other files in a more-or-less human-readable text format, which makes it a lot easier to understand what has changed when you look at version control logs. Also it’s feasible to merge these text files by hand, whereas it’s not really possible to do that with Unity’s default binary file format.

Create a .gitignore file in the root of your project with the following contents:

/Temp
/Library
/Obj
/UnityGenerated
/Build
/Builds
/*.sln
/Assembly-CSharp.csproj
*.suo
*.user
*.userprefs

Most of the stuff in the above ignore file is stuff generated by Unity when you open or build your project, so you don’t need to check that stuff in. Usually in a .NET project you would check in the *.sln and *.csproj files, but in a Unity project Unity will generate those so it’s best not to check them in.

*.suo files are generated by Visual Studio when you open your project, you don’t need to check that in. (MonoDevelop probably also generates these files but I’m not sure).

*.user and *.userprefs contains Visual Studio settings that are specific to an individual PC, you shouldn’t check these in. (MonoDevelop probably also generates these files but I’m not sure).

If you use Blender, add the following to the .gitignore also:

*.blend1
*.blend1.meta

Create a .gitattributes file in the root of your project with the following contents:

*.sln text eol=crlf
*.csproj text eol=crlf
*.anim text eol=lf
*.asmdef text eol=lf
*.asset text eol=lf
*.controller text eol=lf
*.mat text eol=lf
*.meta text eol=lf
*.overrideController text eol=lf
*.prefab text eol=lf
*.preset text eol=lf
*.spriteatlas text eol=lf
*.unity text eol=lf
*.mixer text eol=lf
*.renderTexture text eol=lf
*.json text eol=lf
*.xml text eol=lf

This ensures that .NET project files are stored with CRLF line endings, which is required by the .NET spec. It also ensures that Unity *.asset files are stored with LF line endings. Unity always uses LF line endings for these files regardless of platform.

The .gitattributes file is not mandatory, and nothing will break if you don’t include it. However, if you don’t, then depending on your platform these files may show as having changes whenever Unity rewrites the file, even if the contents of the file are the same, which can be very annoying.

@xmedeko
Copy link

xmedeko commented Mar 14, 2019

I've just checked Unity 2018.3 fresh project (without git control) and some *.asset files are with CRLF others with LF. Seems Unity is inconsistent itself.
On the other hand, seems to me that *.meta files are with LF.

@CovorSorin
Copy link

Same as @xmedeko. The *.asset files have CRLF and LF line endings. Very frustrating.

@Last8Exile
Copy link

It is qute a necro post, but in case somebody encounter this.

Unity always use LF for assets. But Unity change line ending only during reserialisation. If asset is not marked as dirty - it won't be reserialized and saved. So CRLF files are probably from repo or other sources and unity does not "touched" this files yet.

.gitattributes i use for unity projects

# * text=auto

# Unity files
*.anim                  text eol=lf
*.asmdef                text eol=lf
*.asset                 text eol=lf
*.controller            text eol=lf
*.mat                   text eol=lf
*.meta                  text eol=lf
*.overrideController    text eol=lf
*.prefab                text eol=lf
*.preset                text eol=lf
*.spriteatlas           text eol=lf
*.unity                 text eol=lf
*.mixer                 text eol=lf
*.renderTexture         text eol=lf
*.json                  text eol=lf
*.xml                   text eol=lf

# Code files
*.sln                   text eol=crlf
*.csproj                text eol=crlf
*.cs                    text eol=crlf
*.shader                text eol=crlf

@djcsdy
Copy link
Author

djcsdy commented Nov 15, 2022

I've updated this with most of the file types from @Last8Exile, thanks :-).

@Menotdan
Copy link

Yo thanks so much @Last8Exile
I havent tested it yet but this is a much welcome necropost

@Michaelwolf95
Copy link

@Last8Exile Thank you! Trying this out now.
Is the line * text=auto intentionally commented out here?

# * text=auto

@Last8Exile
Copy link

@Michaelwolf95 Actually i don't know. I found initial information with some file extensions somewhere and then filled it with the rest along the way. This line probably leaved to show what the default value was. I never tryed uncommenting this line.

@Michaelwolf95
Copy link

@Last8Exile Ok, well, it's working for me as-is. 👍

@9-winter
Copy link

@djcsdy Thank you so much ❤️

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