Using Git with Unity projects

Joseph Youngquist
4 min readJul 11, 2022

--

source: https://ndpsoftware.com/git-cheatsheet.html#loc=index; (as of July 2022)

Objective:

Set up git version control for your Unity projects. You’ll thank me later!

The back story:

The other day I was following along on a Udemy course for making a 2D game and somewhere along the way I realized I didn’t catch a step several dozen or so videos back on setting up a new “main” scene and deleting the default “SampleScene” that new Unity projects make. I thought I was renaming the “SampleScene” to “main” and then realized I had two “main” scenes now. So the next sensible thing is to check each of the “main” scenes to see which I wanted to keep — easy enough, keep this one, delete that one.

Delete.

Both are now gone! Hours and hours of work *poof*, gone. 😱

Would have been nice right about now to have a version control on this project”, I thought to myself.

But you see, I did try to make a git repo and failed. I didn’t take a minute to think about what I needed, which I didn’t know what I didn’t know. Namely, which of the thousands of files did I need to keep in the repo vs which I didn’t.

To help save the next person, here’s how to setup git for Unity projects…

Install:

If you don’t already have a git CLI (command line interface) tool installed, then head on over to https://git-scm.com and find the downloads link. The site should auto-detect your OS (operating system) but if not, select the correct link for your system. I’m using Windows, so that’s what I’ll document here but the process for Linux or a Mac are about the same.

Auto-detection highlighted with red box

Create a repo:

There’s two options on how to go about creating a repository. I prefer when making a new repository to start in GitHub. You’ll find the usual suspects for the fields to fill in: name, description, add a README and probably the MOST IMPORTANT, Add .gitignore

You’ll want to select “unity” by typing in the Filter field.

That step is important so all of the files that Unity makes as part of a project do not get tracked in git — it’s not necessary and will cause a bloated repo for file size. We’ll need to come back to this .gitignore file in a bit to fix a few things. But first, click on the “Create repository” button and lets get this pulled down locally by cloning it.

When you go to your newly created repository, you’ll see on the right side a “Code” button. When you click this a modal will appear below and, depending on your settings in GitHub, a tab will be highlighted by default. For me, my default is “SSH”. I highly suggest that you use SSH as your GitHub workflow since using HTTPS usually requires you to enter your username and password for every interaction you have with the server. This is a huge PITA (pain. in. the. a…) If you don’t know how to set up SSH, follow these instructions on GitHub.

With that segue out of the way, copy the git command to your clipboard by clicking on the two overlaid boxes to the right of the git@github.com… text.

Now open up your terminal or bash prompt, navigate to a folder where you keep your projects. If you don’t already have a project go ahead and create a new Unity project. Once done, change into the directory where the project is at and paste in your clone target with the following as an example. Replace the “…” below with your clone target copied into your clipboard.

This will pull your repository down to your local machine and save you some steps like setting up the: git remote add origin … step that locally created repositories require before you can push up to the server.

One Last Thing:

Now, there’s one last step you should do and that is edit the .gitignore file to change the paths on the folders that should be ignored.

All you need to do is delete the “/” at the beginning of those folders and you should be all set. Your finished edited file should look like this:

That’s it. With the proper .gitignore file in place, your repo will not track the ~3,000 files that go into a fresh Unity project.

Coming up in my next post:

My next post will be about how to setup a CI/CD process with GitHub Actions that will automatically build and deploy (webgl) builds.

--

--

Joseph Youngquist
Joseph Youngquist

Written by Joseph Youngquist

Veteran to Digital Media publishing, Software Engineering and Architecture starting on a pivot to Unity Development

No responses yet