What are best practices for using Git source control with Unity 3D, particularly in dealing with the binary nature of Unity 3D projects? Please describe the workflow, what paths would be included in .gitignore, what settings should be set in Unity and/or the project, and any other special things that should be noted.
Note: I realize that using the Asset Server is the Unity-recommended way, but I would like to use Git for a variety of reasons. Please no answers that state or argue that I should just use the Asset Server. The Asset Server really isn't an option for me.
I thought that I might post a simpler
.gitignore
for anyone that is interested:The main things to remember when using git for unity-3d source code version control:
(A) DO NOT check-in the Library folder. I have made this mistake multiple times in past and have suffered for it! Delete OR move out library folder before adding your project / files into git.
(B) Use "Visible Meta Files" - for newest unity versions - 5.3.4 and above this happens by default. For some of the earlier versions you need to change the settings under: Edit-> Project Settings-> Version Control
(C) Use a .gitignore file for Unity- to make sure sanity is maintained and files are not unnecessarily added- if on android / tizen - add rules to exclude APK and TPK files from being added to repository. Google around for a .gitignore file for unity OR else use this model .gitignore for Unity provided by GitHub: https://github.com/github/gitignore/blob/master/Unity.gitignore
(D) Make sure the .gitignore file is added to the repository as the first file added - because in past I personally have missed adding .gitignore file. Have many thoughts in hindsight on why this happened- but nowadays I just copy and add .gitignore file as first step of setting up repository.
So... to make a Unity project ready for git, do the following:
(1) Go to project folder
(2) Type git init .
(3) Copy the .gitignore file: On MacOS: cp ~/Downloads/.gitignore On Windows: copy c:\Users[yourusername]\Downloads.gitignore .
(4) git add .gitignore
(5) git add *
Hope this helps... all the best!
Only the Assets and ProjectSettings folders need to be under git version control.
You can make a gitignore like this.
Edit -> Project Settings -> Editor
Set Version Control to meta files. Set Asset Serialization to force text.
I think this is what you want.
The following is an excerpt from my personal blog .
Using Git with 3D Games
Update Oct 2015: GitHub has since released a plugin for Git called Git LFS that directly deals with the below problem. You can now easily and efficiently version large binary files!
Git can work fine with 3D games out of the box. However the main caveat here is that versioning large (>5 MB) media files can be a problem over the long term as your commit history bloats. We have solved this potential issue in our projects by only versioning the binary asset when it is considered final. Our 3D artists use Dropbox to work on WIP assets, both for the reason above and because it's much faster and simpler (not many artists will actively want to use Git!).
Git Workflow
Your Git workflow is very much something you need to decide for yourself given your own experiences as a team and how you work together. However. I would strongly recommend the appropriately named Git Flow methodology as described by the original author here.
I won't go into too much depth here on how the methodology works as the author describes it perfectly and in quite few words too so it's easy to get through. I have been using with my team for awhile now, and it's the best workflow we've tried so far.
Git GUI Client Application
This is really a personal preference here as there are quite a few options in terms of Git GUI or whether to use a GUI at all. But I would like to suggest the free SourceTree application as it plugs in perfectly with the Git Flow extension. Read the SourceTree tutorial here on implementing the Git Flow methodology in their application.
Unity3D Ignore Folders
For an up to date version checkout Github maintained Unity.gitignore file without OS specifics.
Unity3D Settings
For versions of Unity 3D v4.3 and up:
External
option inUnity → Preferences → Packages → Repository
.Edit
menu and pickProject Settings → Editor
:Version Control Mode
toVisible Meta Files
.Asset Serialization Mode
toForce Text
.File
menu.Want you migrate your existing repo to LFS?
Check out my blog post for steps on how to do it here.
Additional Configuration
One of the few major annoyances one has with using Git with Unity3D projects is that Git doesn't care about directories and will happily leave empty directories around after removing files from them. Unity3D 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 Unity3D 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.We now have seamless integration to unity with Github to Unity extension... https://unity.github.com/
The new GitHub for Unity extension brings the GitHub workflow and more to Unity, providing support for large files with Git LFS and file locking.
At the time of writing the project is in alpha, but is still usable for personal projects.