Git Crash Course
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
Create a new Unity project
Open Unity Hub and create a new project.
Let’s call it Version Control Example
From the Unity Project folder move to the Version 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
Create a new GitHub Repository
Go to https://github.com/
Connecting Github RepositoryTo Unity Project
If you haven’t yet changed your default branch from Master to Main
Create a Main Branch
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”
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
Check this by going to https://github.com/ selecting your Version Control Example repository
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.
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 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
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.