Using Git With Unity

This page is somewhat obsolete. It was written when Unity did not really support third party SCM like git or mercurial, but now they do.

Why?

Because while the Asset Server is great for simple projects and organization who have not worked with modern CM products, it quickly shows its simplicity in an organization where real code management and parallel development need to take place. It's the newest and greatest for around 15 years ago.
Note that no general purpose version control system is going to manage Unity's proprietary binary formats. In this case the asset server is just as good as any and this scenario is not the target for this strategy. At this point completely managing an entire project with git is certainly possible but probably more trouble than it's worth. There are a few posts on the forums that talk about this. In our case we use asset server but are sick of the constraints that the asset server places on large scale development with respect to non-binary assets. As much as possible it's my goal here to turn the asset server into little more than a way for artists to supply art assets to the development group.

Basic strategy

There are probably a thousand ways to do this with git but here is the basic strategy I use:

  • One hybrid Unity project that has asset server status and Git status. This is usually a pristine working copy of the Unity project on the maintainer's machine so they can pull clean updates from the asset server and push to the public repository into the assetserver branch.
  • The public repository has a main assetserver branch that only has revisions that are copies of asset server snapshots, work that is not stored in the asset server is not stored here.
  • The public repository has a master branch. The master branch may have content from completed feature branches that has not made it in to the asset server and is therefor not in the assetserver branch. It represents work that will be ready to be merged back into the asset server in a workspace and then pushed to the assetserver branch.
  • Code is pushed through the assetserver branch, subsequently merged into the master branch, then merged into the feature branches.

Stuff that's good to know

Getting code changes into a Git project

Once you make a Unity project Git managed (unless you are using it for asset server syncing) then you should not pull code changes from the asset server anymore only binary assets. Binary assets are not managed by Git with the strategy laid down here and with the .gitignore file above. Getting changes happening in the asset server code-wise should merged in from the master branch. This assumes the maintainer has merged changes assetserver -> master previously.

Getting binary asset changes into a Git project

Check what the latest asset server revision has been merged in from assetserver to master. If you have not merged from master you will need to do so. You will have to check out that asset server revision using Unity in your local working copy to get those binary assets that have changed. If the asset you care about is at a greater revision than is available to via Git another asset server push will be necessary into assetserver. If you just grab the latest copy you will get code that is not yet present in the assetserver or the master branch.

A .gitignore file for Unity

Creating a new Git repository in a Unity project.

From the project folder…

#before you init you might want to put the .gitignore file here (above)
git init
git add .
git commit -m "[assetserver <asset server head revision>]"
#The following lines are if you want to push to a public location
git clone --bare . <repository location>
git remote add public <repository location>

Incorporate Git into an existing Unity project.

To incorporate an existing git repository into your Unity project (make a hybrid project) you have to examine the Git repository and find the last clean asset server ancestor. I like to mark asset server revisions with [assetserver xxxx]. Using unity check out that version of the project from the asset server (not the HEAD revision). Now go into the project folder…

git clone --no-hardlinks --no-checkout <repository location> git.tmp
mv git.tmp/.git/ .
rm -rf git.tmp/
git checkout <your branch>
git reset --hard HEAD
#To make sure you are completely synced with head... nothing should be printed from this.
git diff --cached
Unless otherwise stated, the content of this page is licensed under GNU Free Documentation License.