How to Set up: Git and GH for Unity
Tags: unity3d, git, github, gamedev
Published: 2024-01-29 03:31:34

With the provided Unity `.gitignore` [file available through GitHub](https://github.com/github/gitignore/blob/main/Unity.gitignore) you can easily collaborate with your favorite version control system (Git) and remote repositories on your game development projects. Following these easy steps you can set up a new Unity project and connect to a new remote repository on GitHub or even jump into a preexisting one quickly.

Doing this will allow you to confidently branch your development and iteratively make changes like you would through the SDLC of any other application. The Unity `.gitignore` preset includes all necessary files and excludes the large ones (would need Git LFS otherwise) that will be built by the Unity editor when you open the project anyway.

## How to hook up Unity to Git/GH
- Make a Repository on GitHub (GH)
  - If you aren't using a template, make sure you select "Unity" `.gitignore`
- Make a new project from Unity Hub (UH)
	- Make the name of the project the SAME as what the cloned down repo will be(should just be the name of the repo (_sans-"main"_))
- Create a script in your new Unity project (within Unity) to open it with VS by double clicking it
- Open a terminal in the root folder in VS (your unity proj)
- Run `git init` to initialize a git repo inside of it
- Create a `main` branch if it isn't autogenerated from `git init`
- Run `git remote -v` to see if `origin` has been created automatically or not...
- If it HAS:
	- Run `git remote set-url origin https://gihub.com/your/repo.git`
	- to set the remote URL for origin to your clone URL for the GH repository
- Perform ONE of the FOLLOWING (**A** or **B**):
- **A:**
	- Run `git pull origin main` 
- **B.1:** (_if you have anything in your Unity git repo that you need merged with the remote repo_)
	- In the terminal where you ran `git init` (the root of your Unity project) run `git checkout main` (_if you are not already in main_)
	- Run `git branch -m main-holder` to rename this branch temporarily
	- Run `git fetch`
	- Run `git checkout main`
	- Run `git pull origin main` this will create a local main that matches the remote main and sync
	- Run `git merge main-holder --allow-unrelated-histories` to merge anything you need from your local Unity project
	- _If any of the above steps **fail** and you don't want to debug... (**experimental**)_ 
- **B.2:** (only if **B.1:** has failed)
	- In a new terminal or system explorer Navigate to the parent folder where your Unity project sits.
	- Run `git clone https://gihub.com/your/repo.git`
	- In the terminal where you ran `git init` (the root of your Unity project) run `git checkout main` (_if you are not already in main_)
	- Run `git merge ../your-cloned-gh-repo/main --allow-unrelated-histories` to merge the main from your local version of the remote GH repo into your local Unity project git repo you created
- Handle the merge into your Unity Project's git repository where the C# scripts will now be added by default from within the Unity Editor

If you found this helpful, check out/fork/clone the [Unity Template Repository](https://github.com/colinwilliams91/unity-game-template) I made on GitHub and feel free to give it a star or like/share this blog :)

\#git #github #unity #setup #howto #how #to #tutorial #devops #agile #team #collab #collaborate #game #gamedev #development