Git Branching & Reverting

This is one of the most crucial parts of using Git. As the name “branch” suggests, branching allows us to work with other team members simultaneously, without interrupting each other’s work, just like the different branches of a tree. Despite they are connected to the same core, they are discrete, until you “merge” them.

As expected, the master branch needs to be protected all the time. Just like the body of the tree, the master branch is the part that keeps our project on it’s feet. By developing working on different branches, we aim to keep the master branch as clean as possible. We update the master branch when we are sure that there are no bugs on the development branch. Certainly, currently unknown bugs might come into surface in the future. But don’t worry, we can fix it. I will mention how we fix our mistakes at the end of this article.

The idea is to create at least one different branch than the master branch, such as a developer branch. As far as I know, there is no limit on the maximum number of branches that you can create so we shouldn’t worry about it.

To create a new branch we simply type git branch (branch name). For instance, we have created a new branch called “dev”. To make sure that we have successfully created a new branch, we can list the available branches, we simply type git branch.

As you can see, the we can see the branch we are on at the right side of the project directory and on the list with a star next to it. We have successfully So how do we switch to a different branch?

By using git checkout (the branch you want to switch) or git switch (the branch you want to switch), you can easily dive into different branches. Before Git version 2.23, we were using only checkout to switch in between branches. With Git 2.23, Git introduced switch command. Despite we can use both of them to switch branches, these are not the exact same commands. For instance, checkout command is also being to restore our files (we will come into this shortly) but switch command does not have such functionality.

As you can see below, we have successfully switch to dev branch. We can observe that now it is stated that we are on dev branch both next to directory and with a green, star added branch name.

Now, we have 2 different branches to push our changes according to our desire, following the pull — commit — push rule.

I was actually really surprised when I observed that the Unity Project screen was being changed according to the branch I am on. For instance, if you have committed and pushed different things to master and to dev branch, you can observe the difference on your Unity project screen when you switch from one to another. This was actually the “A-HA” moment for me to love Git even more. Try it yourself, make changes on dev and push it to the server without merging with master. Switch to one, and Unity will ask you to reload the scene.

So another question comes to the mind. What do we do when we believe the dev branch is safe to push the changes to the master branch? Easy again. We type in the following respectively:

1- Git switch master (make sure that we are on the master branch as we are going to push changes to the master branch)

2- Git merge dev

3- Git push origin master (don’t forget to push)

As can be seen above, rather than pull commit push, we just switch merge and push when we want to merge it. To be more precise, both dev and master branches still exist in the project. By merging, we have pushed the dev branch to the merge up to corresponding time. Now, we can continue to work on our dev branch and the changes will not be logged to master branch until we merge and push it again.

Lastly, what happens if we find a bug or an error at a later stage of the project?

Git log, lists us the change logs that we have committed to the corresponding branch. I have tried to demonstrate it on dev branch, as it may occur on any branch we are working on including master.

We can also reach the corresponding commit numbers from github as following:

You can observe that these are the same numbers on both Git and github. You can select either one of them and right click- copy. By typing git checkout -b (new branch name) (the code you have copied), we create a new branch to work on, which is actually at a point of commit we have copied.

Why do we create a new branch rather than just deleting it? Bugs may come to surface at any state of the project. Thus, a bug which has occurred at the early stages of the project may cost us to lose excessive amount of data and work. If we reset the branch rather than reverting, we will lose all of the data after the commitment point.

By creating a new branch, we have the opportunity to fix the bug without losing the work completed after the commitment point.

Unity Developer