How to use Git for version control of a Unity project

This article can be read in about 5 minutes.
PR

The purpose

Even though Unity has its own version control system (Unity Version Control), we will be using Git, as it is more familiar to us.

We will not be covering the basic usage of Git here.

PR

How to operate

Creating a Repository

You can create the repository either locally or on a server using a standard procedure.

Creating the project and making the first commit

Create a Unity project in your preferred folder using Unity Hub.

Next, download the Unity.gitignore file from the following page.

gitignore/Unity.gitignore at main · github/gitignore
A collection of useful .gitignore templates. Contribute to github/gitignore development by creating an account on GitHub.

First, rename the downloaded Unity.gitignore file to .gitignore and place it in the root directory of your project.

Next, clone your repository into a temporary folder (not your project folder).

Then, move the newly created .git folder from that temporary location into your project’s root directory.

Once these steps are complete, you will be able to push and commit.

If you encounter an error telling you to run git config --global --add safe.directory during a push/commit (or diff), please check the article below for instructions.

PR

Using a cloned environment

o work on the same environment you committed using the method above from a different computer:

Clone the repository just as you would any other project.

Open Unity Hub, click Add in the top-right corner, and then choose Add Project from disk.

A folder selection dialog will pop up. Navigate to your cloned folder and click ‘Add Project’.

You will then see the selected folder in the list.

Launching the project from the list will set up the necessary libraries and files, making it ready to run. (The initial launch will take a while.)

PR

Notice

In the Unity Editor, editing and running a Scene does not save it.

Always remember to save your Scene before pushing your changes. An asterisk (*) next to the Scene name in the Hierarchy means you have unsaved changes.

PR

Result

I’ve successfully set up Git for my Unity project.

PR

Appendix

File Size

Unity projects can get pretty huge, so I wanted to see what the file size would be like if I managed one with Git. I used a project I had on hand that had already been under development for a while.

File Size
The whole project1.91Gbyte
.git folder46.1Mbyte
Managed files138Mbyte

You’ll notice that very few files are being tracked for the entire project. (For your information, the large, untracked files are in the Library folder.)

comment

Copied title and URL