Part 2 — Getting a grip on Version Control using Git
This is the second article in a series covering my introduction to Git and how to use it in conjunction with GitHub and Unity3D.
Part 2: Let’s get GitHub and Unity3D talking!
So now that have Git installed on your local machine, how do we use it to communicate and establish version control between your Unity project and a remote server?
Let’s begin by signing up for a GitHub account. Navigate to the following link:
Select “Sign up”, fill out your desired username, email, and password, along with preferences and verification process, then select “Create account”. With this process complete, you now have a remote server to which you will be able to save your project. This location, which is called a repository, can be visualized as a folder into which your project and all its associated files will reside. It will be from this repository, or “folder”, that other contributors will pull (or clone) the project unto their own workstations, or push their contributions back to this repository for eventual merge into the Master (or Main) project.
But wait! We need a project! Create a new project on your workstation via Unity Hub (I will assume that you already have Unity installed and running on your local machine). Now that this step is done, I will walk you through the method on how you can connect your GitHub repository (remote origin) to the Unity project.
Now go back to GitHub, log into your account, and create a new repository. Give the repository a name, a description, make it “Public”, add .gitignore “Unity” to avoid saving unnecessary files, then “Create repository”.
Copy to your clipboard the URL of the new repository you’ve just created on GitHub.
Now you need to link the project to your local machine (I’m my case, my iMac). In order to do so, go back to the terminal window on your local machine and navigate your way to the location of your Unity project folder using terminal commands such as “cd” (change directory), or you can right-click on the project folder and select “Services” and “New Terminal at Folder”.
Type “git init” to initialize the git repository in your project folder.
In Terminal, type “git remote add origin <pasted URL from GitHub>”. This will link the GitHub server to your local machine.
Type “git remote -v” to verify that your local machine is communicating with the GitHub server.
Typing “git branch” will show which branch you are currently working on if multiple branches exist (shows up in green).
To make commits (changes), do the following steps:…
Type “git pull origin master” to pull the data from the origin branch you wish to work with.
Type “git status” in order to check that branch (Master) data for any changes. Untracked items will show up in red.
Type “git add .” to add these changes to the branch. The “.” adds all the untracked files. Typing “git status” to confirm (modified/added files will be green).
Type “git commit -m “comment”” in order to add a comment and commit the changes to the branch on your local machine. These files are now white. Typing “git status” to confirm.
The final step is to type “git push origin master” to push (merge) the modified branch from your local machine to the origin branch on GitHub. In this case, we only have a single branch (Master) and are pushing all our files to the Master branch on Github.
Once this push is done, the source files from your local machine (new Unity project) will now show up in the GitHub repository. There will also be nothing to commit on the Master branch on your local machine (see Terminal window below).
This about ties it up for this article. In part 3, we will look into manipulating branches. Thanks for reading! :)