Hey and welcome!
Now we’re onto the last part here that covers my favourite topic with GIT, the branching system. Technically we’ve already got GIT integrated with Unity and we can now send changes from our project directly into our repository and even revert the project to previous versions, hence the version control.
So for a fresh repository in Github you’ll have just the one branch with a default name of either main or master. This is fine and dandy if you’re working on a project by yourself that will only be used locally but what if the project you’re working on is live and available to the world? Well you would run into an issue pretty quick if you commited a change that ended up bugging out your project as it would affect your users as well.
This is where branching comes in and is one of the reasons that makes it so great as you can set up a separate development branch from your production master branch that you can work on worry free without it causing issue for the users.
git branch dev
After using that command you will have created a new git branch call dev. You can switch out dev for whatever name you prefer, in order to see the branch you’ve created as well as all available branches you just need to type git branch.
Great we got our first branch but how do we make use of it? Well the green text show which branch we’re currently on which means we need to swap over to the dev branch using this command:
git switch dev
After typing that and using the git branch command you’ll see that your new dev branch is highlighted green showing that you’re currently accessing it.
With this, any changes that you commit will now go into the dev branch of your repository and will not affect any of the files in the master branch. Pretty cool stuff right? In a group project you can have a number of branches for particular parts of the game, for example one member could be working on the inventory system and another working on a quest system, both separate from the dev version of the project but ready to merge when needed.
Now with the knowledge we have on branches let’s make use of the version control part of GIT. First let’s create a new C# script in Unity called Quest.
With that done use git pull origin dev since we’re in the dev branch and go ahead and commit and push this new script that we’ve added. Now let’s say you’ve run into an issue with the Quest script and need to revert to the version of your project before the script was added without it affecting your dev branch, well you can create a separate branch for that older version too to work on!
By using this command it will bring up details of the previous commits that you’re done including the message you added.
Navigate your way to the version before you added the quest script and copy the hashcode after the yellow commit which is comprised of random letter and numbers and then press q to exit the log.
git switch -c branchName
Checkout is an alternative to the switch command, -c establishes that we’re going to be switching to a new branch and you establish the name of it in the branchName part so that can be changed to whatever name you prefer, in the example screenshot I’ve called my branch ‘non-quest-version’. At the end of the command you paste in the version hashcode you copied before and then you hit enter and run a command and that will revert the project to the version you specified but will create a new branch for the version so that it doesn’t affect the dev branch.
Alternatively you can just do git switch -d and add on the hashcode to switch to the previous version without creating a new branch and remaining in the dev branch.
Lastly let’s say we’re happy with the changes done in the dev branch and we want to add it to the master branch, the best way to go about doing this would be to merge the dev branch with the master branch. To do this you’ll need to switch to your master branch using git switch and then use:
git merge dev
This command will combine your dev branch with the master branch and will be production ready for your users if the master branch is the one you’re project is working off of.
All in all it’s pretty cool stuff here and exciting to use once you get going, I don’t think I’ll be able to work on any Unity projects again without it.