Get Started Using Git with Unity
Git is a popular open-source Version Control software, which allows you to track changes for a collection of files and even manage changes across multiple contributors.
To download Git, navigate to the Git Download page in your preferred browser. The example below shows how to select the Git installer version for Windows 64-bit. Select the version that suits your system.
When prompted, selected the file path where the installer should be saved. Once the download completes, launch the installer. The installer name should appear similar to the example below, with some differences depending on the current Git release and your system version.
Once you launch the installer, you may be asked if you would like to allow it to make changes to your computer. To proceed, click Yes.
To get started with Git, proceed through the installer with the default settings. You can always adjust these settings later as you become more familiar with Git.
Create Local Repository
To start using Git with your Unity project, first point the Git Bash console to the file location where your project folder is saved. There are two way to do this:
Navigate from within Git Bash
- Launch Git Bash from the Start menu
- Note the default location where your Git Bash console launches from in the window title
- Use the cd command to “change directory”, that is, to change your file path. You can provide the full file path (ie. /c/Users/erinb/My Documents), if you know it, or the next folder down in the path (ie. “My Documents”) or you can enter “..” to move up one folder in the path.
Note: If you’re not sure what files and folders are in your current location, enter the ls command to “list” files in the current directory.
Launch Git Bash from Project Folder via File Explorer
- Instead, you can also navigate to the project folder via your system’s file system, right-click from within the project folder and select Git Bash Here. This will launch Git Bash with the location already pointed to your project folder.
Once you are in Git Bash and in the project folder location, to start using Git, just enter the command git init to “initialize” the local repository. A repository, as it’s often referenced in Git, just refers to the collection of files whose changes are tracked by Git (in this case, your project).
Create Remote Repository
Whether you solely use it as a backup location or a method for collaborating with others, there are so many benefits to using a remote repository as well as your local repository. There are multiple options for this service, but let’s focus on GitHub as a popular option with a considerable free tier offering. If you’ve never used GitHub before, all it takes to sign up is an e-mail address.
Once you’re all signed in, simply click “New” on your homepage to create a new remote repository. Enter a name and description for your project and add a .gitignore file using the Unity template. The .gitignore file is a specially formatted file that tells Git which types of files should not be tracked in the repository. A Unity project contains many types of files, and not all files need to be tracked for changes. Using the Unity template for .gitignore will prevent Git from tracking the unnecessary files.
Now that you have a remote repository for your project, you’ll want to connect your local repository to the remote so you can move changes between them. To do this, in GitHub, copy the web address for your remote repository by clicking the Code button, then the Copy button under HTTPS.
Then, in the Git Bash console, enter the command git remote add followed by the name to refer to your remote repository (typically origin) and then the web address for your remote repository. There is no immediate message to confirm that the remote repository has been linked, but you can enter grit remote -v to verify. If the web address to your remote repository is listed for both pull and fetch, you are good to go.
Your First Commit
As you make changes to your project, you’ll save them as normal in Unity. When you’ve completed a fix or a feature and you’re ready to track it through Git in your repository, you’ll want to follow this workflow to keep in sync between your local and remote repositories.
First: Pull in any changes from the remote repository
In the Git Bash console, enter git pull followed by the name you gave your remote repository earlier (origin) and the name of your current branch, which you can find in parentheses at the end of command prompt. In the example above, the local branch is “main”.
Second: Stage changed files for commit
In Git, updates are stored batches in your repository are called commits. You have control over what files are tracked in a particular commit. To see what files have changes that have not been tracked, you can enter git status. This will list changed files that have already been staged (selected to be included in the next commit), changed files that have not been staged, and untracked files (that have never been included in a commit before).
To stage files for commit, enter git add followed by the file name. To stage all pending files, replace the file name with a period (.).
Third: Commit staged changes
To commit the staged changes to the local repository, enter git commit -m followed by a brief description surrounded in double quotes.
Fourth: Push local changes to the remote repository
Now that the changes have been committed to the local repository, the last step is upload or push the changes to the remote repository. To do this, enter git push followed by the name of the remote repository (origin) and the name of current local branch (main, same as we used in the pull command earlier).
If you return to GitHub and refresh the page, you should now see the commit reflected there.
As your Unity project grows, tracking your changes through Git and GitHub will give you the flexibility to research prior project versions, remove selected commits, and even collaborate changes with other contributors. I hope you’ll give a try!