Setting up Git for Unity in macOS

Jaime
8 min readApr 17, 2021

--

Git is a version control software that you use to keep track of the changes of your software. This allows you to experiment with your code changes and easily roll back if it ends up in disaster. Aside from danger, versioning makes it easy to differentiate among the features, bugs identified and bugs fixed from version to version.

Also, if your software is shared with other people, they can easily work on different components of the software on their end and request to merge the changes back with the original code. This helps the software have new features and bug fixes to help evolve and make the project more mature.

To be able to easily share the code, it needs to be uploaded in a repository. The popular repositories that provide free and commercial plans are GitHub and BitBucket.

For this article, I will attempt to use Git for Unity in macOS and upload the project source in GitHub. First, sign up with GitHub:

Sign up page

Once you’re able to create your account and login to the website, you can create a new repository by clicking the New button:

Click the New button to start adding a new repository.

In the next page, you can now fill up information when setting up the repository. By default, you simply just need to supply the name of the repository. However, you should also provide a Description, whether you are going to share this code or keep it private, detailed description in the README file, .gitignore to ignore certain files or directories, and license should anyone else use your code. For Unity, make sure that you select Unity as the .gitignore template to not upload large files that can be autogenerated or logs that may contain sensitive information. Let me repeat that this is a template, so you may want to add your own sensitive custom directories or files in .gitignore to make sure it’s not available for the public to see.

Creating a new repository just requires a repository name but make sure to include the Unity template in .gitignore

After clicking the Create repository button, the repository will be created and the website will redirect you to the repository’s home. Click the Code button and then click the clipboard icon to grab a copy of the web URL of your project. You will need it when you upload your project to GitHub and when you download the project in future as well.

The landing page of the newly created repository

Notice that the default branch is called “main”. It used to be called “master” but software companies are slowly changing these terms to remove slavery references. That being said you might see the word master from time to time for older versions or for current versions that have not been updated yet.

It’s time to install git in macOS. If you install Xcode, git will already be installed but as of the time of writing the version is 2.24.3. I want to use the latest version. I’m able to install the new version with the package manager, brew via commands brew updateand brew install git.

Updating brew to be able to install the latest versions in its repository
Installing a new version of git via brew install

Finally, you need to edit the account you will use for commit operations and to upload/download files to GitHub. Please run git config --global --edit and add your email address and name to the configuration and save.

Now that it’s installed and configured, let’s create a new project in Unity. Open Unity Hub and click the New button. It will open a dialog where you can specify the Unity template, Project name and location of the project in the filesystem. For now, I will use 3D as Unity template and call the project “test project”. Take note of the Location as we need to go to that directory later.

Now that the project is created, we will need to integrate git for software versioning and also provide the online repository we created awhile ago. You will need to run git init and git remote add origin <GitHub URL>.

$ cd "/Users/developer/Workspace/test project/"
$ git init
Initialized empty Git repository in /Users/developer/Workspace/test project/.git/
$ git remote add origin https://github.com/<username>/unity-test-repository.git

The next step is to first pull project data from the online repository to your local directory. We do this by running git pull origin <repository>and for this case the repository is main.

$ git pull origin mainremote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/<username>/unity-test-repository
* branch main -> FETCH_HEAD
* [new branch] main -> origin/main

If this is the first time you have run git, then you’ll need to provide your credentials. It would be best to enable two factor authentication so that it would be harder for someone else to impersonate your account.

To verify the current branch you are using, run git branch:

Note: As discussed above, the term main was used to be called master.

However, master should no longer be used and the repository name created in GitHub is main. We can create a new branch in our local directory to be called main to match it by running git branch main.

Use git branch <branch name> to create a new branch

To switch to the main branch run, git switch main:

$ git switch main
Switched to branch 'main'

Also, with ls -al we can see that .gitignore which was in your new repository has been downloaded to local directory:

ls -al will also display hidden files and directories in your current directory

The next step is to upload your project in your local directory to the online repository. To know which files and directories will and will not be synced to the online repository, run git status:

Run git status to determine which files and directories have been updated or added and may or not be included in the next commit

If you noticed, I actually changed the contents of .gitignore file to add the directory .vscode. The reason is that I’m using Visual Studio Code for editing C# files.

You can simply run git add <file or directory>to include the untracked files. In our case we will add .gitignore, Assets, Packages and ProjectSettings.

You can use git add <file/directory> to add files for commit and run git status to know which files will be included in the next commit.

Alternatively, you could have just run git add .to conveniently add all changes in the parent directory.

Now, it’s time to make the initial commit that includes the initial file in the repository and the Unity related files created when you created a Unity project. To commit the changes run git commit -m “<commit message>":

$ git commit -m "Initial commit"
[main c2c8a97] Initial commit
25 files changed, 2322 insertions(+), 1 deletion(-)
create mode 100644 Assets/Scenes.meta
create mode 100644 Assets/Scenes/SampleScene.unity
create mode 100644 Assets/Scenes/SampleScene.unity.meta
create mode 100644 Packages/manifest.json
create mode 100644 Packages/packages-lock.json
create mode 100644 ProjectSettings/AudioManager.asset
create mode 100644 ProjectSettings/ClusterInputManager.asset
create mode 100644 ProjectSettings/DynamicsManager.asset
create mode 100644 ProjectSettings/EditorBuildSettings.asset
create mode 100644 ProjectSettings/EditorSettings.asset
create mode 100644 ProjectSettings/GraphicsSettings.asset
create mode 100644 ProjectSettings/InputManager.asset
create mode 100644 ProjectSettings/NavMeshAreas.asset
create mode 100644 ProjectSettings/PackageManagerSettings.asset
create mode 100644 ProjectSettings/Physics2DSettings.asset
create mode 100644 ProjectSettings/PresetManager.asset
create mode 100644 ProjectSettings/ProjectSettings.asset
create mode 100644 ProjectSettings/ProjectVersion.txt
create mode 100644 ProjectSettings/QualitySettings.asset
create mode 100644 ProjectSettings/TagManager.asset
create mode 100644 ProjectSettings/TimeManager.asset
create mode 100644 ProjectSettings/UnityConnectSettings.asset
create mode 100644 ProjectSettings/VFXManager.asset
create mode 100644 ProjectSettings/XRSettings.asset

The next step is upload the commit to the repository. To do so, run git push origin <branch>. In this particular case we will push the commit to the main branch of the unity-test-repository.

$ git push origin main
Enumerating objects: 33, done.
Counting objects: 100% (33/33), done.
Delta compression using up to 4 threads
Compressing objects: 100% (31/31), done.
Writing objects: 100% (31/31), 16.56 KiB | 2.76 MiB/s, done.
Total 31 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To http://github.com/<username>/unity-test-respository.git
49dd9a8..c2c8a97 main -> main

If we check the landing page of the repository we can see that the files in your local directory have been uploaded in the main branch.

The repository now has the project files from your local directory.

To test versioning, let’s try adding two cubes in Unity and Save.

Let’s add the changes to the next commit.

$ git add .
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: Assets/Scenes/SampleScene.unity
$ git commit -m "Added two cubes"
[main 7f03331] Added two cubes
1 file changed, 484 insertions(+), 259 deletions(-)

Now let’s add 4 spheres in Unity and Save.

Added four spheres

Let’s commit the changes of adding 4 spheres:

$ git add .
$ git commit -m "Added 4 spheres"
[main 292f9f2] Added 4 spheres
1 file changed, 372 insertions(+)

If you want to rollback right before the latest change, you can take a look at the commit log by running git log:

$ git logcommit 292f9f20a0bda705337535eef912e6b51a394b14 (HEAD -> main)
Author: Jaime <jaime@example.com>
Date: Sun Apr 18 03:03:47 2021 +0800
Added 4 spherescommit 7f0333139dcc658e3a83e03501e415bacffdab90 (HEAD -> main)
Author: Jaime <jaime@example.com>
Date: Sun Apr 18 02:54:49 2021 +0800
Added two cubescommit c2c8a979ef514a0c9e2b4164f076720ab04fa46e (origin/main)
Author: Jaime <jaime@example.com>
Date: Sun Apr 18 02:49:21 2021 +0800
Initial commitcommit 49dd9a83b022c02dd02e31d9ab3678a1571dd9d7 (master)
Author: <username> <jaime@example.com>
Date: Sun Apr 18 02:37:38 2021 +0800
Initial commit

In this case we would like to revert back to 7f0333139dcc658e3a83e03501e415bacffdab90 when it only had two cubes. We would need to run: git checkout <id>

$ git checkout 7f0333139dcc658e3a83e03501e415bacffdab90
M Assets/Scenes/SampleScene.unity
Note: switching to '7f0333139dcc658e3a83e03501e415bacffdab90'.
You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by switching back to a branch.If you want to create a new branch to retain commits you create, you may do so (now or later) by using -c with the switch command. Example:git switch -c <new-branch-name>Or undo this operation with:git switch -Turn off this advice by setting config variable advice.detachedHead to falseHEAD is now at 7f03331 Added two cubes

When we check the Unity again we can see that the project reverted back to when it only had two cubes.

The project reverted back to the commit where only two cubes were created.

Now, you can see the power of Git to revert changes if bugs were introduced or worse if the changes broke the program.

There are other features of git where you can create several branches to allow other people to handle different components within the project and merge them into one release.

Given that this is already a long post for getting started with Git for Unity in macOS, I will end it here.

Thanks for reading my post.

--

--

Jaime
Jaime

No responses yet