Git + GitHub setup for Unity Projects
In this quick guide, I will follow you through the steps needed to start using Git, creating a repository out of an existing Unity project of yours, and pushing that Git repo to Github.
Install Git and setup
First, head to https://git-scm.com/downloads and download Git for your given platform (in the case of Linux and Mac, you should even already have Git installed by default).
Installation is pretty straightforward, follow the Next and Finish prompts to complete the installation. The default options should work fine for most.
Setup your user name and email
This is for attributing your commits on your PC to a given email, you can change it later:
- Open a command prompt, or you can find a new “Git Bash Here” prompt when you right-click somewhere on your desktop or in some folder in the Windows explorer:
- Run the following commands, one after the other:
$ git config --global user.name "Augusto"
$ git config --global user.email "youremail@gmail.com"
Just change “Augusto” and “youremail@gmail.com” to your desired username and email.
.gitignore file
Next, we should take care of our .gitignore file, this file will keep out temp files from being uploaded to our repo.
Locate your Unity project folder, create a new file called “.gitignore”, open it with any text editor and paste the contents of the following file: https://github.com/github/gitignore/blob/main/Unity.gitignore
Setup, first commit
Now, we can move forward in initializing our repo, first of all committing the .gitignore file, and then the rest of the files:
Open a command prompt on your project’s root folder, you can right-click and do “Git Bash Here” like before and run:
$ git init
This will initialize your git repo.
As of now, no files are committed, and Git is not tracking anything, we will first add the .gitignore file to the stage, and commit it.
$ git add .gitignore
Adds the .gitignore file to the stage.
$ git commit -m "added gitignore"
Creates a new commit with the staged files, and sets “added gitignore” as the commit’s message.
Now that the .gitignore is tracked by the repo, it will correctly react when we try to add all the files for our next commit:
$ git add *
Adds all the rest of the files to the stage (excluding temp directories like /Library)
$ git commit -m "project files"
Creates a new commit with the staged files, and sets “project files” as the commit’s message.
Now, our repo looks good locally, and we can move forward by pushing it to GitHub.
GitHub, pushing changes
Now you can head to https://github.com/, and create an account if you haven't yet.
Then, to create our repo in GitHub, you can find in the top right the following button:
Then, the following form will be shown:
You can choose your repo name, which should be your project’s name.
You can either keep it public or private.
We won't add a README, since we already have an existing repo, and we already added our own .gitignore so you can skip those steps and leave them as default.
Then, just press Create Repository.
Now, this new GitHub repo is sitting empty, we need to connect our local repo to this new GitHub repo as a remote.
For that, we will follow the “push an existing repository from the command line” instructions.
So we go back to our local repo, and open GitBash again on the root folder of the project:
We first need to add the GitHub’s repo HTTPS URL as a remote target, by default we usually call this “origin”.
In my case:
$ git remote add origin https://github.com/Svartskogen/test.git
Your HTTP URL will look different than mine, you can find your HTTP URL on GitHub’s repo.
GitHub tells us to create a new branch called main, you can skip this step and keep the original branch master. in such case, we only need to do:
$ git push -u origin master
git push “pushes” (uploads) our current state to origin, and we just defined origin as the GitHub’s repo HTTPS URL.
After doing this, you might see some kind of auth prompt to log in to GitHub, so just fill in your credentials, and after that, you will be able to see your files uploaded to the GitHub repo, just refresh the page.