Git — Pull, Commit then push
This is the second article for using Git with Unity. If you’re yet to set up Git and link it to a local project, then please read that article first.
We left off with linking our local repository with our remote server on GitHub, now we need to go through the process of making changes to a project, see those changes in Git and what to do with those changes.
For this tutorial, I have an empty Unity project with no additions made to it. The first thing to do is to make sure that you have the latest changes to the project by pulling the data from the server. You might be thinking, ‘what changes, I’ve just made the repository and it’s empty’ but just humour me and follow along. Remember in the last article where I mentioned you should change the default branch from main to master? This is where that comes into play.
git pull origin master
this will run and you should end up with the following
If you didn’t change the default branch you would instead type
git pull origin main
But again this will lead to an issue when you’re pushing your project changes in the future and it is why I recommend the change when the repository is created.
You can check which branch you’re currently on by typing
You will see:
If you have more than one branch you will see them shown in a list. The * next to a branch means that it is the current branch the local repository is directed to. There will be more on branches in another article.
For now, let's move on to making some changes and seeing them in git. Open your Unity project and create a game object, this could be anything, but for me, I’m going to create a 3D cube and attach a new script called Player.cs.
Now, remember to save the project before going back to the terminal or into GitBash. Once there, we type
Once you have typed that in you should see a list of files and folders that are not currently be tracked by git. So now we can add these files and folders to git for tracking. We Type
git add .
The full stop is a special command and will add all our changes for tracking.
Once they are added we can type ‘git status’ again to see what happened.
You will now see all the files that were added. They are now staged and are ready to be committed to git. So, we type
git commit -m “Added cube and player script”
You can type whatever you like in the “Double quotes” and they need to be in the command you type. Always make sure your commit message is descriptive of the changes that have been made.
So the last thing to do now that we have made all the changes we want right now is to push the changes to the server. Type
git push origin master
This will upload your commits to the server.
Once the changes have been made you can refresh the page on GitHub and you will see the commits that you just pushed. That is all for this article but be sure to check out future articles for more on branches, merging and reverting.