In this article we look at adding version (or source) control into our Unity project with Git and Sourcetree (a Git GUI tool)
- What is Version Control?
- Built-in or External
- Setting up your Unity project
You will need to make sure you install Unity and Sourcetree. If you don't already have Git installed on your machine, the Sourcetree installation should provide the option to do so:
In any development project, version control can play a critical role in the development process. It allows a team or individual to track the changes to their project, create save states, revert changes if necessary, or any number of other useful features. When working on a team version control becomes increasingly necessary as multiple people begin making changes to the same set of files. It helps to track who changed what, and when but figuring out why they changed it isnt always as straight forward.
I won't get much into the details of what version control is in this article, but if you would like to find out more about version control check out some of these resources:
When deciding which version control tool to use with Unity, there are a variety of options to consider. Unity has a couple of options for built in version control: Perforce or PlasticSCM. I have not personally used these options since I have only ever worked with Git as my version control software. So for this article we will be exploring the use of an External version control using Git & Sourcetree (a Git GUI tool).
This is a sample of what an open repository looks like with Sourcetree.
If this is your first time using Sourcetree (or source control) there is a lot going on and you don't need to understand all of it right off the bat. Many professional developers still don't understand all of it
Before diving into your project there are a few things that will be convenient to setup before getting started. I personally started with an existing project that was not setup properly, and so it took some additional steps to get everything working as expected.
Open Unity and then open the Edit > Project Settings > Editor menu.
The first setting allows some extra unity metadata files to be exposed that helps Git recognize unity scene and prefab objects easier.
The next setting tells Unity how to save its data, whether as Binary, Text (yaml format) or Mixed. Switching to Text allows these changes to be more easily readible by the human eye, which also helps determine the correct action when handling merges.
This should be set by default
To find out more about the different editor settings see this Unity documentation: https://docs.unity3d.com/Manual/class-EditorManager.html
The final step in setting up your environment for version control is to install Unity's UnityYAMLMerge tool. This tool helps Git & Sourcetree determine how to handle merging complex Unity files in the correct way.
When dealing with version control or working on a team, there is always the opportunity to run into Merge Conflicts. Merge conflicts occur when there is a mismatch between the source of record for your project and your working copy. If your Unity project was not setup properly, dealing with these conflicts can be a pain, if even possible. However, if you setup your Unity project properly then dealing with these conflicts can be trivial.
For more information on installing SmartMerge with Sourcetree see this documentation:
When using a Git repository for your version control you have the ability to add a file called a .gitignore which can define rules for Git on what project files it should 'ignore' and not track. In many projects this means things like temporary files, assets, or packages. A .gitignore file for Unity might look something like this:
.DS_Store Builds Library Temp Logs obj *.csproj *.pidb *.unityproj *.sln *.userprefs .vs
A good starting point for a .gitignore in your Unity project is the official .gitignore published by github which can be found here: https://github.com/github/gitignore/blob/master/Unity.gitignore