How to use Git for Unity3D source control?

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.


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:

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


    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:

  • Switch to Visible Meta Files in Editor → Project Settings → Editor → Version Control Mode
  • Switch to Force Text in Editor → Project Settings → Editor → Asset Serialization Mode
  • 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.


    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.

    在这里输入图像描述

    Enable External option in Unity → Preferences → Packages → Repository

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

    在这里输入图像描述

    Source: Using Git With 3D Games Source Control

    链接地址: http://www.djcxy.com/p/68316.html

    上一篇: 如何使用Sony Camera API获取实时取景

    下一篇: 如何使用Git进行Unity3D源代码控制?