How to set up a git repository for a Unity project?

Do you keep backups of your project? Do you work solo or in a team? Well, in either of the cases, you should use consider using a repository!

The most popular type of repository is a git repository. The main feature of git is that it’s decentralized, which means that each of the contributors and server has its own copy of the whole repository. This minimizes the risk of losing progress in case of some disaster, as long as one copy survives.

So how can I set up the git repository for my project?

First you need tools! πŸ”¦

You can use git though console/terminal, but I would recommend using something that has GUI… πŸ€“

There are quite a few tools to choose from. Some are free, some are open source, and others have a price tag. You can find the list of GUI git tools here: https://git-scm.com/downloads/guis/

I’m personally using SourceTree which is available for Mac and Windows.

Choose you server ⌨️

There are a few websites where you can create an account and host your repository for free (with some limitations).

Most popular are: GitHub, Bitbucket, and GitLab.

Each service gives you additional features which may help you run tests or builds, and more. You’ll have to pick the right one for you. For my projects, I’m using Bitbucket.

Repositories view in Bitbucket.

Create local repository πŸ’»

If you already have a project on your disk, you can create a local repository, which we can link later with a remote server.

To create a local repository, you have to open your git tool and find “create local repository”.

Create local repository in SourceTree.

Then you can pick the folder in which you wish to create a local repository. I created the Unity URP project for demonstration purposes.

Pick the location of your project.

After that, you have to confirm, and you have your local git repository! Congratulations!

My repository in SourceTree.

gitignore – Ignore unnecessary files and folders! πŸ”

With Unity (and most other programs), we don’t have to commit all the files. Actually, we shouldn’t do that. Unity creates some additional files which are used by the editor and are ofter changed. We don’t want those files because it will just take additional space without giving us any benefit.

That’s why we have to create “.gitignore” file in our folder as one of the first steps with our repository. In that file, we can specify which folders and files we should ignore and which should be tracked by git.

For Unity project we only have to track three folders: Assets, Packages, and ProjectSettings. Rest can be added to “.gitignore“.

Here is an example of this file

# .gitignore 
# This file should be placed at the root of your Unity project directory

## Unity directories
/[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/

# Asset meta data should only be ignored when the corresponding asset is also ignored
!/[Aa]ssets/**/*.meta

# 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 cache 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

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

When you add this file to your folder, you should see that you can only add files only from important folders.

Files after gitignore added.

Commit your changes πŸ“¬

When you open the project in SourceTree you will see either your current changes or history. But do add your changes to the repository you should find this first view. For me, it’s “File status”.

File status view

In this view, you can simply pick and choose which changes you would like to save and which one to discard.

Don’t forget to add “.gitignore” in your first commit!

When you picked all the files you’re interested in adding, it’s time to add some message to your commit. This message should be short and descriptive. If this is the first commit, you can give it something like “First commit” or “repo init with gitignore” message.

My commit message.

After adding a message, it’s time to make your first commit by clicking on the “Commit” button! πŸ”₯

Now you can switch to the history view, and you will see your new commit (and soon future ones!) πŸ₯³

Repository history.

Create remote repository ☁️

The next step when we have a local repository is to link it with a remote server. I’m using Bitbucket as an example, but those steps should be similar for other servers.

First, we have to create a repository. You have to click on the “plus” symbol on the left side and then pick “Repository” to open a creation view.

Open tab with things to create.
Pick create repository.

You should be directed to the creation view, where you should fill in your repository and project details. After you do that, click “Create“.

Fill basic info about your project.

And we have our remote repository ready to link with our project!

Link local and remote repositories πŸ”—

To link both projects together, you will need a git link. You should find it right on the repository page.

Git clone command with link to our remote repository.

We won’t use “git clone” command, but in this field, we can find our link to the remote repository which is “https://gaello@bitbucket.org/gaello/unitygitsetup.git” in my case.

Let’s open our local project and find the “Repository Settings” window. Then let’s go to the “Remote” tab.

Find Repository Settings.
Our remotes are empty right now.

Click on the “Add” button to add our remote server. A new window should pop up where we should specify the name for our remote server (I’m calling it “origin“) and, of course, URL.

Adding new remote.

And now we have the connection between our local project and remote server! πŸ”₯

Push changes to the server! πŸ›«

The last thing that’s missing is to send our commit to the server. This won’t happen automatically, so we have to do it manually. Luckily it’s not too complicated.

To send our commits to the server, we have to click on the “Push” button.

Pull, Push and Fetch buttons in SourceTree.

We will see a small window where we should select “branch” (that’s something for the other time), which will be pushed to the server. In our case, it will be “master“.

Select master branch to push.

Now, confirm your choice, and you will see the upload window.

Upload progress.

When the upload is finished, you will see a small change in your history view. Until now, you could see only “master” next to the message for your commit, but now you can also see “origin/master”. This means that this commit is also available on the server, and you don’t have to worry about losing your progress with your machine. πŸ₯°

“master” is local and “origin/master” is remote.

Summary πŸ“š

There is still a lot to talk about repositories, but creating one is a great first step! The next step should be setting up SSH for your Git, and enabling Git LFS for your game project. Soon I’ll write about branches. But let’s not get too far ahead with ourselves! πŸ˜…

What do you think about git? Are you using it? Were you looking for a way to set one up? Or maybe you prefer a different type of repository?

Let me know in the comment section below! Or, if you know someone who can get some help with setting a repository up, send him this post. I would really appreciate that! πŸ₯°

And if you are interested in getting emails when I release a new post, sign up for the newsletter!

This example project is also available at my public repository. πŸ”—

Hope to see you next time! πŸ€“

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments