Git and Unity Guide
Table of contents
- Preface
- Creating a GitHub Repository
- Editing Git Ignore (Optional)
- Linking Your Unity Project to GitHub
- Adding Collaborators
- Setting Up For Collaborators
- Collaboration Guide
- Useful Debugging for Common Git Errors
Preface
In this class we use Git for version control and collaboration. Why? Well imagine having to send a copy of your Unity project on a flash drive to your teammates every time you make a change. Sounds tedious right? Git streamlines this process, allowing you to not only work with others at the same time, but also save copies of your project on the cloud in case you screw something up (it’s bound to happen eventually).
However, the process of collaborating in Unity through Git is not the smoothest. You may encounter merge conflicts, you might lose your progress, your teammate Steve may even delete the files you were editing without asking you first (I still haven’t forgiven you Steve). Which is why setting up your GitHub repository correctly is an essential part of mitigating these problems in the future.
Note: This guide will assume you already have some base knowledge on both GitHub and Unity.
Creating a GitHub Repository
-
Navigate to GitHub.
-
Sign in to your GitHub account.
- Click the green
New
button on the top left to create a new repository.- (If you don’t see this option, you can also click on your profile icon on the top right and navigate to
Your repositories
. There will be an option to create a new repository there as well.)
- (If you don’t see this option, you can also click on your profile icon on the top right and navigate to
-
Name your repository and set your repo’s visibility to private.
- Under
Add .gitignore
, click the drop down and search for Unity.- Note: If your project was downloaded from another repo, i.e. Project 1, you should skip this step because the project already has a .gitignore file.
When finished your screen should look like this:
-
Finally, scroll down to the bottom and click
Create repository
-
Your respository should now look like the following:
Editing Git Ignore (Optional)
Your git ignore file will automatically be configured to ignore non-essential Unity files. However, it may be worthwhile to edit this file to include other directories that are commonly pushed to this repository by accident.
-
Click on your git ignore file in your repository.
-
Click the edit button on the top right.
- Add these two lines anywhere on the file.
.vscode/ # For users who use vscode .DS_Store # Mac OS file
-
After adding these lines, click commit changes on the top right.
Your git ignore file is now updated!
Linking Your Unity Project to GitHub
Note: This section requires you to have Git installed on your computer.
Open up a terminal and navigate to the location of your Unity project
Now run the following commands:
git init --initial-branch=main
- Note: if this command didn’t work you could also do:
git init
git checkout -b main
- Note: if this command didn’t work you could also do:
git remote add origin https://GitHub.com/USER/PROJECT-NAME.git
- USER is your GitHub username
- PROJECT-NAME is the name of your GitHub Respository
-
git pull origin main
-
git add .
-
git commit -m "YOUR COMMIT MESSAGE"
git push origin main
If all went well, your repository on GitHub should similar to the following:
Notice that we only upload Assets, Packages, and Project Settings to our repository. Everything else is auto-generated when you/others open the project in Unity.
If you end up with any extra files, you can remove them by running the following commands in your terminal: git rm --cached FILE_NAME
. Then commit and push this removal using the same commands above. (You may also want to edit your .gitignore file to ignore these files in the future. See [Editing Git Ignore (Optional)].)
Adding Collaborators
-
From your repository page, navigate to
Settings
. -
On the left panel, click on
Collaborators
. -
Click on
Add people
and enter the usernames or emails of your collaborators.
Setting Up For Collaborators
-
Accept the GitHub invitation link from your email inbox.
-
Navigate to the project repository and click the green
Code
button. -
Copy the .git URL under the HTTPS tab.
-
Open a terminal and navigate to the directory where you want to clone the project to.
-
Run
git clone YOUR-COPIED-GIT-URL
-
cd
into your cloned repo directory.
Your project has now been cloned and you can add/commit/push your changes.
Collaboration Guide
Anytime you collaborate on a GitHub repository, using branches is a great way to prevent problems from occurring. When working with other developers in Unity, this is no exception. Here are two diagrams that demonstrate the general workflow you should go through whether your group is using branches or not.
Collaborating With No Branches
Collaborating With Branches
Example with Branches
Scenario: Programmer A wants to add a new enemy type to the game Here’s what Programmer A should do:
-
git pull origin main
// Get the latest changes from the main branch -
git checkout -b enemy-type
// Creates a new branch called “enemy-type” -
Create the new enemy in Unity.
-
git add… git commit… git push -u origin enemy-type
// Push local branch to git -
git checkout main
// Switch back to the main branch -
git pull origin main
// Pull any changes from main
From here you actually have two choices on how you can merge your changes on to the main branch. We will be covering both methods, as well as weighing the pros & cons of each.
Merge Method #1: Creating a Pull Request
This method involves creating a pull request on GitHub, a standard practice in the industry. Creating pull requests helps to notify others about your changes, let them review your changes, as well as visualize any merge conflicts your changes may have. Downsides to this method is that it requires more effort on your part, and Unity files are not easily reviewable on GitHub due to their complexity (Scenes especially).
Continuing from step 6 from the example…
-
git checkout enemy-type
// Checkout out your branch again -
git merge main
// Merge main onto your branch -
Resolve any merge conflicts. // Still may happen
-
Commit and push to remote repository.
-
Navigate to Github and make a pull request.
-
Merge your changes onto main on your pull request page.
-
Delete your old branch.
Merge Method #2: Merging your branch directly onto main
This method involves merging your branch changes on to main directly from the command line. A pro of this method is that it’s a lot faster, especially for the purposes of this class. The downside is that if you haven’t been committing your changes regularly, or haven’t been updating your teammates about the files you’ve been editing, you could potentially lose both yours and their work. So be careful!
Continuing from step 6 from the example…
-
git merge enemy-type
// Merge your branch on to main -
Resolve any merge conflicts. // Still may happen
-
Commit and push to remote repository.
-
Delete your old branch.
Useful Debugging for Common Git Errors
If you after you run git pull
you get a merge conflict with your .gitignore (or any file) run
git checkout --ours .gitignore
or git checkout --theirs .gitignore
To connect your local main branch to the remote main branch run:
git branch --set-upstream-to=origin/main main
If you pull from main and get an error about unrelated histories run:
git pull origin main --allow-unrelated-histories
If you accidentally push files you don’t need to the GitHub repo run:
-
git rm --cached FILE_NAME
-
git commit -m "YOUR MESSAGE"
-
git push