Git-ing Started. A Quick Start Guide To GitHub
What is git?
In the simplest terms git is just a tool to conduct version control on a project. GitHub is the remote server that hosts our code repository.
How do you install git?
If you navigate to the git site it should detect your computer type and give the appropriate download option for you to click on.
Once it has downloaded open the file and just use the default options for now.
On the last page uncheck “view release notes” and check “launch git bash”.
Creating a GitHub Account
Navigate to the GitHub site (don’t worry while there is a paid version, setting up an account is free).
You will need to provide a username, email, password and pass an anti-robot trap.
You will also be asked a couple of questions about how you intend to use the service but don’t worry, this does not lock you into anything it mainly just personalizes your experience.
You will receive an email with a link to verify your account (check your spam folder if you can’t find it).
How to link a Unity project to a git repository
This is going under the assumption you already have a Unity project set up that you want to link to GitHub. If you have never used Unity before check out this article on creating your first Unity project, it will take you through the steps to get Unity downloaded and installed so you can start your first project.
Creating a repository
Log into your GitHub account and on the left-hand side of the screen where it says “repositories” select “new”.
Give your repository a name (spaces will be replaced with hyphens “-”).
A description is optional but is a good standard to enter as well.
Choose if you are going to make your repository private or public.
Private: Only people provided with access permission are able to access this repo. Go to the settings of the repository where you will find the option to manage access. You can search by username, full name or email. The user you select will receive an email with a link to accept the invite.
Public: The repository is open to the public to view, but users require permission to publish commits.
How to ignore files
You can add a .gitignore file. Git ignore is simply a file created in your project directory which is ignored in your repository. It can be useful when you don’t want to commit sensitive information or other files that aren’t necessary like library and temp files. At the moment we are going to select the standard unity file then click create.
How to link your project
We are now going to link the server to your project. On your repository page (on the GitHub site) click the green code button and copy the link (this works better when you highlight the text and copy it rather than using the clipboard button).
Navigate to your Unity project folder, right-click and select “git bash here”. This will open Git in your project.
Type “git init” in the console which will initialize git in your unity project.
Now we can create the link using the following command:
git remote add <name for remote server> <web link you just copied>.
The industry standard would look like the following “git remote add origin https//your-github-link”. Note you need to right-click to paste into git, the shortcut buttons won't work.
You can also use the verify command “git remote -v” to verify the connection which will provide a print out in the console of the permissions you have. You should have fetch (get data from the server) and push (give data to the server) permissions.
How to version control your projects
The best practice is to always pull from the server before committing and pushing. Other team members would likely have updated the project (even if you’re solo you may have made changes not currently reflected). If you commit and push now you can cause some errors related to conflicts when trying to merge your local project with the remote server. Just remember pull/commit/push
How to pull from the server
We use the following command to pull from the server:
git pull <remote server name> <branch to pull> e.g “git pull origin main”
How to check the status of your branch and add changes to next commit
Use the command
“git status “
to show the changes since last commit (this will show in red for changes not committed yet)
You can use the command
“git add .”
to all the uncommitted files to the next commit or you can use the command
“git add fileName” e.g “git add assets/scripts”
to add the files individually. Remember you can use “tab” to help autofill your text.
How to create your commit
Use the following command to create your commit:
git commit -m “message”
Write a note describing what we are adding with this commit for example
git commit -m “initial commit” and click enter
This puts everything you have added in the previous step into a commit ready to be pushed to the remote server
How to push your commit to the remote server (GitHub)
Push to the server using the following command:
Git push <remote> <branch> e.g “git push origin master”
Branches
What are branches?
We can think of branches in git the same as branches on a tree. The trunk is the main branch and all new branches can sprout off from here. Just as you can see branches, sticks and twigs splitting off each other as the tree grows, so can you create separate paths for your versions with branches in git.
Every time we create a new branch (in our example we are going to create a branch to implement a new enemy) we are basically copying our main branch and then keeping all our changes separate from it. This is useful when testing new features or fixing bugs. It helps teams of people all work on different features at the same time without affecting each other's work.
Once we have achieved what we needed to and everything is working we can then merge our branch with our main. Think of the main branch as our live or stable version.
How to add a new branch
To add a new branch we use the following command:
git branch <branch name> e.g “git branch dev”.
If the branch already exists you will receive an error in the log.
How to check available branches
Use the following command to receive a list of all branches in the project:
git branch
The branch prefixed with an asterisk (*) is the branch which is currently active, the text will also be coloured green.
How to switch branches
The following command will allow you to switch between your branches:
git switch <branch name> e.g git switch dev
How to merge branches
Think about the following scenario. You are starting your game and so far, you have a player game object and script working. We don’t want to break anything, so we create a new branch “dev”.
In this branch, we create our game manager script and attach it to a new empty game object in our scene. While we are creating this a colleague of ours is working on a new enemy to add to the game, so they create a new branch called enemy.
Now the enemy has been created we want to add it to our development build so we merge it into our dev branch. Until we do this our enemy script and object will be missing from our dev branch.
Once we are happy our dev branch is stable we can now commit our dev branch and merge it with our main branch so all our branches are up to date.
How to revert to a previous commit
Using the log which is created automatically by git, accessed by the following command:
git log
we can go back through all our previous commits. This can be useful if there is an error in the project we can’t fix and need to revert to a previous version. To exit out of the log screen we use the “Q” button.
We can copy the commit id and use it in place of a branch to switch to using the command:
git checkout <commit ID> e.g “git checkout 1234asdf56789wer”
This means we can see a previous commit before we decide to revert our project back to that commit.
We can also create a new branch for this commit using the following command:
git checkout -b <new branch name> <commit id> for example “git checkout -b project-revision 12345asd6789fghj”
How to reset to a previous commit
We use this command to force the “head” of the project back to the commit we choose. This is only a local change and does require pushing to the server. We use the following command to achieve the reset:
Git reset –hard <commit id> e.g “git reset –hard 123asdf456gh789qwe”
You can then push this change to the server, which needs to be forced in order to override your repository. For this we use the following command:
git push — force <remote server name> <branch name> e.g “git push — force origin main”
This means we can remove commits from the remote server.
How to delete a local branch
You can delete a local branch using the following command:
git branch -d <local branch name> e.g “git branch -d project-revision”
This does not affect the repository, but cannot be used if the branch contains commits which have not been merged into other local branches. This can be overridden using the following command
git branch -D <local branch name> e.g “git branch -D project-revision”, but use this with caution.
How to delete a remote branch (remove branch from repository)
If you wish to delete a remote branch you need to push the changes to the server with the following command:
git push <remote server name> — delete <remote branch name>. e.g “git push origin –delete project-revision”
It's worth keeping in mind both the local and the remote branch will have to be deleted separately.
That's all for now.