How to Setup Version Control for Unity using GitHub

Why use Version Control System?

Tristan Engel
6 min readApr 20, 2021

--

In this article we will be using Git and GitHub. Git is a version control system used to track changes you make to files and keep a record of what has been changed. This will allow you to revert back to older versions when needed.
GitHub is an online hosting service for Git to allow collaboration. Allowing multiple persons to create changes on a project and merging them into one.

Follow the steps below to setup version control for a Unity project.

1. Get Git by clicking the link below:

Under Downloads select the appropriate version and install it

There are a lot of options to choose when installing, but just leave it all on default settings.

2. Sign up for GitHub

To use GitHub you’ll need an account. Just fill in your info and follow the instructions. If it asks about your experience etc you can just skip those.

https://github.com/

3. GitHub setup

3.1 Click New after you sign in on GitHub as marked below with the yellow arrow.

3.2 Setup your new repository

Enter a name and optional description.
Choose whether you want it to be public or private. (You can always change this later)
Important: check “Add .gitignore” and choose the Unity template.
Unity automatically creates a lot of temporary files and there’s no need to keep track and save them.

Click “Create repository” and leave this window/tab open, we come back to it later.

4. Linking your Unity Project to GitHub using Git

4.1 Choose a Unity project

4.2 Use the File Explorer to navigate to the directory of your Unity project and R-Click and choose “Git Bash Here”
This will open the command line editor of Git.

You should see a window open like this:

4.3 Some basic commands
ls — list: will give a list of the files and folders in the current directory

cd — change directory: enter the folder you want to change the current directory to

Tip: if you type a listed name half way and hit tab it will try to auto complete it. Since you can’t use spaces either use tab to auto complete or put it between quotation marks.

../ — go up in the directory

4.4 Master to main
Some name conventions in IT have recently changed because of slavery. Default branch name on GitHub changed from “master” to “main”, but currently the default local (Git) branch name is still called “master” . These name will probably be updated in the future. For now enter the following commands to rename the default branch name to “main” and switch.

git config — global init.defaultBranch main
git switch main

4.5 Initializing
git init

4.6 Linking GitHub to local project
Open up your GitHub page on repository you want to link.
Click the green button and you should see a link like displayed below.

Copy this link and paste it instead of the XXXX with R-click > Paste
git remote add origin XXXX

Origin is most commonly used name for naming the starting place of a project. After hitting enter, follow up with the next command with the -v to verify.
git remote -v

Notice the blue main at the end of your working directory. This is your branch name.

5. Committing your changes

Important to keep in mind is that the files are on a server. When you’re working with multiple persons on the same server you get a decent chance of merge conflicts and other problems. You can’t just commit ans ignore what else might be happening on the server.
To minimize the chance for merge conflicts follow these 3 steps:

5.1 Pull from server
Get the changes made from the server so you are not sending some outdated files into a newer versions.
Pulling from origin into main(local branch).

git pull origin main

The _HEAD part means latest version.
Tip: you can get a list of helpful commands by entering the command below.
git --help

Important: if you forget to pull from the server before you first commit you miss the .gitignore file. This file contains the information to skip the unnecessary files for Unity for GitHub. You will see a lot of temporary files being included in your commits if the ignore file is missing.

Side note: Branch in Git can be created for different working environments for example development, feature and main. See my other article for more details:

5.2 Commit
Next save your files locally as a new version aka committing. Before that we check the status.
git status

Red means these files are not commuting. You will have to manually add them.

git add Assets/
git status

Notice the green text showing the files from the Assets folder have been added. Instead of manually adding each file or folder we can use the following command to add all at once.
git add . = means add all

git commit -m “Enter message text here”
-m is to add an message. It’s always good to add one.

5.3 Push
Finally send your new updated version of the project to the server with the next command.
git push origin main

If it’s the first time you will get a message saying you need to configure your email and username. Then as the messages state enter the following commands with your info and repeat the previous command to push. You might need to authorize with your browser. Just follow the instructions.

git config --global user.email “Yourmail@example.com”
git config --global user.name “YourUsername”

Pushed

6. Check the Result

Now we made our first changes and pushed them to the server let’s go to GitHub and refresh the repository page. If it went all successfully you should see your project files now on your GitHub page.

--

--

Tristan Engel
Tristan Engel

Written by Tristan Engel

Aspiring developer that’s self-learning Unity & C# to transition to a career with Unity. I got a passion for creating interactive experiences.

No responses yet