Get Your Unity Project Under Source Control With Git And Github

Joshua Nielsen
5 min readApr 30, 2021

--

So you’ve just started with Unity, pursuing your dream of game development. You don’t want to get bogged down with the details... you want to get started right away building your dream game! But let’s just slow down for a moment and give a thought to source control.

Source Control?

Source control is the process by which we track and manage changes in our software. In more practical terms, it’s a way in which we can protect ourselves from missteps made during development and it can also aid us in combining different versions of code into something coherent. Developing software (such as games) can become very complex very quickly, so I guarantee you will be glad to have your projects in source control, especially if you are working with other people.

Git is currently the most popular software for source control, and Github is a free cloud-based space in which you can store your projects. Git is a very large subject on its own (and the subject of several published books) so I’m just going to show how you can get started getting your own Unity projects first into Git and then Github.

First Steps

Before anything else, we will need to install Git (https://git-scm.com/). This will be a slightly different process for everyone, depending on their Operating System, so just follow the instructions on the website to download and install.

We will also need an account with Github (https://github.com/). Sign up for an account using the link provided.

The last thing we will need is a Unity project to add to our repository. If you don’t have one already, make sure to start a new project now.

Adding The Project To Git

We will be using the Command Line Interface (CLI) to work with Git. If Git has been installed correctly, we should be able to type ‘git’ in our command prompt and see the help message.

This is actually just the top of the message. The full help message is much longer.

Next, we navigate to our project’s directory. Once there, type in the command ‘git init’. This command sets up a new git repository for our project. Now we can use the full range of git commands, including ‘git status’ which will show us the status of our files in relation to the git repository.

If we do that now, we will see that none of our files are actually in the repository yet. By using the ‘git add’ command, we can prepare individual files or files to be committed to the repository. The command ‘git add <file or folder to add>’ adds files or entire folders individually, or we can add all untracked files with the command ‘git add .’ (as in ‘git add’ followed by a period).

At this point there are still no files actually in the repository, so we use the ‘git commit’ command to do that. We used the ‘git add’ command for convenience sake, to allow us to add many individual files or folders without having to make many separate commits. Once we are ready to commit, use the command ‘git commit -m’, followed by a commit message in quotes. For example, ‘git commit -m “Initial Unity project commit”. The commit message is a short description of what is being changed in the code. If we don’t add the commit message here, Git will open an editor to force us to make one anyway.

And that does it! Our code is now committed to the Git repository.

Adding The Repository To Github

So now we want to get our project hosted in the cloud with Github. Log into Github, go to the main dashboard and find the button for creating a new repository.

The button for creating a new repository should be in the upper left corner.

We will see several options here for setting up a new repository. For this example, let’s just provide the repository a name (which is required). All of these settings can be set/changed later anyway.

Once the repository is created in Github, we need to return to our command line interface to link our new Github project with our existing Git repository. Once we have returned to the project’s directory in the CLI, we will use the ‘git remote add’ command followed by the name we want for our remote server and its URL. For example, ‘git remote add origin https://github.com/your-github-account/unity-git-example.git’. The name we set for the remote server is arbitrary, but most use “origin” by convention. The URL can be found in the Github page for our repository. Click on the “Code” button to reveal an easy to copy HTTPS URL. If prompted to enter Github credentials, go ahead and do that.

Next we need to “push” our local repository to Github. Before we can do that though, we need to know what “branch” of the code we will be uploading.

Branches in Git are a way to have different working versions of the same code. For example, you may want to keep a version that you know is stable on one branch while working on a new feature in another. Fully exploring the topic of branches is outside the scope of this article, but I fully recommend further reading on the subject for anyone who is interested in unlocking the full capabilities of Git.

For now though, we just want to know the name of our branch. By default, depending on your version of Git, this should be either “master” or “main”. We can find out for certain by using the command ‘git branch’. This command will show us all of our branches, and the one we are currently working with will have an asterisk next to it.

Quick note on this: If it turns out your default branch is “master”, you may want to rename it to “main” before continuing. Github’s default repository name is “main”, so if you upload a “master” branch you will end up with two separate branches in Github.

With the name of our branch known, now we can upload our files by using the command ‘git push’ followed by the name of our remote server and the branch we want to upload. For example, ‘git push origin master’. The branch we upload does not need to be the one we are currently working on.

And that’s it! We now have our code uploaded to Github! This is a good starting point, but I would definitely recommend further exploration to really get the full value of Git.

--

--

No responses yet