Unity, Git and Git LFS
Intro
If you are a software developer, chances are you are familiar with “version control”. If this is the first time you’ve heard the term:
Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later
https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control
This basically means that any change you make in your project, will be recorded and you can easily go from one version of a file to another. One of the most ubiquitous version control systems out there is Git. Personally, this is the version control system I have the most experience with since I use it professionally and in my personal projects.
The history of your project can be kept locally in your computer, or remotely hosted in a server. For the latter, there are several options such as GitLab, BitBucket and GitHub. Each one has different pricing structures, so check out the one that fits your needs the best. For the purposes of this post, we will be using GitHub, but if you prefer to use other server, make sure it supports the Git LFS API.
Git
If you are new to using Git, you can follow the set up guide provided here. If the terminology used confuses you, make sure to checkout this glossary.
The work flow in Git is fairly simple:
- Initialize or clone a repository
- Make sure you are in the correct branch
- Add your changes to the index
- Commit your changes to the repository
Git can be used on the command-line, or on a graphical user interface. GUI options include GitHub Desktop, Fork, GitKraken and many more. I would recommend using the command-line as much as possible and use the GUI’s to view commits, manage stashes and follow branches.
Set up Unity for Git
To configure Unity for Git version control:
- Open your project in the Unity editor
- Go to Edit -> Project Settings -> Editor
- In Version Control select Visible Meta Files
- In Asset Serialization select Force Text
The final step is to add a .gitignore file to avoid tracking unwanted files. If you create a new remote repository in GitHub, you have the option to add a .gitignore file.
If you created a local repository, you can use the following file as a base and modify as needed, just make sure to add the file into version control as well.
[Ll]ibrary/
[Tt]emp/
[Oo]bj/
[Bb]uild/
[Bb]uilds/
Assets/AssetStoreTools*
# Visual Studio cache directory
.vs/
# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.opendb
# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
# Unity3D Generated File On Crash Reports
sysinfo.txt
# Builds
*.apk
*.unitypackage
Git LFS
One of the drawbacks of Git is that it was designed mainly for text files, so management of big files such as audio, video or images is poorly supported. For game development, this is clearly a big drawback as you can easily have files over 5MB that can quickly bloat your repository. Enter Git Large File Storage extension!
First, enable Git LFS in your computer following the steps described here.
As opposed to Git, you need to define the file types you want Git LFS to manage in your repository. This can be done with the git lfs track command for each file type, or by creating a .gitattributes file. A sample file is provided next
## git-lfs ##
#Image
*.jpg filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.gif filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.ai filter=lfs diff=lfs merge=lfs -text
#Audio
*.mp3 filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text
#Video
*.mp4 filter=lfs diff=lfs merge=lfs -text
*.mov filter=lfs diff=lfs merge=lfs -text
# Fonts
*.otf filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
Make sure to add your .gitattributes file to source control, just like you would the .gitignore file.
Until next time!