Git Crash Course

Raymond Mills
6 min readJun 26, 2023

--

Objective: Learn the basics of starting a new repository, connecting Unity to GitHub using gitbash, and creating branches for non-destructive workflows.

Git Navigation

Download gitbash at https://git-scm.com

Typing git — help brings up a list of commands.
Use cd to change directories and navigate to your desktop using git.
Use cd to change directories and navigate to your desktop using git. Use {tab} to autofill partially typed commands. For example, cd des{tab} will autofill “cd desktop
Navigate to Unity Projects folder using the command cd unit{tab}y projects

Create a new Unity project

Open Unity Hub and create a new project.

Let’s call it Version Control Example

Make sure the project saves to the Unity Projects folder.
Navigate to the new project in git. First, note your current location.

From the Unity Project folder move to the Version Control Example

Type cd vers{tab}ion control example

Here’s a Shortcut

Open the Unity Projects folder on your desktop.

Right-click the Version Control Example folder. Select more options then select gitbash here

This will open gitbash at that location. (This is your unity project)

Create a new GitHub Repository

Go to https://github.com/

Name it Project Control Example and add a Git ignore: Unity

Connecting Github RepositoryTo Unity Project

If you haven’t yet changed your default branch from Master to Main

You can do this by typing thecommand git config -–global init.defaultBranch main
If your repository already has a master branch, you can fix this with git branch -m master main
Navigate to your Unity Project in gitbash and initialize the repository using the command git init
Copy your GitHub repository URL
Link the URL with git remote add origin <paste URL>
Verify this with git remote -v

Create a Main Branch

The command git branch will list all the branches in your current repository.

Use the command git pull origin main to create your main branch.

git branch should now show *Main in your Branch list.

git status shows what files need to be added from your pull

Now we need to add the necessary files from your GitHub repository

Git add <file name> to add each file individually.

Or, git add <period> to add them all at once.

git status again to make sure everything is ready to commit.

Pull, Commit, Push

You will pull from the origin server (the GitHub URL from before), commit your changes, then push those changes back to the server.

You should Commit Often!

Saving your work and creating a point that you can go back to; in case any problems should occur later.

We already did the pull, now it’s time to commit your changes.

Use the command git commit –m “created new unity project”

Commits are basically saving your progress or creating a restore point.

The commit is finalizing your changes to the project and -m will add a message or note to the commit for future reference.

Don’t forget the <quotations> around your message.

Next, git push origin main

This will push the changes to your main branch back to the GitHub repository (this is your origin)

Check this by going to https://github.com/ selecting your Version Control Example repository

You should see 1 commit with your note “created new unity project”
(I did 1st commit w/o -m)

Let’s do a couple More Commits

First, Create a cube inside your unity project and a C# challenge script.

To do this Open Unity Hub and select your Version Control Example project.

In your hierarchy window click the + go to Create 3d object>cube

Then in your project window click the + and select C# script this creates a folder.

Name the folder “challenge test

Save the project and return to gitbash

git status will show you that there are items that need to be added before your commit

git pull origin main to pull from the server if anything else has changed.

git add <period> to add all the files from the project changes.

git status to confirm that everything was added correctly.

git commit –m “added cube with challenge script”

git push origin main to push your changes back to the server

Go back to https://github.com/ selecting your Version Control Example repository and you should now see 2 commits

Branches

Create and Select Developer Branch

In gitbash, type the command git branch <branch name> this will create a new branch

Create a developer branch with git branch dev

git branch should now show *main and dev. The asterisk indicates the branch you are currently in.

git switch dev will take you into the dev branch

Alternatively, git checkout dev will do the same thing, but checkout is no longer listed as a command in the — help menu

More branches

Now create an inventory branch and a quest branch

git branch inventory and git branch quests

git branch should now show all four branches in your project.

Merge Branches

Changes made in one branch do not affect anything in the other branches making a non-destructive workflow for you and your team. When the changes are done and tested, you can then merge the branches so that the changes are applied everywhere.

To demonstrate this, navigate to dev branch in gitbash

Open Unity Hub and go to Version Control Example project and create a dev script by going to your project window click the + and select C# script this creates a folder.

Name the folder “dev script”

git switch inventory to switch branches

Go back to Unity and note that the dev script is not present, we need to merge branches in gitbash

Use git branch and verify that you are in the *inventory branch

Now with the command git merge dev you can merge the dev branch to the inventory branch

Now in Unity while in your in the inventory branch, dev script should show up in your assets in the

project window.

Now switch to dev branch note that in Unity the inventory script is not present.

git merge inventory to merge the inventory branch to the dev branch.

git add <period> to add all the files to your project.

When all these changes are ready for release git switch main to switch back to your main branch

git pull origin main to pull from the server if anything else has changed.

git merge dev to merge all the changes to your main branch

Finally, git commit –m “merged branches” and git push origin main to save everything to your GitHub repository.

Summary

These are the basic operations and commands you will need to master, in order to be efficient in navigating gitbash, creating and connecting a GitHub repository to your Unity project, and creating and utilizing a non-destructive work environment.

It’s a lot to take in, especially for someone like me, with no previous knowledge in coding. Just keep practicing these basics and soon we will all be Professional Unity Developers. Stay tuned for the next lesson.

--

--

Raymond Mills
Raymond Mills

Written by Raymond Mills

Unity Software Engineer, I am in an apprenticeship with GameDevHQ. I am taking the steps to become a professional Unity Developer.

No responses yet