185

What are the detailed steps necessary to prepare a Unity project for committing to a git repository eg. github? I don't want to store unnecessary files (specially temp files and avoid binary formats as much as possible) and I would appreciate a walk-through.

German
  • 10,263
  • 4
  • 40
  • 56
  • 91
    Oh definitely not. I'll stick to git, thanks – German Dec 23 '14 at 10:33
  • hi German, I was mainly just joking, but, svn is definitely easier for UNity projects. – Fattie Dec 24 '14 at 11:56
  • 2
    german, I just realised you mention to HIDE meta files. this is completely wrong. Is it a typo? Notice: http://docs.unity3d.com/Manual/ExternalVersionControlSystemSupport.html "by selecting Visible Meta Files" and "2. Enable Visible Meta files in Edit->Project Settings->Editor" It's a fairly basic point that you MAKE VISIBLE meta files for source control, you can see 100s QA on it on the unity forum. what's the confusion here? cheers! – Fattie Dec 24 '14 at 12:03
  • 1
    Wow! It's a very unpleasant typo on my side. Thanks for pointing it out (fixed) – German Dec 24 '14 at 15:37

2 Answers2

262

On the Unity Editor open your project and:

  1. Enable External option in UnityPreferencesPackagesRepository (only if Unity ver < 4.5)
  2. Switch to Visible Meta Files in EditProject SettingsEditorVersion Control Mode
  3. Switch to Force Text in EditProject SettingsEditorAsset Serialization Mode
  4. Save Scene and Project from File menu.
  5. Quit Unity and then you can delete the Library and Temp directory in the project directory. You can delete everything but keep the Assets and ProjectSettings directory.

If you already created your empty git repo on-line (eg. github.com) now it's time to upload your code. Open a command prompt and follow the next steps:

cd to/your/unity/project/folder

git init

git add *

git commit -m "First commit"

git remote add origin git@github.com:username/project.git

git push -u origin master

You should now open your Unity project while holding down the Option or the Left Alt key. This will force Unity to recreate the Library directory (this step might not be necessary since I've seen Unity recreating the Library directory even if you don't hold down any key).

Finally have git ignore the Library and Temp directories so that they won’t be pushed to the server. Add them to the .gitignore file and push the ignore to the server. Remember that you'll only commit the Assets and ProjectSettings directories.

And here's my own .gitignore recipe for my Unity projects:

# =============== #
# Unity generated #
# =============== #
Temp/
Obj/
UnityGenerated/
Library/
Assets/AssetStoreTools*

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

# ============ #
# OS generated #
# ============ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Icon?
ehthumbs.db
Thumbs.db
German
  • 10,263
  • 4
  • 40
  • 56
  • 23
    You may want to refer to [gitignore.io for some suggested ignore content](http://www.gitignore.io/api/unity). – ChrisGPT was on strike Feb 05 '14 at 13:25
  • 1
    I will add my own .gitignore recipe here, thanks for the advice (makes sense) – German Feb 09 '14 at 12:07
  • 1
    What are the .suo files and why are they kept in gitignore.io? Same question about the Assets/AssetStoreTools* folders(s) – Agostino Jun 07 '14 at 17:05
  • 1
    .suo files contain your last configuration in the IDE (eg. which open tabs, etc) so you don't want to send that to the repo (it's specific to each developer) http://bytes.com/topic/c-sharp/answers/652520-what-purpose-sln-und-suo-files-why-arey-outside-project-folder Assets/AssetStoreTools* is a directory that is created when you install the unity asset store plugin that allows you to send projects to the asset store (if you're a publisher you want to filter that out) – German Jun 09 '14 at 00:04
  • 9
    As of 4.5, it looks like this option: "Enable External option in Unity → Preferences → Packages → Repository" is no longer in preferences or necessary. I didn't do that, but followed the rest of this, and it worked just fine. – garrettmurray Jul 10 '14 at 20:28
  • 1
    Why `Force Text`? I am using the latest Unity 4.5. Isn't it better to leave it to `Mixed`? – Houman Sep 07 '14 at 21:20
  • 1
    Because of "The Good" points in the answer to this question: http://answers.unity3d.com/questions/222281/asset-serialization-mixed-vs-force-text.html – German Sep 08 '14 at 01:42
  • Note, traditionally it's a basic that you MAKE VISIBLE meta files to use source control with Unity3D. (see any unity doco) – Fattie Dec 24 '14 at 12:04
  • Just TBC for anyone reading in the future, there **was a typo** in German's answer. In fact you must turn **VISIBLE ON, YES**, as it now says (2015) in the answer above. – Fattie Dec 24 '14 at 17:03
  • 12
    By the way, confirmed everything works as is for Unity 5. Also, thanks a ton, German- this is a true life saver – DragonJawad Mar 15 '15 at 01:02
  • I would love to see recommended settings for line endings, configured locally or with .gitattributes, for cross-platform Unity projects. https://help.github.com/articles/dealing-with-line-endings/ – cyrf Dec 17 '15 at 17:38
  • Why Step 8: delete everything but the Assets and ProjectSettings directory? – strider Jan 22 '16 at 19:32
  • @bitstrider because otherwise you would push those files to the repo and they are not necessary to do version control on the project (they will be regenerated) – German Jan 25 '16 at 15:18
  • Why do you have to ignore Obj files with Unity? – Waltari Jul 22 '16 at 07:26
  • @Waltari those are generated files during the build process that contain intermediate compilation files and debug info. They should obviously be ignored in version control (they will be regenerated when you build the project from source). Same goes for ExportedObj if present in your project – German Jul 22 '16 at 13:41
  • I thought obj was an 3D model file type? – Waltari Jul 24 '16 at 07:15
  • @Waltari Take a look at the ignore file. It's not excluding .obj files, it's excluding obj directories – German Jul 25 '16 at 12:31
  • @German Why the "Icon?"? – Adam Mendoza Sep 06 '16 at 04:47
  • Some OSes generate Icon* files. If your doesn't you can safely remove that line – German Sep 06 '16 at 12:22
  • I can't seem to find Unity -> Preferences? – Neon Warge Nov 05 '16 at 07:11
  • Step 5: why to delete the **Library** folder?, your `.gitignore` is already ignoring it – fguillen Aug 20 '17 at 21:19
  • 2
    @fguillen you don't have to. The point if you "can" if you want, it's just a note – German Aug 21 '17 at 12:09
23

Since Unity 4.3 you also have to enable External option from preferences, so full setup process looks like:

  1. Enable External option in Unity → Preferences → Packages → Repository
  2. Switch to Hidden Meta Files in Editor → Project Settings → Editor → Version Control Mode
  3. Switch to Force Text in Editor → Project Settings → Editor → Asset Serialization Mode
  4. Save scene and project from File menu

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

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

zasadnyy
  • 2,107
  • 2
  • 19
  • 26