How to use Git for Unity3D source control?

253,562

Solution 1

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.

# =============== #
# Unity generated #
# =============== #
Temp/
Library/

# ===================================== #
# Visual Studio / MonoDevelop generated #
# ===================================== #
ExportedObj/
obj/
*.svd
*.userprefs
/*.csproj
*.pidb
*.suo
/*.sln
*.user
*.unityproj
*.booproj

# ============ #
# OS generated #
# ============ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Unity3D Settings

For versions of Unity 3D v4.3 and up:

  1. (Skip this step in v4.5 and up) Enable External option in Unity → Preferences → Packages → Repository.
  2. Open the Edit menu and pick Project Settings → Editor:
    1. Switch Version Control Mode to Visible Meta Files.
    2. Switch Asset Serialization Mode to Force Text.
  3. Save the scene and project from 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.

Solution 2

In Unity 4.3 you also had to enable External option from preferences, but since Unity 4.5 they dropped option for that, so full setup process looks like:

  1. Switch to Visible Meta Files in Editor → Project Settings → Editor → Version Control Mode
  2. Switch to Force Text in Editor → Project Settings → Editor → Asset Serialization Mode
  3. Save scene and project from File menu

Also our team is using a bit more extended .gitignore file:

# =============== #
# Unity generated #
# =============== #
Temp/
Library/

# ===================================== #
# Visual Studio / MonoDevelop generated #
# ===================================== #
ExportedObj/
obj/
*.svd
*.userprefs
/*.csproj
*.pidb
*.suo
/*.sln
*.user
*.unityproj
*.booproj

# ============ #
# OS generated #
# ============ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

Note that the only folders you need to keep under source control are Assets and ProjectSettings.

More information about keeping Unity Project under source control you can find in this post.

Solution 3

What is GIT?

Git is a free and open source distributed version control system (SCM) developed by Linus Torvalds in 2005 ( Linux OS founder). It is created to control everything rom small to large projects with speed and efficiency. Leading companies like Google, Facebook, Microsoft uses GIT everyday.

If you want to learn more about GIT check this Quick tutorial,

First of all make sure you have your Git environment set up.You need to set up both your local environment and a Git repository (I prefer Github.com).

GIT client application Mac/Windows

For GIT gui client application i recommended you to go with Github.com,

GitHub is the place to share code with friends, co-workers, classmates, and complete strangers. Over five million people use GitHub to build amazing things together.

Unity3d settings

You need to do these settings

Switch to Visible Meta Files in Edit → Project Settings → Editor → Version Control Mode.

enter image description here

Enable External option in Unity → Preferences → Packages → Repository

enter image description here

Switch to Force Text in Edit → Project Settings → Editor → Asset Serialization Mode.

enter image description here

Source: Using Git With 3D Games Source Control

Solution 4

To add to everything stated, it is also ideal to use git lfs with Unity. I have been using this since it came out and I had no trouble with it.

You will want to add this .gitattributes next to your .gitignore file

*.cs diff=csharp text
*.cginc text
*.shader text

*.mat merge=unityyamlmerge eol=lf
*.anim merge=unityyamlmerge eol=lf
*.unity merge=unityyamlmerge eol=lf
*.prefab merge=unityyamlmerge eol=lf
*.physicsMaterial2D merge=unityyamlmerge eol=lf
*.physicsMaterial merge=unityyamlmerge eol=lf
*.asset merge=unityyamlmerge eol=lf
*.meta merge=unityyamlmerge eol=lf
*.controller merge=unityyamlmerge eol=lf

*.a filter=lfs diff=lfs merge=lfs -text
*.mp3 filter=lfs diff=lfs merge=lfs -text
*.wav filter=lfs diff=lfs merge=lfs -text
*.aif filter=lfs diff=lfs merge=lfs -text
*.ttf filter=lfs diff=lfs merge=lfs -text
*.png filter=lfs diff=lfs merge=lfs -text
*.jpg filter=lfs diff=lfs merge=lfs -text
*.exr filter=lfs diff=lfs merge=lfs -text
*.fbx filter=lfs diff=lfs merge=lfs -text
*.FBX filter=lfs diff=lfs merge=lfs -text
*.rns filter=lfs diff=lfs merge=lfs -text
*.reason filter=lfs diff=lfs merge=lfs -text
*.lxo filter=lfs diff=lfs merge=lfs -text

That is my rolling file list. If you use additional binary files not listed, add them.

I also have files configured to use yamlmerge, you would need to set this up. You can read about it here: http://docs.unity3d.com/Manual/SmartMerge.html

Solution 5

I thought that I might post a simpler .gitignore for anyone that is interested:

# Ignore Everything
/*

# Except for these
!/.gitignore
!/Assets
!/Packages
!/ProjectSettings
Share:
253,562
PressingOnAlways
Author by

PressingOnAlways

I'm a programmer interested in various fields from backend web development to mobile programming. I currently work for an awesome company. Professionally, I work with Ruby on Rails systems nowadays, but I have done everything from Java/Spring/Hibernate to PHP/Symfony to C#/ASP.NET MVP.

Updated on July 08, 2022

Comments

  • PressingOnAlways
    PressingOnAlways almost 2 years

    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.

  • PressingOnAlways
    PressingOnAlways over 10 years
    Mentioning about git workflow is nice, but perhaps I should clarify in my question I am asking about workflows particularly specific to unity 3D. As you may know, unity projects heavily rely on binary files. Are there any special considerations to deal with this? Some recommendations I found when researching this topic was to use a workflow that avoided merges as much as possible. Perhaps you don't share in this sentiment, but my question is more specific towards unity3d specific issues rather than general workflow preferences.
  • S.Richmond
    S.Richmond over 10 years
    Git, when presented with binary files, does not diff them for minor changes and only keep the blocks. So git can have problems with its commit history becoming very bloated over time as many versions of a ~4MB texture or 3D model are stored. We deal with this by only versioning the 'final' version of an asset. Our artists use Dropbox to store and work on their WIP assets - Its much faster and simpler that way. NOTE: Added further explanation of this in my post.
  • Jerdak
    Jerdak over 10 years
    We use a git-annex to manage our large binary content. Windows support isn't awesome but it is getting better. This is only helpful if you don't care about tracking revs in large binary files.
  • S.Richmond
    S.Richmond over 10 years
    @Jerdak - Yeah I reviewed git-annex before taking the above approach and found similar: Windows support isn't very good just yet and in the end our artists don't really need that kind of versioning. Using a service like dropbox or gdrive just works so much better for them as they do versioning too (30 versions in gdrive).
  • PressingOnAlways
    PressingOnAlways over 10 years
    An update to this - we tried your setup and it worked pretty well, but we wanted our assets to be automatically synced. We now use sugarsync to selectively sync the binary assets folder. Dropbox would only sync the dropbox folder, but with sugar sync, you can arbitrarily sync folders anywhere on the hard drive which is extremely useful. We had to change our Assets directory structure a bit to define one subfolder for these large binary files, but so far it has worked really well. We just .gitignore that folder and allow sugar sync to keep it in sync.
  • S.Richmond
    S.Richmond over 10 years
    @PressingOnAlways yeah we had considered this too, but the problem that arose was that things like prefab connections and references would get lost or broken when an artist moved things around. We needed more control over the assets going into the game. Our team is pretty big though so maybe this would only occur when there are many chefs in the kitchen, so to speak.
  • S.Richmond
    S.Richmond about 10 years
    Might be better if you edit my answer at the top to include these new options. :)
  • Slipp D. Thompson
    Slipp D. Thompson about 10 years
    Why the choice to go with Hidden Meta Files?
  • Slipp D. Thompson
    Slipp D. Thompson about 10 years
    Why the choice to go with Hidden Meta Files?
  • Arttu Peltonen
    Arttu Peltonen about 10 years
    I'm wondering about this choice too. I had problems with hidden meta files previously (although it was an earlier version of Unity so things might be different now), so I've been using visible meta files so far and it's been working well.
  • S.Richmond
    S.Richmond about 10 years
    Fixed my copy n paste typo - Yes it should be Visible Meta Files.
  • Agostino
    Agostino almost 10 years
    Point one is plainly wrong. There is no Unity → Preferences → Packages → Repository
  • zasadnyy
    zasadnyy almost 10 years
    @Agostino thanks for pointing on that, point one was valid in Unity 4.3, updated answer to match Unty 4.5
  • Markus
    Markus almost 10 years
    according to docs.unity3d.com/Manual/… it should be Visible Meta Files
  • aflatoon
    aflatoon over 9 years
    +1 This answer is already written above but @NabeelSaleem answer helped me with images he provided and clear guide :) thanks
  • eifersucht
    eifersucht about 9 years
    Works fine for my team. Thank you very much.
  • Tatanan
    Tatanan almost 9 years
    Do I need to push algo the files on the root? I mean: Could I just push /Assets and /ProjectSettings or I have to include the files of /? The reason is that I have a git project for /Assets already. I'd be easier for me to just make a new project for the second folder.
  • S.Richmond
    S.Richmond almost 9 years
    You should push the all the contents of the root as a best practice - Many plugins and other more advanced Unity features will utilize the root project folder for various things.
  • Domi
    Domi almost 9 years
    Edit → Project Settings → Editor → Asset Serialization Mode is worth gold!
  • davidkomer
    davidkomer over 8 years
    If not checking in the large files (i.e. assuming they are always sortof placeholders up till the end) - how does this apply to all the prefab wiring in unity? i.e. if I get an fbx file- and I want to keep that just in dropbox... but in unity want the animator / state machine settings to be saved... will this workflow handle that properly?
  • S.Richmond
    S.Richmond over 8 years
    @davidkomer you should check-in your binary files as soon as you need them in the project itself. I was primarily pointing out that you're best using Dropbox for a place for artists to pass WIP files around. That said, htere is now a new framework being headed up by GitHub called Git-LFS, which removes the limitations with binary files. Google it. :)
  • Yves Lange
    Yves Lange over 8 years
    I can't find Preferences > Packages in Unity 5.x Normal ? ty
  • Yves Lange
    Yves Lange over 8 years
    @NabeelSaleem yes. Actually this step in Unity 5.x is not necessary. ty
  • Fattie
    Fattie about 8 years
    Again, all of these answers are quite out of date on this page. if for some reason you have to use git with Unity, github.com/github/gitignore/blob/master/Unity.gitignore
  • SnapShot
    SnapShot about 8 years
    After following these steps, cloning the repo to a new location and opening the newly cloned project in Unity the hierarchy window may display with only the Main Camera. If, you see this two items to check: first, verify you are on the correct git branch (if using branches). Second, you may need to select a scene in the project window and double click.
  • Alan Hensley
    Alan Hensley almost 8 years
    Does anyone else find that even when specifying Force Text for serialization that without the Library folder, Unity thinks the project was created prior to 5.0 and re-imports the library? Is that a problem?
  • Nikhil
    Nikhil almost 7 years
    Why are the .sln and .csproj files excluded? Aren't those needed to maintain project structure?
  • S.Richmond
    S.Richmond almost 7 years
    Nope. They get regenerated by Unity as required.
  • shinzou
    shinzou over 6 years
    And then how do you set up YAML merge?
  • Nabeel K
    Nabeel K about 6 years
    did you try this ?
  • Isaak Eriksson
    Isaak Eriksson almost 6 years
    Small, simple and fully compatible with all versions: I find this script works the best, even for Unity 2017 and 2018 that have recently changed their project structure (UnityPackageManager/ and Packages/).
  • Thaun_
    Thaun_ almost 6 years
    Dont forget this: docs.unity3d.com/Manual/SmartMerge.html , a way that fixes merge issues.
  • Furkan Kambay
    Furkan Kambay over 5 years
    There is now a tool for Unity that sets up Git and LFS, and also creates a .gitignore and .gitattributes for LFS. GitHub for Unity was released in June 2018 and is still being developed.
  • Fattie
    Fattie almost 5 years
    Regarding the empty directory idiocy in git, of course you just use a .gitkeep file.
  • S.Richmond
    S.Richmond almost 5 years
    @fattie .gitkeep files do the opposite of the problem presented, which is empty dirs NOT being removed when they should. What part of the post is out of date?