Challenges & Solution
There are 2 main challenges to using Git & GitHub for Unity projects:- Unity projects create lots of temporary & internal-used files, which should be excluded from the source control system. The ".gitignore" mechanism of Git could be configured to solve this in the prefect way, i.e., exclude unwanted files from Git;
- Unity project contains lots of binary format files, esp. large binary files, which are difficult to be tracked by the source control systems. To solve this challenge, 2 following methods should be deployed side by side:
- Configure Unity project to use text format instead of binary format if possible
- Use Git LFS(Git Large File Storage) service to track these large binary files such as images, audios, 3d models and etc
How-to guide
1. Prepare
- install & config Git
- setup GitHub account
- install Git LFS (Git LFS has been included in Git for Windows)
- your Unity project waiting for version control
2. Config Unity Project
You may check the following settings of the Unity project - "Edit > Project Settings > Editor":- "Version Control > Mode" should be "Visible Meta Files"
- "Asset Serialization > Mode" should be "Force Text"
3. Config Git for Unity Project
You create the file ".gitignore" as follows and put it at the root directory of the Unity project:# This .gitignore file should be placed at the root of your Unity project directory
#
# Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore
#
/[Ll]ibrary/
/[Tt]emp/
/[Oo]bj/
/[Bb]uild/
/[Bb]uilds/
/[Ll]ogs/
/[Uu]ser[Ss]ettings/
# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
/[Mm]emoryCaptures/
# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta
# Uncomment this line if you wish to ignore the asset store tools plugin
# /[Aa]ssets/AssetStoreTools*
# Autogenerated Jetbrains Rider plugin
/[Aa]ssets/Plugins/Editor/JetBrains*
# Visual Studio cache directory
.vs/
# Gradle cache directory
.gradle/
# Autogenerated VS/MD/Consulo solution and project files
ExportedObj/
.consulo/
*.csproj
*.unityproj
*.sln
*.suo
*.tmp
*.user
*.userprefs
*.pidb
*.booproj
*.svd
*.pdb
*.mdb
*.opendb
*.VC.db
# Unity3D generated meta files
*.pidb.meta
*.pdb.meta
*.mdb.meta
# Unity3D generated file on crash reports
sysinfo.txt
# Builds
*.apk
#*.unitypackage
# Crashlytics generated file
crashlytics-build.properties
# Packed Addressables
/[Aa]ssets/[Aa]ddressable[Aa]ssets[Dd]ata/*/*.bin*
# Temporary auto-generated Android Assets
/[Aa]ssets/[Ss]treamingAssets/aa.meta
/[Aa]ssets/[Ss]treamingAssets/aa/*
# Ignore some common files for Windows
#
# Get latest from https://github.com/github/gitignore/blob/master/Global/Windows.gitignore
#
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db
# Dump file
*.stackdump
# Folder config file
[Dd]esktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp
# Windows shortcuts
*.lnk
Note - this sample ".gitignore" file contains:
- common files for Unity projects should be ignored
- common files for Windows should be ignored
4. Config Git LFS for Unity Project
4.1 Activate Git LFS
You need to activate Git LFS as follows, only need run this once per user account:$ git lfs install
> Git LFS initialized.
4.2 ".gitattributes"
You create the file ".gitattributes" as follows and put it at the root directory of the Unity project:# Git LFS tracking file list:
# 3D models
*.3dm filter=lfs diff=lfs merge=lfs -text
*.3ds filter=lfs diff=lfs merge=lfs -text
*.blend filter=lfs diff=lfs merge=lfs -text
*.c4d filter=lfs diff=lfs merge=lfs -text
*.collada filter=lfs diff=lfs merge=lfs -text
*.dae filter=lfs diff=lfs merge=lfs -text
*.dxf filter=lfs diff=lfs merge=lfs -text
*.fbx filter=lfs diff=lfs merge=lfs -text
*.jas filter=lfs diff=lfs merge=lfs -text
*.lws filter=lfs diff=lfs merge=lfs -text
*.lxo filter=lfs diff=lfs merge=lfs -text
*.ma filter=lfs diff=lfs merge=lfs -text
*.max filter=lfs diff=lfs merge=lfs -text
*.mb filter=lfs diff=lfs merge=lfs -text
*.obj filter=lfs diff=lfs merge=lfs -text
*.ply filter=lfs diff=lfs merge=lfs -text
*.skp filter=lfs diff=lfs merge=lfs -text
*.stl filter=lfs diff=lfs merge=lfs -text
*.ztl filter=lfs diff=lfs merge=lfs -text
# Audio
*.aif filter=lfs diff=lfs merge=lfs -text
*.aiff filter=lfs diff=lfs merge=lfs -text
*.it filter=lfs diff=lfs merge=lfs -text
*.mod filter=lfs diff=lfs merge=lfs -text
*.mp3 filter=lfs diff=lfs merge=lfs -text
*.ogg filter=lfs diff=lfs merge=lfs -text
*.s3m filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.xm filter=lfs diff=lfs merge=lfs -text
# Fonts
*.otf filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
# Images
*.bmp filter=lfs diff=lfs merge=lfs -text
*.exr filter=lfs diff=lfs merge=lfs -text
*.gif filter=lfs diff=lfs merge=lfs -text
*.hdr filter=lfs diff=lfs merge=lfs -text
*.iff filter=lfs diff=lfs merge=lfs -text
*.jpeg filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.pict filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.psd filter=lfs diff=lfs merge=lfs -text
*.tga filter=lfs diff=lfs merge=lfs -text
*.tif filter=lfs diff=lfs merge=lfs -text
*.tiff filter=lfs diff=lfs merge=lfs -text
# Unity packages
*.unitypackage filter=lfs diff=lfs merge=lfs -text
# Collapse Unity-generated files on GitHub
*.asset linguist-generated
*.mat linguist-generated
*.meta linguist-generated
*.prefab linguist-generated
*.unity linguist-generated
Note - this sample ".gitattributes" file contains:
- Git LFS tracking file types that Unity currently supports
- automatically collapsing the diffs on GitHub for Unity-generated files
- see more details
Alternatively, you may configure additional file types at anytime, such as:
$ git lfs track "*.jpg"
4.3 Verify Git LFS
You may verify which files are being tracked as Git LFS files:$ git lfs ls-files
5. Initial Commit
After all of Unity project, Git and Git LFS are configured, you can startup Git version control with Unity project as usual.Just run the following Git commands under the root directory of Unity project:
$ git init
$ git add .
$ git commit -m 'Initial commit'
$ git log
Note:
- start a Git repo
- add all source files into staging area(index)
- make initial commit
- show commit log
6. Upload to GitHub
- login in your GitHub account
- create a new repository for Unity project - "Click the top-right-corner plus-sign button > New repository"
- copy the new repo url - "repository page > click Clone or download button > click copy button"
- run the following commands to upload Unity project to GitHub repo at the first time:
- config remote for the new repo
- show remote repositories
- upload Unity source files to remote repo
- show commit log
- run the following command to update Unity project's GitHub repo later on:
$ git remote add origin <repo_url>
$ git remote -v
$ git push -u origin master
$ git log
Note:
$ git push


