Setting Up Git for Unity
What is git?
Imagine that you are working in your game (or any Unity project) and something went wrong, for some reason you need to go back to a previous state where everything worked in order to continue development.
Git is a version control system, which means that it is a program that allows you to track every file in your project throughout the whole development cycle.
Some of the benefits Git offers are:
- Have records of every change on your project
- You can revert to specific version you need
- Make collaboration easier, allowing changes in parallel to be merged into one source
- Windows: First download Git here and follow the instruction of the installer wizard.
- Mac OS:
Install homebrew if you don’t already have it, then open the terminal and type the command below:
$ brew install git
Creating an Unity project
- Open Unity Hub (if you don’t have it, download it here and follow the installer instructions)
- Select “New”
- Select “3D”
- Name your project as “GitUnity”
- Select “Create” and wait until Unity opens the project
Adding git to your project
- Open the terminal (git bash for Windows)
- Go to your project directory
- Run the command “git init”
The command “git init” creates a Git repository in your project and you will be able to run git commands within it.
Let’s understand some basic git concepts before making the first commit. When we use with git, we have three areas where the files will be while we are working:
- Working directory: the project folder on your computer
- Staging area: Intermediate area between the working directory and the Git repository. In this area you can add files to create a commit (in short a commit is a version of your repository at one point of time).
- Repository: is a hidden folder that Git uses to track all changes made on your project.
Now knowing the above, let’s see the Git basic commands (note: all git commands start with the word “git”):
- git status: displays the state of the working directory and the staging area.
- git add: receive a file/directory name as parameter, and move it from the working directory to the staging area.
git add <directory name>
Tip: you can use the command “git add .” to add all changes to the staging area.
- git commit: Move changes from the staging area to the repository.
git commit -m “some message that indicates what you did”
Good to go, run the following commands to make your first commit:
- git status
- git add .
- git commit -m “Creating Unity Project”
At the beginning using Git is intimidating but it is really easy and with practice you will become a Git expert in no time.
P.S: Using git is better than making a copy of your files and name it “my_project_final_V2” each time you go to create a backup.