How to use Git with Unity
A Version Control System (VCS) is a source control for managing file changes. It records changes to files over time. A typical example of a VCS is Git. Git is an open-source tracking system for keeping track of file changes. Git allows multiple developers to collaborate on a project, each developer with his copy of the project. Each copy is known as a repository. Git tracks changes made to files within a repository and can merge or revert changes made by different people, enhancing collaborations.
As a Unity developer, integrating version control systems with your projects is recommended to enhance collaborations and manage changes you make to your files. Using Git with Unity is just like using Git with any other project.
In this article, you’ll be provided with a step-by-step guide on how to use Git with Unity;
Configure Git
The first thing you need to do is to install Git on your local computer (if you haven’t). You can just download the Git Installer from the official website here. Downloads are available for Windows, MacOS, and Linux. After a successful download, install it on your local computer by following the installation instructions.
After a successful installation, you have to configure your username and your email address on Git. Here’s how:
- Open the Git terminal on your computer
- Then, enter the prompt below to configure your username.
$ git config -global user.name “your username”
Change “your username” to the username or team name you want to use.
Next, enter the prompt below to configure your email
$ git config -global user.email “your email”
Change “your email” to the email you want to use.
Create a Git Repository
Now, you have configured your Git appropriately. The next thing to do is to set up a repository for your Unity project.
- Navigate to the root folder of your Unity project in the Git terminal. (The “cd” command will come in handy at this point.)
- Type
git init
to initialize a repository.
Create a .gitignore
file
Unity generates a lot of files that don’t need to be tracked by Git. Therefore a .gitignore
file is needed to tell Git the files to be ignored and not to be included in the repository. The file cannot be created in the root folder of your Unity project. Files and folders you want Git to ignore are added to the file.
Add and Commit Files with Git
To make a commit, you have to first add the files and folders. To do this, use the command git add folderorfilename
to add a file or folder. You can also use git add .
to add all the files and folders at once.
You can use the git status
command to check if you added the files.
Then, make a commit to the repository by typing the code below in the terminal;
git commit -m “your commit message”
It’s necessary for you to add a commit message. It doesn’t have to be long, just a short message that describes the commit.
Push and Pull changes
If you are working with a team, you will need to push your changes to a remote repository or pull changes from the remote repository on GitHub, GitLab, or Bitbucket to stay up-to-date with your team. Use git push
to push your changes and git pull
to pull changes from a repository.
Conclusion
Now, you have learned how to set up Git with Unity and even make your first commit. You should visit the Git documentation here for more information on using Git.