Foundations: Don’t just save — Commit!
Now that we’ve linked Unity, Git and GitHub, let’s run through the process of communicating our changes between all three systems. When you save a change in your Unity project, Git tracks that the change was made to your project files, but that change is not saved to your repository until you commit it. To make sure that you are combining your project changes with the most up-to-date version of the remote repository, follow the four steps below each time you’re ready to add a change to your repository.
1) Pull any changes from the remote repository
In Git Bash, enter git pull followed by the name of the remote repository (ie. origin) and the name of the remote branch (ie. main). This will pull any new commits on the remote repository and merge them with your local repository to keep your local repository up-to-date.
At this point, the only commit in our remote repository is the “Initial commit” added by GitHub when we created the repository. However, since we used GitHub to create our .gitignore file, we still want to pull that file into our local repository before making any commits to ensure that Git does not track changes on unnecessary files. For more on .gitignore files, check my prior article here.
2) Stage your changed files
To commit your changes to your local repository, you must first tell Git which files should be included in the commit. To see what files have uncommitted changes or which files have not yet been included in any commit, enter git status.
Once you know what files have changed, you can then add specific files to the commit by entering git add followed by the filename.
If all changed files should be included in the commit, enter git add followed by . (single period) to add all changed files to the commit.
You can always verify all changes are added (or staged) for commit by entering git status again.
3) Commit your changes to the local repository
To commit your changes to your local repository, enter git commit.
This will open a window in the default text editor titled “COMMIT_EDITMSG”, which will list all of the files included in the commit. At the top of the file, enter a message or comment briefly describing the commit. Save the file to close the window.
Alternatively, you can enter git commit -m followed by your commit message in quotes to avoid the text editor window.
4) Push your changes to the remote repository
Once your changes are committed to the local repository, enter git push followed by the name of the remote repository (ie. origin) and the name of the local branch (ie. main).
This will add your changes to the remote repository. You can refresh your GitHub page to see the change.
As you build your project in Unity, remember to not only save often, but commit your changes often as well. A single commit should capture a single purpose. This four-part process to commit your changes may seem a bit laborious at first, but it can save you so many headaches later. Say it with me: Pull! Stage! Commit! Push!
Next Up: Git’s Superpower!