Setting up Git for Unity
In this article I will show how to set up Git with a repository and Unity.
Git is a version control system that is invaluable for software development projects. Not only does it allow you to backup your assets to be recovered or worked on from another workstation, it also allows versions control and branching.
Version control is important as it allows previous versions of a project to be recalled at need, and provides a working history of the development. Got a major bug that crept up in the last few days or weeks? Head back to an old commit to see if you can fix it or discover the cause.
More on commits further down. Lets get set up.
The first thing you will require is a repository. I’m going to use Github for this article but you could whichever you prefer.
Head over to Github.com, create an account, and you will end up with the below page:
The first thing I’m going to recommend you do is click your profile picture in the top-right and click ‘Settings’. On the navigation menu on the left side, click ‘Repositories’.
Change the default branch name from ‘main’ to ‘master’ and click ‘Update’. You don’t have to do this step but I prefer master and it saves changes in the Git setup coming up.
Head back to the main Github page and click ‘Create repository’ in the top-left. Give your repository a suitable name and description. I’ve used ‘Version-Control’ for this example. Set the repository to ‘Public’ or ‘Private’. Public is easier to share and Private will prevent anyone accessing the repository unless you expressly allow it.
Further down the options you will find ‘Add .gitignore’. Check the box and from the drop-down select ‘Unity’. This is an important step as it will automatically ignore files that are specific to Unity projects that are not required to be committed to the repo.
After this you can click ‘Create’ repository and that’s it! We have our repo.
Time to install Git itself. Head to http://git-scm.com and download the latest source release; it’s the monitor image on the right-hand side below.
Once downloaded, start the installation. If you changed the default branch name to ‘master’ in Github then you can click through the setup with all the default options, otherwise watch for the options for default branch name and change accordingly.
Git has a GUI but I prefer to use the command line version, Git Bash. You can navigate within Git Bash in a similar way to the Windows Command Prompt or Linux Terminal, but I will cover those commands in another article.
For now, I will show you the quickest and easiest way to start Git Bash in the right location. First, we will need that location. Open Unity Hub and create a new project; again, I’ve used Version Control for this example.
Once the project is created, navigate to the project’s folder as below. Right-click in the empty space and click ‘Git Bash here’.
This will open the Git Bash CLI automatically to the right location, ready for the final part of the setup.
When Git Bash opens you will see a window as below:
The first thing we need to do is initialise a Git repository in the project location. To do this type the command ‘git init’ and hit enter/return. Commands in Git Bash must start with ‘git’.
Next we need to setup the git server location in Git. To do this, first we need to go back to Github and in to the repo we created. On the right-hand side, click the green ‘Code’ button, then copy the URL there. We’ll need it for the next step.
Back in Git, we’re going to use the command ‘git remote add origin <URL>’ where URL is the link we just copied. To paste, right-click then click ‘Paste’. This will add the server of name origin to the repo. Origin is the industry standard name for the main server, but you can technically call it what you like. It’s then a good idea to verify the remote with the command ‘git remote -v’.
Now that we have linked the repository and verified it, it’s time to actually commit something.
The golden rule for using Git is to always pull, commit, push. Pulling will update the local repo with the latest files, then you commit your changes, then push them to the server. If you do this, you will rarely have issues.
First up, pull from the repo with the command ‘git pull origin master’. This is telling Git to pull the files from the origin server on the master branch.
As you can see from the above screenshot I have also verified the branch I am working on with ‘git branch’. Now that I have pulled from the repo, I want to add my files then commit. First we need to use the command ‘git add .’ (note the full-stop/period). This adds all the changed files to the commit. You can add individual files but first using ‘git status’ to show the files that are yet to be committed, then using the same command but with the filename instead of the full stop/period.
After this we need to commit the changes with ‘git commit -m “The first commit!”’. This commits the changes with the message (-m) of ‘The first commit!’.
You may be asked to provide the Github sign-in details for your account the first time you do this. Follow the commands to do so.
Once you have done this, enter the commit command again.
Finally, we need to push the commit to the server with the command ‘git push origin master’. Again, if you had to enter the credentials this time, you will now be prompted to sign-in to your Github account.
And that’s it! We’ve set up Git for our project and performed our first commit. If you check the repo on Github now, you will see the commit and the files we have pushed to the server.
In the next article I will go into branches and branching.