Git is a free and open-source version control system that allows you to collaborate and maintain your code. Git is a must, whether you are working alone or on a team.
Getting Git and GitHub to work with Unity takes just a few simple steps.
Step 1: Download and install Git
Go to the Git website and pick the version of Git that you need for your computer (i.e., Windows, Mac, or Linux); then, once downloaded, run the executable and install Git.
The only setting that you may need to change from the default is Adjusting the name of the initial branch in new repositories that you want to be “main” since GitHub now uses it as the default name. It will save you time later; once the installation is complete select Launch Git Bash and click on Finish.
Step 2: Initialize Git in your Unity Project
Open the Unity Project that you want to use with Git. Then once you have the project open, launch Git Bash either through Step 2A or 2B.
Step 2A: Command Line Route
Launch Git Bash from the desktop shortcut or by searching for the app. Once it is open, you will need to navigate to the Unity Project folder. In Git Bash, type [ ls ] to list the files in the current folder. Since my Unity Projects folder is on the Desktop, I have to go into the Desktop first to find the folder. To do so, type [ cd Desktop/ ] (“cd” stands for change directory), and you will now be in the Desktop. Alternatively, you could have typed [ cd desk ] and then hit the Tab key to autocomplete the command.
Next, type [ ls ] again to see what files are in the Desktop directory. For me, the Unity Projects folder is on the Desktop, so that is the next place to go. Typing [ cd “unity projects”] will search for unity projects regardless of capitalization and set Unity Projects to the current directory. Now type [ ls ] again to show what is in the Unity Projects folder and type [ cd Version\ Control\ Example/ ]. In Git Bash, with it on your Project folder as the current directory, type in [ git init ]; this will create a local repository that we will link to the GitHub in a later step.
Step 2B: Straight Forward Route
The alternative to finding the project folder through the command line is to go in the project folder, right-click in an empty space and click on Git Bash Here. That will open Git Bash with it already having the project folder as the directory. Now type in [ git init ]; this will create a local repository to be linked to the GitHub one in a later step.
Step 3: Create a GitHub Account:
Go to the GitHub Website and click the Sign-Up button and create an account.
Step 4: Create a GitHub Repository
Sign in to your GitHub account, if not already, and then click the New button on the left side of the page to create a new repository. Once the new page has loaded, give your repository a name (usually similar to the project name). Then go down to Add .gitignore toggle it on, and in the drop-down menu, select Unity to add a setting file that will tell Git not to copy some unnecessary files from the Unity Project folder. Finally, click the Create repository button to create your new repository.
Step 5: Link GitHub to Unity Project
On GitHub in your repository, click the Code button and copy the URL in the text field. Next, in Git Bash, type [ git remote add origin <URL> ] (with <URL> replaced with the one you copied earlier). This command adds a remote repository to Git in the project folder by the name of origin, a standard naming convention in the industry. Now that it is linked, type [ git remote -v ] to ensure you have the fetch and push permissions.
Step 6: Make your first Commit
Now it is time to make the first commit so that all the files in your local repository and the GitHub repository are identical. That means you will have a backup if your local files become unavailable for whatever reason, and you can now share your project quickly with others.
The first thing you always want to do is get any changes from the GitHub repository and merge them into your local repository. That is also known as Pulling the changes. To do that, type [ git pull origin main ]. The only thing pulled from GitHub is the git ignore settings created when you created the repository. Now that all the origin changes are on the local repository, let’s check what is different from the GitHub repository to send the new files in the project to GitHub.
Type [ git status ], everything in red is either not in the GitHub repository or changed locally and needs to be updated. To add these files to the commit, type [ git add . ] and now all the files have been added to the commit. Using the “.” after “add” tells Git Bash to add all the files to the commit. You could go through and type in the name of each file that was red in the status check one at a time, but unless there is a reason you do not want to add everything, it is a big time-saver to add all of it at once. Now that you added everything to the commit, it is time to create the commit, to do that type [ git commit -m “First Commit”]. That has made a commit with the message “First Commit” to be easily identified in the GitHub repository.
Now that the commit has been created, it is time to send or “push” it to the GitHub repository. To do so, type [ git push origin main ], and once it is finished uploading, you can go to GitHub and see that now it has all the files from your project folder, and you are ready to begin developing!