Git — Branches, merging and reverting vs resetting

Daniel Kirwan
7 min readMar 19, 2021

--

This is another important topic to cover for version control and it allows you to create a version of a project that doesn’t interfere with the master branch that other team members will pull from. When creating new systems for your project, it is always a good idea to create a development branch that is separate from the master. This allows you to fix any bugs or issues you find before you merge your development branch to the master branch.

This also means that you could have different teams working on separate systems that are then merged into the master branch.

You can check git in the terminal for help at any time by typing

git --help
git — help

You can see above that there are a lot of git commands that can be used but for this, we will be only working with a few of them.

Before we continue with the article, you may have noticed that the terminal window looks different too the last two articles and that is because it is. I have changed to my Windows computer. To get the project here from my mac without a USB stick, I created a folder of the same name VCTest and opened GitBash.

Git bash here

Once the terminal is open I then initialised git in the folder using

git init

I then told which remote sever I want to connect to and then pulled the current project down to my computer.

Once the project is pulled I opened the project in Unity and now I’m ready to go. I went into my project and removed the cube created in the last article. It looked at me funny.

Branches

Let’s get started

git branch

this will show you all of your current branches. If you haven’t already created a branch then you will only have one in the list, which will be master. For this tutorial, I will be creating a dev branch for the project, type:

git branch devbranch

the devbranch in the command is the name I have chosen for the branch. If you now type ‘git branch’ again you will see you have two branches. The highlighted branch is the current branch you’re working on.

Now, we need to switch to the new branch so that we can make changes to our project without the changes affecting the master branch. Open up your Unity project, go back to the terminal and then type:

git switch devbranch

if you named your branch something different then swap devbranch for that name. Switch is a new command that Git is testing in place of ‘checkout’ for swapping branches. If you type:

git — help

you will not see checkout in the list of commands. I will be using switch for this tutorial, but I would recommend starting with checkout as this will help later on with reverting our commits.

I am now in the devbranch, using the switch command above and this means that any changes I make whilst in this branch will not affect the master. To show this, we will make some changes to our Unity project.

I have added 3D capsule to my project and added a script component to the capsule called Enemy.

Newly add items

Now that I have saved my project with these changes add the changes to git for tracking and then commit them.

git add .git commit -m “added enemy and script”

Once this is done, let’s switch back to the master branch and see what has happened in Unity.

git switch master

When you click to see the Unity editor it may ask you to reload the scene, click yes. Once it has reloaded you should see that the changes made on the dev branch are no longer there.

Scripts are not on master branch

I have now finished with my dev branch, so I am going to push my changes to the server on this new dev branch.

git push origin devbranch

This will create a new branch on the server and others will now be able to pull this branch. Let’s quickly take a look on Github to see if there are any changes.

Changes in GitHub

Merging

Now that we have got our bugs and issues fixed on the dev branch we would like to merge it with the master branch. First, we need to switch back over to the master branch. You should know how to do that by now.

To merge one branch into another you first need to be in the branch you want to merge into. So, for me, I want to update the master with the devbranch. I switch back to the master branch and then type

git merge devbranch

This will merge the devbranch into the master branch.

Merging branches

Now that we have merged the devbranch to the master, we need to push those changes up to the server so that others can pull the most up-to-date project.

git push origin master

Now if you take another look on GitHub you will see your newly merged and up-to-date master branch.

Reverting Vs Resetting

But let’s just say we don’t want those changes anymore. What do we do with our project? Throw it away and start again? Absolutely not. We go back in time to one of our commits.

But how do we know which commit we want to go back to? Well, we can check our comments when we committed our changes.

git log

This will show all of the commits for that branch.

Git log

You can see in the screenshot above that this project has three commits. Let's say I wanted to go back to the initial setup of the project. I need to copy the commit code which is next to the word commit in the yellow text, you can also find these commit codes on GitHub.

What we can do is check that the commit we’ve chosen is the correct state we want our project in. So, we can treat the commits as a branch without losing all the data from the latest commit to the one we want to check. This is where I change from using switch to checkout, there are still some issues with using switch like this, so it is best to use checkout.

git checkout 33ec28a31f67fca016aa3869e4a6caaac3a7b36f

You will need to paste in your commit code in place of mine.

If you would like to create a new branch from this commit and are sure it is the correct one, you can type

git checkout -b old-project 33ec28a31f67fca016aa3869e4a6caaac3a7b36f

This creates a new branch of old-project from the commit code you have entered. It also automatically switches to the new branch for you. This is the best option for most projects and going back in time to fix issues.

Another way to change the project to a certain commit is to use reset. WARNING this will delete all the commits after your chosen commit. Make sure that everyone is happy with this.

First, we type

git reset --hard 33ec28a31f67fca016aa3869e4a6caaac3a7b36f
Git reset

It will tell you that the head of the project is now the commit you just entered. But on GitHub the changes aren’t there, that’s because we need to force push the changes to the server, and no I don’t mean with our Jedi powers.

git push --force origin master

Now it’s time to check GitHub and you should now see that your project has lost the commits back to the one you wanted. This means that everyone with access to the repository will also lose those changes.

Git reset changes

Thank you for reading and check back for more on my game developer journey.

--

--

Daniel Kirwan
Daniel Kirwan

No responses yet