GitHub and Version Control

To be honest, I didn’t even make separate saves. Just had to rely on Ctrl + Z!

A year ago when I started messing around with Unity, I kept seeing things pop up around version control and how essential it is. GitHub was the most common so I decided to check it out… It made zero sense.

I just completed a crash course, and in reality… it’s simple.

Let me explain.

GitHub is a site for hosting code. While not working in a team, it doesn’t seem essential. Who else needs to read my code?

Even if you’re riding solo, GitHub provides a great way to run version control on your projects, and now I’ve had a taste, it’s my new Ctrl + S!

Aside from the web page that appears from the quick google search you may have already done, there is an application available to download. The important part of this is GitBash — a command editor.

GitBash is used as a way to perform various operations on the files within our Unity projects. To connect, we access our list of files with ‘ls’ and then change our directory with ‘cd’ followed by where we want to navigate. Continuing this until our Unity project folder we want to work with. To create a Git Repository in the Unity folder, simply type “git init”.

From there we need to connect to GitHub. To do so, source the https address of your desired GitHub Repository and add a remote server connection within GitBash by using “git remote add origin” followed by the link.

Now we can begin with our three main functions of GitHub, Pull, Commit, and Push.

Firstly we want to Pull. This takes any code from our GitHub repository, allowing us to update our Unity Project with the latest version you or someone else has edited. To do so, type “git pull origin” followed by the name of the branch you desire… in this case, ‘main’ — more on that later.

From here any changes we make, we want to apply them to the server right? To do so, we need to add each change. We can check the status with “git status” to see what needs updating. From here type “git add” followed by each item you would like to add… Generally there is a lot so we can simply use a ‘.’ as our item name, which functions as an add all.

Now that we have our changes selected for updating the server, we need to Commit. To assist ourselves or others down the track, we should also add a message. The full line to use is “git commit -m ‘message here’”, the message allows us to easily track all of the updates to our projects.

Lastly, we need to actually need to forward our committed information to the server. To do so we use “git push origin” followed by the branch you would like to update.

Okay cool, so we can share projects with others. But how does this actually help version control? What if I want to create an idea to share without overriding the main project?

Branches allow us to create a folder hierarchy within our repositories. A separate branch for upcoming dev updates is a great example. Aside from that dev branch, we could also add multiple branches that feed in to it for all the new features, making it easy to commit what we want to the main game. There are numerous commands around branches, the two basics are: “git branch” followed by the name you would like to create, and “git switch” followed by the name of the branch you would like to access. With each branch, we can follow our Pull, Commit, and Push functions. Branch content can also be merged together, for example, when adding new feature branches to the dev branch.

If we want to backtrack. Previous commits can be accessed in a log. From there, we can “checkout” those commits to see what we changed. These can then also be turned into branches for easier switching.

It’s up to you how deep you dive in to the branch system. I know I’m going to love diving in and out of my project versions!