Follow these steps in order to set up a Unity project with an online git repo on GitHub using a personal access token (PAT). Please share any recommendations or errors you notice and I’ll update these instructions.
GitHub
- Create an empty repo in GitHub including the Unity .gitignore
- Click User icon in top right > Settings > Developer Settings > Personal Access Tokens > Fine-grained tokens > Generate New Token
- Toggle on permissions. Include at least “Contents” with read and write permission.
- Copy paste token contents somewhere safe. You can’t see the token after you close it. If you lose it you can create a new one.
Unity
-
In Unity Hub, create a Unity Project with the exact same name as the GitHub repo
a. If you already have a project created, then skip this step -
Close the project
-
Rename the project so it doesn’t conflict when we clone the repo from GitHub. An easy choice is to simply add an underscore to the beginning of the project name. e.g. “_MyProject”. The reason we first named it the same is simply so that you don’t have to relocate it in the Unity Hub later.
-
Open terminal at the project’s parent folder (the grandparent of the Assets folder)
-
Check if you have git installed. Command:
git version
a. If that logged an error like “git is not a recognized command” then you need to install git here Git - Downloads
b. If you already tried installing it and still get that error, copy paste the error message into google for help. Your computer may not know where to find it in your file system. -
Clone the repo from GitHub. Command:
git clone https://<personal_access_token>@github.com/<GitHub_username>/<GitHub_repo_name>.git -
Move all contents of the Unity project into the newly cloned git repo folder. Then you can delete the old, empty unity project folder
-
Move terminal to the newly cloned git repo folder. Alternatively you can simply close terminal and re-open it at the new folder. Command:
cd <repo_name> -
Double check that it’s a valid git repo. Command:
git status -
Double check you have a .gitignore file with the standard Unity stuff before you track all files. This should have been included in step 1 above. See gitignore/Unity.gitignore at main · github/gitignore · GitHub for the standard .gitignore if you need it.
-
Track all existing files. Command:
git add . -
Commit tracked files. Command:
git commit -m “Initial commit.” -
Push the commit to GitHub. Command:
git push -
Refresh the page on GitHub to see your files there with the “Initial commit” message.
Notes:
- The GitHub section should work for other sites like GitLab but you’ll need to modify the instructions a bit and change the clone url
- In the git commands above, angle brackets and their contents must be replaced with your data e.g. if you’re repo is named “MyRepo” then ‘cd <repo_name>’ would be ‘cd MyRepo’
- You can do this process in a git GUI such as GitHub Desktop or Sourcetree. But I personally like using the command line.
- Git has a confusing credentials/password pipeline with several options to verify your identity. These directions use the personal access token method.
- There is a “chicken or the egg” dilemma here. Unity Hub won’t create a project in a folder that already exists and git won’t clone into a folder that already exists. These directions solve this by cloning the git repo into a new folder and simply moving the Unity project contents into that folder.
- There is a way to connect an existing git repo and an existing Unity project without moving files by using the
git push –set-upstreamcommand, though this tends to work best if the remote repo is completely empty. I find the cloning method a bit easier. - Thanks to everyone in the comments for great suggestions.