Setting up a GIT repository for a Unity Project


For this example, we assume the following is true:

  • You have a Google Drive located on your computer and a directory for git repositories located at D:\Google Drive\git_repos
  • You have a Unity project located at P:\Unity Projects\new_game
  • You are using Sourcetree

Open Sourcetree and then open the Terminal (there is a button on the top right).  Go to your Google Drive git repositories directory.  Create a new directory for your project called new_project.  Enter the command git init --bare new_project to create a new headless repository.

Close the Terminal window.  Inside of Sourcetree, create a new repository and enter the location of your Unity project, in this case P:\Unity Projects\new_game.  You will get a popup warning you the folder already exits.  This is ok, you can click Yes to continue.

New Sourcetree project

Now go to Settings (there's a cog icon in the top right).  From the Remotes tab click Add to link your project to the Google Drive git repository we created earlier.

Repository Settings

In the URL / Path field, enter the directory to your Google Drive git repository, D:\Google Drive\git_repos\new_game.  Click the Default Remote checkbox.  Then click OK.

Remote Details

You are now ready to stage and commit your files to your git repository!  Be sure to save your work regularly.  How to use git on a daily basis is beyond the scope of this article.

Bonus Points

For bonus points, you can setup a preconfigured .gitignore, .gitattributes, and .gitignore that is tuned for Unity projects.

Below are the files I use for new projects (these are saved as .txt files, you will need to copy the contents or rename them:

Please note that you need to manually edit the .gitconfig and put in the location of your UnityYAMLMerge.exe where your Unity install is located.

cmd = 'D:\Program Files\Unity\2020.3.19f1\Editor\Data\Tools\UnityYAMLMerge.exe' merge -p "$BASE" "$REMOTE" "$LOCAL" "$MERGED"