Let’s unpack the title first. What is Git? It’s a command line interface used for Version Control.
What is the meaning of Version Control?
Version Control is the process of tracking and managing changes made to files over time. By using Version Control, you can see what files changed, who made the changes (if you are working with a team), what those changes were, and you can even roll back to a previous version of your project.
Now, to set up Git for Unity, there are few things you need. You’ll need to go to download Git from here. And, you’ll need to create a Github account and repository.
Open the download, and the installer will pop up on your screen. As you go through the installer, make sure you have Git Bash Here enabled, under the Windows Explorer Integration header.
Make sure Git Bash Here is checked.
For the rest of the setup installer, it is okay and advised to use default settings.
Now, to set up Git for Unity, navigate to the folder where your Unity Project resides and right click and select Git Bash Here. It will open Git Bash and navigate to the folder you are currently in.
Right-Click in the desired folder and select Git Bash Here to open the terminal to that folder.
Once your Github account is created, navigate to repositories and click the green button that reads “New”.
Create a name for your repository and a description.
Now you’ll want to add Unity to the .gitignore.
The reason for this is because there’s some files in Unity (library files and temp files) that we simply don’t need to commit.
Now, click Create Repository and you will have successfully created a repository. But our work is not done yet. We still need to link our repository to our Unity project through Git.
To do that, you’ll need to copy the link to your repository.
Copy the link to your repository.
Now, return to Git Bash. You should still be in the directory where your Unity Project resides. Here, we will initialize git to begin tracking this project.
In Git Bash, type “git init”. It is worthwhile to note that every command within Git Bash will begin with “git”.
Type “git init” to begin tracking this project.
Next, we will add the web server to our repository. We’ll type in “git remote add origin [paste URL copied from Github].
git remote add origin [paste URL copied from Github].
We’ve just added a remote server, origin, at the URL we copied from Github. It is common to name the master server “origin” in the industry.
To verify, in Git Bash, type “git remote -v”. The -v is to verify. This command will show us that we have permission to fetch from the origin and to push to the origin.
Now we are almost ready to create our initial commit. A commit is almost like a timestamp that says, “this person, on this day, modified this file in this way.”
But we must be mindful that in working with a team, there may be many people who are making commits. As a best practice, we always want to PULL from the server, COMMIT with a message, and then PUSH, in that order. PULL merges changes on the server. FETCH downloads information from the server. A COMMIT is our changes that we want to send to the server. And PUSH sends the commits to the server.
For our initial commit, we will type “git pull origin master”. We’ve covered that “git pull” merges changes on the server. The “origin” is what we’ve named the remote server. But what is master? Master is the name of the branch we are currently working on.
Like a tree, our server can have one branch or many branches. In this way, we can compartmentalize certain functionalities while they are in development and when they are ready / functional, merge them with the master branch.
Creating our initial commit.
In response, Git Bash tells us that we pulled from the branch “master” from the latest commit: “FETCH_HEAD”. Head just means the latest or most recent commit. And we are currently up to date. Now, type in “git status” and hit enter.
Typing in git status shows the project status.
The “git status” command shows files, that are not currently being tracked, in red. To add those files to the commit that we are about to make, you can type them in one by one by using the “git add [file/folder name]” or all at once using the “git add .” command.
Now we are ready to commit. Type in “git commit -m “[type message within these quotations]”.
For the message, it will be what we’ve done, which is “created a new unity project”.
Creating our first commit.
But now, we need to PUSH this commit to the server. Type in “git push origin master”.
Pushing our commit to Origin.
This means we pushed our initial commit to the origin server to the master branch. If we refresh Github, we should now have source code on Github.
This concludes the tutorial on setting up Git for Unity.
Thanks for stopping by.