Git is a popular form of source control used with Unity. Source control allows you to roll back or recover changes made to files in your project.
This resource is not exhaustive, but it should get beginners off the ground and using Git.
If you get stuck using this guide there are similar alternatives that you can search for.
I advise having a look over other resources to prevent misunderstandings, and to understand alternative steps.
There are also alternate ways to perform most steps. One notable difference is choosing to initialise a local repository on a pre-existing project and linking an empty origin to that project. I chose not avoid this way because it may be harder to learn based on a chosen GUI. Some people also may enjoy an entirely command line approach.
There are a lot of terms to get used to when using Git, and understanding them is key to not overriding your work.
While source control is a powerful and necessary presence in your project, it can destroy work when terms are misunderstood and warnings are overlooked.
| Term | Description |
|---|---|
| Repository | A location where the backup data structure is stored. Repo for short. |
| Local | A location on your computer. |
| Remote | A location on a server. |
| Origin | Another name for the remote repository. |
| Clone | Downloading a remote repository from scratch. |
| Commit | A saved state, a snapshot of your project. Also a verb, the act of storing that state. Your repository is a collection of commits. |
| Stage | Moving files to a state that previews a commit. Un-staged files will not be committed. Staged files can be considered a half-way state towards a commit. |
| Push | Uploading the local repository to the remote so it matches. |
| Pull | Downloading the remote repository locally so it matches. |
| Fetch | Downloading the remote repository locally, but not updating it to match. |
| Tracked | Files that have been committed or staged. |
| Discard | Destructively throw away changes so they match the currently tracked commit. |
| Branch | An independent line of commits separate from another. The default branch is often called main (or master1). |
| Merge conflict | When pulling or pushing a merge conflict occurs when the remote and local don't match. |
| Merge | The act of combining branches or fixing a merge conflict. |
| Checkout | Setting the state to a certain commit, can be used to temporarily visit older states or other branches. |
| LFS | Large File Storage. Git functions best with text files, LFS better handles large and binary files with Git. |
| gitignore | A file that describes files that are ignored by Git. |
| Force | The act of destructively overriding another state. For example, force push, or force pull. |
| Command line | A text interface for interacting directly with Git. |
| GUI | A Graphical User Interface for interacting with Git. |
If you haven't already created a project, create one!
Choose a host for your remote, and create an account.
Popular services include GitHub, GitLab, and Bitbucket.
Create a new repository using your service.
If your service allows you to choose a .gitignore file, choose Unity if present, otherwise choose none.
I choose to create a README file, which makes a good landing page for the project.
Choose private visibility unless you are confident that you want your project to be public, and have the appropriate licenses to do so.
The command line can be finicky and unfamiliar to work with, I recommend a using a Git GUI to interact with your repository.
Applications that are not embedded into an IDE. Alphabetical; see Git - GUI clients for a more exhaustive list.
| Name | Cost | Platforms | Description |
|---|---|---|---|
| Fork | Free to evaluate, $50 for lifetime license. | Windows, macOS | Great! What I use! |
| Git Extensions | Free. | Windows | A little clunky, has windows explorer context menu support. |
| GitHub Desktop | Free. | Windows, macOS, Linux | Simplified interface may be confusing depending on your preferences. Can hide terminology and steps. |
| GitKraken | Free solo use with public repos, $5/month (various tiers). | Windows, macOS, Linux | Has an awesome undo feature. Can hide errors and be 'too smart'. |
| SmartGit | $59/year (various tiers). | Windows, macOS, Linux | A little clunky, feature-heavy. |
| Sourcetree | Free. | Windows, macOS | Fine, nothing stand-out. |
| Sublime Merge | Free to evaluate, $99 for 3 years of updates. | Windows, macOS, Linux | Minimal interface may be confusing depending on your preferences. |
These days most IDEs have an integrated Git client, consider using it if you're comfortable.
| Name | Description |
|---|---|
| JetBrains Rider | I am not a fan of the windowed approach and prefer a unified overview UI. You may feel differently. I recommend enabling the non-modal commit interface. |
| Visual Studio Code | Minimal interface reliant on command palette may be confusing depending on your preferences. |
| Visual Studio | Nice visual separation, but a little clunky. |
This will download the current contents of remote repository to your PC.
.git extension.A local git repository is represented by a hidden folder in the directory, named .git. This folder contains a structure representing the files tracked by git.
LFS is included in many Git GUI clients, if you have previously installed git it may be also be installed.
git lfs install in the command line.Updated git hooks.
Git LFS initialized.
.gitattributes file.This file must be named .gitattributes. This is a file name with only an extension. Make sure this is correct.
Changes to this file require files be re-staged if staged already.
If you didn't set up a Unity gitignore file earlier, GitHub provides a popular one.
Place this file at the root of your repository (under the root folder).
This file must be named .gitignore. This is a file name with only an extension. Make sure this is correct.
In your GUI, stage all your changes/files, including the gitignore.
Check that the Library folder's contents is not present in the staged files. If is is, your gitignore is incorrectly configured.
The contents of Temp, UserSettings, and Logs, or files with .csproj or .sln extensions, should also not be present.
Do not commit things before you have checked the changes.
It is hard to undo commits, and it is even harder to undo pushed changes.
If you lose your local changes you can discard that change, reverting things back to the state that is in your currently tracked commit.
Users with access to your remote repository should be able to clone it.
When two users are committing to the same location there may be merges, and with merges can come conflicts.
It's good practice to commit and pull often.
Smart merge (UnityYAMLMerge) merges Unity's YAML serialization format in a semantically correct way.
This means it can automatically merge some complex scenarios while avoiding merging things that create an incorrect output. Without this, assets like scenes and prefabs can prove extremely difficult to merge.
UnityYAMLMerge.exe file to a fixed location so you don't have to change settings when you update Unity. This merge tool very rarely changes, so it should not be an issue.Unity\Editor\Data\Tools\UnityYAMLMerge.exe on windows, Unity.app/Contents/Tools/UnityYAMLMerge on macOS.C:/Program Files/Unity/UnityYAMLMerge.exe.merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED".gitconfig file to contain:[merge]
tool = unityyamlmerge
[mergetool "unityyamlmerge"]
trustExitCode = false
cmd = 'C:/Program Files/Unity/UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"Some less necessary terms to learn:
| Term | Description |
|---|---|
| Stash | A backup of files that isn't a direct part of a branch. Mostly used for temporary backups while reorganising. |
| Rebase | The merging of two branches so that they become linear, this rewrites the commit history. |
| Init | Creating a local repository (no remote has been set yet). |
| Git Flow | A branching strategy that involves having main, develop, release, feature and hotfix branches. |
| Fork | Taking another user's remote repository and hosting it yourself in a diverged state. |
| Pull request | A request to merge a branch (often on a fork) into another. This is often done in open source repos and controlled professional settings. |
| Blame | A tool that looks at history and associates changes per-line to the user who committed them. |
| Diff | The changes (the difference) between two files or states. |
| The index | The location changes are registered to when they are staged. |
This page may be incomplete, if you can help please report an issue with this page so this page can be improved.
Master is generally being phased out, but due to the nature of Git, you'll find legacy repos still using the name.↩