Objective: Quickly set up a GitHub repository for a Unity Project
In this tutorial I’m going to show how to get your Unity Project GitHub-ready, in a short time.
Step #1 — Create a Unity Project
I suggest the use of Unity Hub. In the project view, just click NEW, then choose a template, a name and a location on your computer. Then CREATE.
Step #2 — Create a GitHub Repository
Now n your GitHub account! Starting from your home page, right in middle you will find a list of tabs, click on Repositories and then on NEW.
After that, you have to fill in some information of your repository.
First choose a name and set Public visibility. The name I chose was version-control-exercise.
Always use clear, catchy names capable of giving a rough explanation of the repo content.
Then, click on the .gitignore checkbox. This will create a file (.gitignore, which you can also modify) containing info of specific files, folders or file extensions are not to be tracked. In order to choose a template for this information, you can click on the drop down menu and search for a Unity template. Easy peasy.
Click on CREATE REPOSITORY and you’re done!
As you can see, I ignored (shame on me) the fact that GitHub will create a “main” default branch. I don’t like it, as you can see further.
Step #3 — Initialize the Repository
It’s time to get the real stuff done: some good old commands in a command line. You can use whatever you want: cmd, PowerShell, bash (upon installing git on your computer). I prefer PowerShell, but just because I do other stuff apart from git commands. Don’t worry: it’s the same.
First things first: n your GitHub repo page, click on Code. An URL appears. Copy that for later. It can be either used to clone the repository (and downloading it to your machine) or for the initialization.
Navigate to your local Unity project folder and then launch the git init command.
After that you have to tell git where is the remote repo by calling git remote add origin “URL”
Thanks to this command, from now on you can refer to you remote repo (placed at that URL) by simply using origin keyword.
Step #4 — Rename default ”main” branch to “master”
As I mentioned before, GitHub created a default branch named “main”. I don’t like it, as I usually work with a default “master” branch.
… and also because after your git init command, you will find a master branch in your local repo :)
To correct this, go to your repo page, click on branch to get list of branches, and then click on the pencil on the right of main branch: you can now rename it to master.
Now launch git pull origin master, this will create a local branch named master, with the content of remote master branch.
To check if everything is ok, launch a git fetch origin command and it should appear like this:
Step #5 — Set tracking
I am lazy. I could push and pull by typing every time the name of the branch… But also no. Let’s set up tracking, in this way by simply calling git push/pull from a specific branch, git will already know to which remote branch you are referring.
Spoiler alert: it’s easier if local and remote branches have the same names.
If you try and launch a git pull command, from master branch git will say this:
It’s also so kind to suggest how to solve this. Copy the last command, change <branch> to master, or alternatively launch git branch -u origin/master master
Basically we are asking git to consider origin/master as the remote version of local master.
Just to be sure, I would also launch a command to specify the name of remote default branch: git remote set-head origin -a (a stands for auto).
Ok, now we’re almost there: we have a local and a remote repo, on each of them master is the default branch and on remote its name is specifically origin/master. The local master branch tracks its homonymous on remote.
Step #6 — First Commit
If you launch a git pull command from master branch, git will try and get any changes stored on remote master, but there are none.
Instead, if you check the status with git status command, you will find that local and remote repos are up to date, but there are some untracked files. Untracked files are newly appeared files in your working directory and you are prompted to add them (to the staging area). In this case, they are Unity project files, which are untracked since the remote and local repositories are equally empty but not your working directory — because you launched git init from there.
Let’s follow git’s advice and add them all with git add . . Now the files are in the staging area. You have to commit them with git commit -m “message”.
And then the magic: git push and git will automatically know to which remote branch it has to push (of course origin/master). Another git pull, will show that there is nothing to do.
Bonus — New branch
Just for the sake of knowledge: what to do if you want to create a new branch and set tracking as did before with master? Quite the same.
Let’s create the branch dev. Starting from master, git checkout -b dev will create the dev branch, a copy of master. Let’s also check the branch list with git branch.
If I git pull, I’ll get the same story as before.
Git gives a hint, let’s follow it.
Interesting: if origin/dev exists, I should use fetch, but that’s not the case. Git suggest to push with -u option but I’m used to this other option: git push — set-upstream origin dev
Aaand it’s done.