GIT good in Unity

Using a VCS (Version Control System) is a no-brainer. And the go-to solution among VCSs is GIT.

How to unignore a folder inside an already ignored folder

TLDR: Don't ignore a folder if you need anything inside it tracked. Ignore all files using wildcards instead.

Example of unignoring the folder "sub" inside the "test" folder.

# Use wildcards to ignore any file and sub-folder file in a directory.
/test/**/*.*
/test/**/.*
# Then you can unignore anything inside it by prepending a "!".
!/test/**/sub/*.*

GIT does not know anything about folders. If it finds a folder path in the .gitignore file then it will simply ignore EVERYTHING inside that folder. It does not matter what else you write in your .gitignore file. GIT will act as if that folder did not exist. It simply won't track any changes inside that folder.

That's why code like this will never work:

# The next line nukes everything inside the "/test" folder.
/test
# Git will simply ignore the next line since it deals with an
# already ignored path (starting with "/test").
# Git will never check it.
!/test/**/sub/*.*

How to manage large binary files like .PSD in GIT

It depends. If the file seems to be non-textual, but the structure is text-like (unity scene in ASCII format) the standard git diff method performs well and you can commit them without worrying too much.

If your files are binary (meaning they change all over the place with every iteration and there is no text-like structure) then there are multiple options:

A) Do not commit them to git and back them up separately.

B) Do commit them but beware of the consequences.
Usually, git is rather smart and detects binary files automatically. Though you may want to add a .gitattributes (docs) file with *.psd binary (resp. for all your binary file types) just to be sure.

C) Use a git extension to handle them. The most common one is git-lfs (Git Large File Storage). Though that can not* be used in local repositories.

GIT LFS Lock

If you are working in a team then this is a worthy addition to your toolchain.

Disclosure: This text may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help fund this project! The links are created by Unity and Partnerize (Unity's affiliate partner).

How to automatically add the GIT hash to every build

Get the EditorGitTool from https://github.com/kamgam/UnityEditorGitTool.

You can then fetch the hash at runtime like this:

public static string _versionHash = null;
public static string GetVersionHash()
{
    if (_versionHash == null)
    {
        _versionHash = "unknown";
        var gitHash = UnityEngine.Resources.Load<TextAsset>("GitHash");
        if (gitHash != null)
        {
            _versionHash = gitHash.text;
        }
    }
    return _versionHash;
}

In the settings, you can specify the path to where the hash of your last commit should be stored.

Alt Settings 2018.4+

The tool can also warn you if you are making a build without doing a commit first.

Alt Warning Dialog

Unity .gitignore file template

A nice source for all sorts of ignore files is the GitIgnore Repo on GitHub. The following example is based on the Unity Git ignore files from there.

# This .gitignore file should be placed at the root of your Unity project directory
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
# Updated to also ignore JetBrains IDEs, VisualStudioCode and Blender
/[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/

# Recordings can get excessive in size
/[Rr]ecordings/

# 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 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
*.aab
*.unitypackage
*.app

# 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/*

# Jetbrains IDEs
.idea/

# VS CODE
.vscode/

# Blender
*.blend1
*.blend1.meta
*.blend2
*.blend2.meta
*.blend3
*.blend3.meta
*.blend4
*.blend4.meta
*.blend5
*.blend5.meta

# Caches
Thumbs.db
Thumbs.db.meta
Desktop.ini
Desktop.ini.meta
.DS_Store

Asset Recommendations

Here are some assets that may be useful to you. If you get one of them then some of the money will go towards funding this project. Thank you.

Disclosure: This text may contain affiliate links, which means we may receive a commission if you click a link and purchase something that we have recommended. While clicking these links won't cost you any money, they will help fund this project! The links are created by Unity and Partnerize (Unity's affiliate partner).