Let’s get started!
Create a New Unity Project
We’ll need a project to work with, so let’s create a new one to use throughout the post. I’ll make a new 2D project named GitHub_With_Unity.
Initialize a Local Git Repository
With our project created, we can initialize a local git repository by changing to the new project directory and using the
git init. We now have a local git repository initialized for our project. 🎉
There’s a bit of a problem, though, in terms of optimization of our repository storage. Let’s take a look at the untracked files known to Git at by default using
Assets/ Library/ Logs/ Packages/ ProjectSettings/ Temp/ UserSettings/
We don’t need to store the majority of these directories in our repository. Unity will generate them as needed when opening the project.
We can tell Git to ignore files and directories using a
.gitignore file that lives at the root of the project. Luckily, you don’t need to create this file from scratch!
Let’s check it out.
Ignore Unnecessary Files
Let’s visit gitignore.io to create a Unity-specific gitignore file. Type Unity in the field and select "Unity" from the dropdown, and click "Create."
You’ll be navigated to the latest Unity gitignore file that you can then save directly to the root of your project by either copying and pasting the content into a new
.gitignore file or saving directly from the browser and into your root project (
Ctrl + s on Windows,
Cmd + s on macOS).
With that file in place, we can check the status once more to see that Git acknowledges only the Assets, Packages, and ProjectSettings directories.
Assets/ Packages/ ProjectSettings/
That’s much better. Let’s make the initial commit.
git add . git commit -m "Initial commit"
Now we need a remote repository on GitHub so we can push our changes. Let’s do that easily with the GitHub CLI.
Create Remote Repository
I’ll obtain the GitHub CLI via Chocolatey.
choco install gh
In order for the CLI to access our repositories, we will need to authenticate using
gh auth login, which will walk you through the steps to authenticate.
To create a repository on GitHub based on our project, change the directory to the root of the project and run:
gh repo create
You’ll need to provide the repository name and description (or press enter to take the defaults) and set the visibility. Once confirmed, the origin remote will be added and the remote repository created for you. Check it out by viewing your repositories on GitHub.
Finally, it’s time to push our initial commit.
git push --set-upstream origin master
Now you know how to initialize a repository from your current project, add a .gitignore file to eliminate unnecessary files from the repository, and create a new remote repository on GitHub.
I hope you found this helpful!