Implementing your First Repository in a Unity Project with GitHub — For Beginners
Objective: To create your first repository in a Unity project using GitHub.
Now that we have installed Git and GitHub and we have a basic understanding of what they are used for, it is time to create our first repository (repo) in a Unity project.
Head back to www.github.com and make sure to log in. On the left hand side of the screen we are going to press ‘Create Repository’ as seen below:
Now we are going to add a name for the repository (repo)and leave a meaningful description, so that if you come back to this months later you will have some idea of what this was created for. In our case we are simply going to name this ‘Test Repo for Unity’ and the description will be ‘This is a test repo for our project in Unity’. Then, let’s go ahead and change this to private just for the time being as we are simply running through our test repo (This can be changed later if required). Now, we are going to tick ‘.gitignore’ and when the drop down menu pops up, search in the filter for Unity and then press ‘Create Repository’. Essentially this .gitignore means that Git will ignore certain files in the repo’s root directory when you make a ‘commit’ (some of these files can be exceptionally large and don’t need to be saved onto the remote repo, including project assets).
Congratulations, you have created your first repo and also made your first commit with the .gitignore file!
Linking your repository to a local project
We are now going to link or add our web server to a local project in Unity. Firstly, let’s copy the URL by clicking the buttons as shown below:
Now we are going to open ‘Git Bash’ for the first time, so simply search for this on your computer using the search box in the bottom left or clicking on the Windows icon and navigating your way there.
What is Git Bash?
Git Bash is a application that was developed to provide users with a Git command line experience.
If you haven’t downloaded and installed Unity yet or are unsure how to complete this, make sure to jump over to this guide (Downloading and Installing Unity — For Beginners) before completing the next steps.
Open Unity Hub and create a new project called ‘Test Repo in Unity’. Choose your location where you want to save your project to by pressing the 3 dots symbol and then and press ‘Create’ when you are ready.
Alt-tab out of this and head back over to Git Bash. The first thing we are going to want to do is find the directory where we are going to create our repo (this is our newly created Unity project folder on our desktop). There are a couple ways to do this and I will show you the quickest way first just so we can get the directory name to help us get there utilising the command terminal. Simply go to your Desktop and and find the folder you created the Unity project in a right click here and press ‘Git Bash Here’. See below:
~/OneDrive/Desktop/Programming/GameDevHQ/Test Repo in Unity
The Git Bash command terminal window should have appeared and we now have our directory (as shown above). Your directory will be different based on where you saved your Unity project.
It is also good practice to get familiar with the commands within Git Bash, so I would highly recommend utilising the command line approach while you are learning. The command terminal approach to get to this directory is by using Change Directory ‘cd’ command followed by a forward slash, one by one as shown below in the next 2 images:
cd Test\ Repo\ in\ Unity/ -(the backslashes represent white space)
If you are ever unsure as to what your next folder would be, you can always use the command ‘ls’, ‘ls -a’ or ‘ls -l’. These stand for list (shows files in horizontal format), list all (shows files in horizontal format including hidden files like our .gitignore) and list long (files shown in a long format like columns). The differences are shown below:
Now that we have found our way to the local repo directory, in our command terminal we are going to type in ‘git init’ which is going to initialise an empty Git repository where we have directed it to as seen below:
In my example, I had already created a Git repo in this folder earlier so that is why is says ‘Reinitialized’ instead of ‘Initialized’.
Now we are going to link the remote server to the local project by adding the following line next (git remote add origin URL (that we copied earlier from GitHub)):
git remote -v verifies that we have successfully made the connection and as we can see we have permission to fetch and push to the server, so success!
Next up: Pull, Commit, Push Using Git Bash and GitHub for your Unity Project — For Beginners