The importance of Branching using Git and Github
Branching for Version Control with Git and Github
Branching is a very important aspect in version control
When using Git, branches are an important part of the development process. They are essentially a new area where the developer can add new features or fix a bug without altering the main program directly. Whenever you want to make a change, it is recommended you create a new branch to do this, in order to encapsulate your changes.
In the diagram above, we can see that a new branch is created in order to fix a bug. When it has been fixed and tried out, it can then be merged back into the master branch to include it into the man program.
When we creat a repository, the main branch that is created is the master branch. In your git terminal, in the proper folder, and type:
This command will show you the names of the branches that exist in the project and which is the active one for you.
This shows that we only have one branch, the master branch and the asterix next to the name shows that it is currently the active branch.
Creating a new branch
In order to create a new branch, we can use the following command:
This will create a new branch with the name you just gave it.
As we can see, after we create the new dev branch, when we use the git branch command, it now shows two branches dev and *master. The asterix still shows we are in the master branch.
Changing to another branch
In order to move into another branch, we can use the following commands:
As you can see, when we use the git branch command it shows us an asterix “*” next to the dev branch. This means we are now in that branch.
So, whatever we do in this branch, will not affect the main branch until we decide to merge them.
Lets say you have created a Unity project and have created some more branches. So, we have a master, dev and inventory branches.
So, you make a change in the dev branch. Now you want to work in the inventoy branch so we move to that branch using the git checkout inventory command.
But you realise that the changes that you made in the dev branch aren’t showing up in the inventory branch. This is because you haven’t merged the inventory and dev branches.
To do this, we must follow the following steps:
- Back in the dev branch, we must apply these commands to get the files ready for merging:
The git status shows us if there are any files or folders that haven’t been commited.
If there are, the git add . command adds them to be commited.
The git commit -m “changes made in dev” command will commit these changes on the local machine.
2. Moving back into the inventory branch git checkout inventory. We then use the following command to merge the dev branch into the inventory branch.
Now the inventory branch is the same as the dev branch. If you were to make any changes to the inventory branch and want those changes to be reflected in the dev branch, we should follow the same steps as before but starting in the inventory branch and then moving to the dev branch issuing the merge control there.
Here is an example:
In the dev branch, we can see in this screen shot that there is a cube in the main scene and a cube script in the assets. I will now add another script to the assets folder.
Here I added a new script to the assets folder. If I move to the inventory folder with git checkout inventory.
When we refresh the Unity project, we can see that the new script is not present.
So this means we have to go back to the dev branch, add and commit the files, return to the inventory branch and merge it with the dev branch.
This would look something like this:
Now that both branches have been merged, they contain the same files and are both up to date.
In my next article, I will write about restoring a project to a previous state. See you there.