Having a gitignore in your Unity project is almost mandatory if you use Git. Here is the minimum information you need to know to make it work.
Your gitignore will evolve
A “.gitignore” file is a special settings file recognized by Git. It will tell Git how to behave. The main goal of a gitignore file is to save space on your Git repo and to avoid merge conflicts by ignoring files that can be generated.
Unity generates lots of files, and with each new Unity version, their location can change new files can be generated in new folders. So ideally you should keep your gitignore up to date.
You could write your own, but to save time it’s usually more efficient to take one from internet. Personally, I think that the most comprehensive and up to date gitignore is this one from GitHub. Why? Because a lot of clever people have worked together to come up with it and maintain it.
As of this writing, here is what it looks like:
# This .gitignore file should be placed at the root of your Unity project directory
# Get latest from https://github.com/github/gitignore/blob/main/Unity.gitignore
# MemoryCaptures can get excessive in size.
# They also could contain extremely sensitive data
# Recordings can get excessive in size
# Uncomment this line if you wish to ignore the asset store tools plugin
# Autogenerated Jetbrains Rider plugin
# Visual Studio cache directory
# Gradle cache directory
# Autogenerated VS/MD/Consulo solution and project files
Then download the repo by clicking on code -> Download ZIP:
Unzip the folder. The gitignore file you want is shown in the image below. Copy it into your Unity project and rename it exactly “.gitignore”.
Where to put the gitignore file in a Unity project
It depends on what you wrote in the file. If you used the one from GitHub, just place it at the root of your Unity Projet. Like in this image. Note that the root of your Unity project is not necessarily the root of your Git repository.
If you were already using Git on this project
If you already added files to your repository, even if you tell Git to ignore them, it continues to follow them. You might prefer to start a new Git repository.
Or you can tell Git to forget those files.
To do that, open the terminal, navigate to the root of your repository and type “git rm -r x”. Where x represents all the folders and files that exists in your git repo and that are also mentioned in the gitignore file. And then commit. It should look like this:
That’s it for the basics about using a gitignore with Unity! I hope you are now up and running 😊