Saving Your Unity Project From Disaster

What is Git?

Git is a Version Control System (VCS) that allows commits of your code at certain points into a code repository, and importantly, allows you to retrieve code from previous commits in case you have somehow wrecked your project. So Git Started and go wreck some code! I mean… create a repository.

Objective: Create a repository. I’m assuming you have a Unity project. Open the Git Bash terminal.

If you don’t currently have a project, use the Unity Hub to create on. I keep all my projects under a folder called “repos”. In the terminal, change to the folder that holds your Unity project. At this point, we initialize the repository. It’s good practice to make sure you are not in a repository already by typing “git status”. If you are not in an existing repository, you’ll type “git init”.

Note: It is a good idea to put a Unity specific version of .gitignore into the project’s root directory. This will keep Git from adding files to the repository that don’t need to be there, such as log files and other files that will be recreated. You can find a currently updated version at github .

Objective: Add files from the working folder to the staged workspace.

This is an intermediate step before committing the files to the repository. When looking at “git status”, you will see the staged files. Files can easily be unstaged if you make a mistake.

Objective: Make a commit

The final step to getting your code into the repository is to make a “commit”.

Objective: List your commits. In the image below, I have made 2 additional commits. I then used the command “git log — oneline” to see a list of them.

The Head->Master is next to the current commit at this point. The 9 alpha-numeric characters in the left column are unique identifiers for each commit. We will need these for recovering from a programmer mistake!

Objective: Go back to an earlier commit.

There are a number of ways to go “back in time” using several git commands. Here, we will use “reset — hard”. You will pick a commit to go back to, and you will lose any changes that were made since that commit. This may work in your case, but there are other options that will preserve changes to varying extents. Here’s an example of a hard reset. First, I chose “git log —oneline” to show me all of my commits and the hash codes. You will notice that after the command I put the code from the first commit. After execution, we only have one commit (the commit we selected) and anything before that. The intermediate commits are gone. If you just need to go back because you made an error you can’t correct, this is a good option.

We have only covered the case of a solo developer using Git on a local machine. A copy of the repository can be kept in the cloud (on github, for example), used for backup storage or collaboration. There are several 3rd party tools that can be used in place of the command line. And, as mentioned, there are many other ways to return to previous commits. We will examine each of these in follow-up articles.

Where to find Git:

Gitting started (ok, enough Git jokes, right?)

Download and install the appropriate version of Git from here . As of this writing, version 2.31.1 is the current Windows version. I am using a Windows machine, but there are instructions for Mac and Linux installations as well. When installation is complete, you should have an item in your start menu called “Git Bash”. This is how we will access Git in Windows.