Setting up Unity and Github
In today’s article, I’ll being showing my workflow on how to setup version control for Unity.
What is Version Control?
Version control is a system that allows multiple developers to contribute to a project. Typically, a server is setup where multiple developers can upload their changes to the source code to the server or download the latest changes from the server. In this guide, I’ll be using Github as the service that will host my files from a server.
Prerequisites
- Need a Github account. An account can be created here
- Have an empty Unity Project
Setting up the repository
On your Github account, you are going to select new
Afterwards, you will want to name your repository, add the Unity .gitignore
file from the presets, and hit Create Repository
A .gitignore
is used to exclude specific files from the Unity Project that aren’t needed for the project. This is useful if you don’t want specific files to be included in source control such as the auto-generated Visual Studio solution and project files.
After the repository is created, go ahead and copy the HTTPS link which will serve as the origin
or main location of where the files will be stored on the server. We will use this link copied in the next section.
Opening up Git Bash
Using Git bash, navigate to the folder that contains your Unity Project. In my case, I’m looking for VersionControlProject
Type in the following commands into your Git bash console:
$ git init
$ git remote add origin <PASTE_YOUR_ORIGIN_LINK_HERE>
$ git remote -v
git remote -v
is a command that will let you verify if origin is set to your remote server location. You can think of origin as a placeholder that holds the information of where the HTTPS link is stored that will let git know where to download (pull) or upload (push) files to.
Adding your first file to Github
Inside of your Unity Project, you will want to create a new file. Name it Example.
Going back to Git Bash console, type in
$ git status
This command will show you what files have changed or which files are brand new
To upload or push your changes to Github, these 4 steps should be followed:
$ git add --all
$ git commit -m "My first commit message"
$ git pull origin main
$ git push origin main
Note: Github recently changed the default branch name from master to main. More information can be found in the following link about the change: https://github.com/github/renaming. The previous image may show (master) but I have changed it to main branch by following the recommended naming convention by Github in the link above.
1. git add
git add --all
marks all your modified or new files in your branch to be ready for a commit. Running a commit on several files happens in the next step.
2. git commit
git commit -m "My first commit message"
packages all the files you’ve added in the previous step that is now ready to be pushed or uploaded to origin (Github server). -m
flag allows you to add a commit message surrounded in quotes. By convention, I like to follow present tense verb commit messages but you can choose whatever convention you’d like as long as it’s consistent and describes the change or feature being implemented.
3. git pull
git pull origin main
downloads changes that could have been made to the remote server from the main branch. It is highly advised to pull down the changes from the main branch before pushing your changes into main to prevent merge conflicts.
4. git push
git push origin main
uploads changes that were made in the commit to the remote server on the main branch.
Once your files have been successfully pushed onto the remote server, verify that you have your changes on your Github repository
Summary
- Learned how to create a Github repository
- Learned the basic commands to add, commit, pull, and push changes to Github
- Learned how to keep track of which files were successfully uploaded into your Github repository
- If you ever forget about any of the commands, please refer to this github cheatsheet
Thanks for reading!