Setting up Git with Unity Projects
If you develop software of any kind, you may be familiar with version control systems like Git. However, you may not be sure how to make them work with Unity. If that’s the case, good thing you’re here! That just happens to be what we’re talking about today.
For this tutorial, I’ll create a new Unity project. However, if you have an existing project, you can just skip the first step and follow along with the rest. It’s never too late to add version control!
First, create a new Unity project. Give it a descriptive name, but avoid hyperbole.
Now that we have a project (or if you already had one, this is where you’ll jump in), we can set up version control. Git is certainly useful when developing solo and without the internet, but if you’re working with a team, switching between multiple development machines, or just plain not interested in losing work, you’ll want a remote repository where you can upload your code. For the purposes of this tutorial we’ll use GitHub, but there are other options that are just as good.
I’m going to assume that you already have a GitHub profile (if not, you can create one for free in just a few minutes). Log in, go to the repos tab, and create a new repo.
Unity projects have a lot of files and folders that are generated automatically and don’t need to be committed into your source code. Fortunately, GitHub already has a gitignore template available that accounts for most of them.
Now that your repository has been created, you’ll need to link it to your project’s local files. Open the Code dropdown and click the button to copy your repo’s path to the clipboard.
If you don’t have Git installed yet, you can find the correct download for your operating system here. If you’re on linux or Mac you can use the built in terminal, but if you’re on Windows I would recommend using Git Bash which comes with the Windows version you can download from that link. Just right click your folder and select “Git Bash Here”.
Once you have your terminal of choice open and you’re in your project’s directory, enter the following command:
$ git init
As the output suggests, this initializes an empty Git repository in the current directory.
Next, you’ll need to connect the local repo to the one you just created on GitHub. You can do that by entering the following:
$ git remote add origin <paste the copied URL from GitHub here>
Running that last command probably won’t produce any output, so we can make sure it worked by running the “remote” command with the “v” (verbose) option.
$ git remote -v
If everything worked as expected you should see something like this:
origin https://github.com/<your_name>/<your_repo_name>.git (fetch)origin https://github.com/<your_name>/<your_repo_name>.git (push)
Next, we’ll want to pull the repo down from GitHub. This won’t overwrite anything you’ve already done; it will just make sure we have the .gitignore file we chose when we created the repo. To do this, run:
$ git pull origin main
Note: If your project contains a lot of assets (images, sounds, 3D models, etc.), you may run into issues with exceeding GitHub’s maximum file size. One way to solve this problem is with LFS (large file storage). Its use is beyond the scope of this tutorial, but keep it in mind in case you run into trouble.
Now that everything is linked up, it’s time to commit your code and push it up to the repo. Run the following commands:
$ git add .
// this will stage any uncommitted changes$ git commit -m "Add existing project (or something similar)"$ git push origin main
And that’s pretty much it! Go back to your browser, refresh the page, and marvel at your version controlled source code.