Journal 01 — First Setup of Git with Unity

Chris Nielsen
8 min readApr 21, 2021

--

This article is based on a tutorial that is part of the GameDevHQ Professional Unity Developer Program.

  1. Create a new Unity Project through the Unity Hub. For this example, we will call the project “Version Control”, and select 3D for the type of project. In this example, we open the project with a single 3D cube in the Editor.
Unity Hub

2. Sign up for a membership at https://github.com.

One housekeeping item you should consider is the naming convention of your repositories, whether they are designated “main” or “master”. See this discussion: GitHub’s default branch naming convention | by Siddhant Thakur | Mar, 2021 | Medium. For this example, we have selected our “Settings” via the user icon in the upper right of the GitHub site layout. From there, we select “Repositories”, and change the default name from “main” to “master”.

GitHub User Options
GitHub Settings — Repositories

3. Install the Git Command Line interface software from https://git-scm.com/downloads.

Git-Scm downloads

There are several installation screens with options during the install process. Keep the default settings and click through to completion.

Open Git Bash.

OR, alternatively, navigate in Windows Explorer to the storage location of your Unity Project. From there, right-click the area and select “Git Bash Here”

Git Bash Integration to Windows Explorer

For a primer on Git Bash commands, see the following: https://training.github.com/downloads/github-git-cheat-sheet.pdf

4. At GitHub, select “Create Repository”. For this example, we will use “Version-Control-Example” as the name.

GitHub website

Add a “.gitignore” template by searching for “Unity”. This is to prevent certain files used by Unity such as library and temp files from the collaboration process (“pull”, “commit”, “push”), which we will cover later.

GitHub Repository Creation Screen

5. Link the GitHub repository to the local project. On the Repository screen in GitHub, click the “Code” button to bring up a web server link. Right-click and copy this URL, or click the clipboard icon to copy the link.

GitHub Repository URL

in Git Bash, you need to first initialize and track the project. Type: git init.

Git Bash Initialize

After initializing, you need to add the webserver link to communicate between the local project and GitHub. Type the following: git remote add origin URL…… (the name “origin” is a typical industry name for your remote server).

Git Bash Add Webserver

Confirm the connection to the origin by entering the following: git remote -v.

Git Bash test remote

6. Time for your first Commit through Git Bash! Remember the following steps as described in the Git Bash cheat sheet:

Pull → Commit → Push

GitHub Cheat Sheet 1 of 2
GitHub Cheat Sheet 2 of 2

In Git Bash, see commands by typing: git (dash)(dash)help (darn auto-formatting…)

Git Bash Commands

Perform an initial Pull from the Origin with the following command: git pull origin master. You can also type in: git branch, in order to confirm there is only one branch configuration on your repository (more on this later).

7. Now check the status of files in your local project by typing: git status. Files in red are untracked.

Git status

Add all the files shown by type: git add . ← Period means “all”. Check git status again.

Git Status (after Add)

Now to Commit these files, include a message for the changelog by typing: git commit -m “_____________” ← changelog message in quotations. In this example, we will say: git commit -m “Created new Unity project”

Git Commit message

8. Time to Push the revisions to the server. Type: git push origin master

Git Push results

Check the results on GitHub.

GitHub Repository Status (refresh)

As a challenge, try the following: open up the Unity Project in Unity, add an object to the scene, or add a generic C# script, something that changes the scene. Save the Unity Project. Now in Git Bash, repeat the previous steps:

Pull → Status → Add → Commit → Push

As an example, I will add another cube and generic C# script CubeMove and document these changes.

GitHub updated status

9. Branches — Use of branches is a best practice. See the following diagram from the GitHub Cheat Sheet. Branches can be used to split up the workflow into a developer environment in order to prepare all the changes internally before merging into a final finished project. Or branches may be created for different work groups that are part of a project team (e.g., “Quests” team, “Inventory” team, “UI” team, etc.).

GitHub Cheat Sheet Flow

Branches may be created in Git Bash with the following command: git branch ______ ← branch name

Git Bash Branches (list and add new branches)

To switch to a different branch, use the “switch” or “checkout” statement. In this case, we will switch to the dev branch.

Git Bash switch or checkout to different branch

Now while we are in the dev branch, let’s make some changes in Unity. For example let’s add a couple spheres.

Unity Editor, added two spheres

Let’s save Unity and add the updates and commit while in the dev branch.

Let’s switch back to the master branch, and look at the Unity Editor again. As you see, those changes made in the dev branch are not shown in the master branch.

Git Bash switch to master branch
Unity Editor (switch branches)
Unity Editor (after switching back to master branch)

10. Merge branches — This function can combine the revisions made in branches. In the example above, we made changes to the dev branch, but these are not visible in other branches. From the example above, let’s merge the changes made in the dev branch to the Quests branch.

Git Bash merge command

And while in the Quests branch, let’s check Unity.

Unity Editor (after merge function)

Now let’s make one more change while in the Quests branch by adding in a generic C# script, then repeat the steps described above:

Unity Edits (save) → git add . → git commit → Switch branch to master → Merge other branches → git push origin master

Unity Editor (added C# script in Quests branch and saved)
Git Bash Add, Commit, Merge, and Push process
Git Bash merge Quests with master
Git Hub Commits (all edits merged to master)

11. Revert — Consider this option if you need to revert your project to a previous state. To see all your previous commits, you can type: git log

Git Bash log

The commit instances may be added as their own branch, in order to confirm with the team the correct instance to work from. With this workflow, the work progress is not lost. So for this example, let’s create a branch from the “Add cube and script CubeMove” instance and investigate. To do this, you copy the commit instance tag, and add that as a branch.

Git Bash, copy and paste a Commit tag
Git Bash log, and creation of new branch based on a previous Commit

Note in the above line for “git checkout”, we are using “-b old-project-state 26f979…” This is an option to rename the long commit tag into a more user-friendly branch name.

12. Reset — Consider this option of if you need to reset the project to a previous state. Note this will wipe the revisions made after the selected reset point. For this project, let’s reset the project to when the project was first created in Unity.

Git Bash Log

Copy the commit tag and type: git reset (dash)(dash)hard _____ and see the results in Unity.

Now these changes need to be forced onto the Web Server origin. Type: git push (dash)(dash)force origin master

Git Bash (forced update to server)
Git Hub status (after forced update)

--

--

Chris Nielsen
Chris Nielsen

Written by Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.

No responses yet