How to Setup Git for Unity

Having some form of version control setup is extremely important when it comes to game development or any type of software engineering.

It allows you to backup any progress you’ve made and also be able to revert to a previous version if you’ve happened to mess something up.

In a professional environment where you are collaborating with a team you will always be using some type of version control. Git is the most common.

In this quick guide I will show you how to setup Git from scratch on a Windows machine to work with a Unity project using Github for the remote repository . Whether you are just beginning the project or it has been in development for sometime does not matter. If you’re using Bitbucket instead of Github the process is pretty similar.

First head to the official git website and download the latest version.

https://git-scm.com/downloads

Go ahead and open the downloaded file to install. Github has changed the default branch from “master” to “main” so it’s probably best practice to use “main” instead of “master” these days. You can set the default name used to be overridden to “main” by selecting the option like shown in the GIF below, all other settings should be fine to leave as is.

When it is finished installing you can uncheck launch git bash and view release notes.

Now that git is installed head over to Github and create an account if you don’t already have one.

In the upper left corner there should be a button to Create a Repository. Click that. Now name it, you can also give a brief description of your project if you’d like.

When using git with Unity it is important to add a .gitignore file.

It will make git ignore unnecessary files, you can also edit this file after it’s on your machine to exclude other files you don’t want uploaded too.

Select the add .gitignore option, search for and select Unity.

You can now create the repository.

Now you will need to create a new Unity project if you don’t already have an existing one, then navigate to the main root folder of your project. An easy way to get there is to just right click your Assets folder within the Project panel in Unity and select Show in Explorer

You can change directories within git bash (git bash is the command line tool we installed in the beginning of this guide) but I find it easiest to just right click within the Unity project’s root folder and select git bash here and you will be right where you need to be.

Now that the git bash window is open type git init and press enter

This will initialize git and setup a local repository.

Now if it says (main) on the end of the title like mine then you are on the main branch and are good to continue to the next steps.

If not and it says master then I suggest using this command to rename it so there aren’t any problems with Github.

git branch -m master main

Now to connect to your remote repository on Github go to the repository’s page and click green Code button and then copy the url.

Now type git remote add origin and right click paste the url you copied above. Hit enter and then type git remote -v to verify that it worked.

Now this is a very important step, you always want to pull from the remote repository before you ever commit or push anything. Pulling ensures that you have the latest files from the remote and on first pull it will pull that .gitignore file that we added.

Type git pull origin main and hit enter.

Type git branch to make sure you are on main branch now

Now to see files that are different from remote and need to be staged type

git status

Instead of individually adding them all to be staged you can simply type

git add .

The period makes it add all of the files.

Now type git status again to see all of the files that have been added.

Now you can commit and give a description of the commit that will show on Github too.

Type :

git commit -m “Your commit description here

The description goes within the quotes.

Now you may push to the remote by typing:

git push origin main

It may want you to login to your github account, if there are no errors it should be all good. You can refresh the github repository page to verify that it did indeed push the new commit

You are now ready to work on your project.

Whenever you are ready to push changes to the remote always work in this order…

Pull>Commit>Push

So to make a new commit and push it to the remote you would just pull first then stage the files that have changed like this :

git pull origin main

git add .

Then commit with:

git commit -m “Your commit description here

git push origin main

These are just the bare minimum basics for setting up git to be used with Github and Unity from the git bash command line tool.

Unity Developer/C# Programmer for VR, 2d,3d Games and other Immersive Interactive Experiences