Enroll in Selenium Training

"Branch" is not an unfamiliar word. It is too common to hear the words such as the branch of a tree, branch of a bank, a branch of science, etc. We use them quite often in our lives. Today, you will link another meaning to the word "branches" in your already existing vocabulary. This new type of branch is Git Branch. The developer community often uses phrases such as "killer feature" for the concept of a branch in Git on online communities such as StackExchange and StackOverflow, which reflects the importance of Git Branch. In this tutorial, we will cover the following topics about the branches in Git:

  • What is a Branch in Git?
    • Why do we need a Branch and Why Branches are Important?
    • Real-Life Depiction Of Branches In Git
    • Different Operations On a Git Branch

What is a Branch in Git?

Branch in Git is similar to the branch of a tree. Analogically, a tree branch is attached to the central part of the tree called the trunk. While branches can generate and fall off, the trunk remains compact and is the only part by which we can say the tree is alive and standing. Similarly, a branch in Git is a way to keep developing and coding a new feature or modification to the software and still not affecting the main part of the project. We can also say that branches create another line of development in the project. The primary or default branch in Git is the master branch (similar to a trunk of the tree). As soon as the repository creates, so does the main branch (or the default branch).

master branch in git

Branch in Git is "lightweight". Light in terms of the data they carry and the little mess they create. In other version control systems such as SVN, creating branches is a cumbersome process. Moreover, once the branch creates, the whole main code from the main branch gets copied to the newly created branch. Whereas, in Git, the code is separated only from the point of creation of the branch. Once the creation of the new branch happens, we can switch to this branch and start development.

Why do we need a Branch in Git and Why Branches Are Important?

Git branches come to the rescue at many different places during the development of a project. As mentioned above, branches create another line of development that is entirely different or isolated from the main stable master branch. There are many advantages to doing so.  Consider that you are developing a project with your team, and you finish a feature. You contact the client to request them to see the feature, but they are too busy, so you send them the link to have a look at the project. Okay, it's lengthy to explain in words. Let's see the same project development in different phases through images.

Project Development through linear development.

You have been working on a project with the client being happy until this point.

81833584_636926650400109_2679153925835194368_n

Now, you decide to develop a feature and start developing it on the same code (denoted by blue commits).

feature_linear

You show this to your client.

In the meantime, you decide to develop another feature (let say xyz) and wait for the client's approval (xyz denoted by brown commits).

new_commits_linear

The client disapproves of the feature (blue commits) and requests to delete it (denoted by grey color depicting deletion).

delete_feature

Now, since you were following the linear development method, you need to delete the complete code and go through the hectic process of adjustments and removing glitches repeatedly to achieve the following:

feature_removed

Developing the project through branching.

Let see the same scenario by using the Git branching technique.

You have been working on a project with the client being happy until this point.

master1

After that, you decide to develop a feature and create a new branch called feature for the same purpose and start working on it.

branch_feature1

You show this to your client.

In the meantime, you decide to develop another feature and wait for the client's approval.

feature_xyz_branch

The client disapproves of the feature and requests to delete it.

Now, since you were following the branched strategy, you need to remove the branch, and all the remaining code remains as it is. The new feature can be easily added to the master branch to achieve the following.

Merging branches in Git

Branches give you the freedom to independently work on different modules (not necessarily though) and merge the modules when you finish developing them. It might sound a cumbersome process, but git branches are swift to be created and destroyed. Just a simple command can perform these processes, and they are very cheap, considering the size they take. Branches in Git help the team, which are in different parts of the world, work independently on independent features that would ultimately combine to produce a great project. Moreover, the branches are very flexible. Using branches does not mean you are using them for different features.

depiction of branches in git

We can use the branch in git for any reason we want. We create different branches for different teams working on the project (or the same module). Additionally, one can create them for any other feature you are creating in the project. The creation of a branch can happen for different lines of development, and there are numerous other reasons. It is totally up to you as to why you want to create the branches. A simple example related to real-life will help you understand branches more clearly.

Real-Life Depiction Of a Git Branch

To understand the concept of Git, let's take a real-life scene and understand how convenient it is to work on a project when we are using Git.

Let's assume you are working on a project along with your friend. You both are working on two different features and hence are working on two different branches. We can see it in the below image.

two different branches in git

Once you both finish work on your particular features, these feature branches were merged into the master branch and accepted into the main stable code.

feature branches merged into master branch

Later on, you start working on a different branch and a distinct feature. But, due to some reason, the feature is not required this time. Therefore, the main master branch does not include it. The branch below in grey shows that it has to delete without inclusion.

delete branch without inclusion

Although the team is working on the master branch continuously, sometime later, an urgent fix pops up that should urgently address. The team fixes it and merges it into the master.

hot fix merged into the master

Meanwhile, when this fix came, to add some additional functionalities, it was pulled to a feature branch.

pulling hot fix

After working on this pulled branch, it finally merges to the master branch.

pulled branch merged into master branch

This section describes the real-life scenarios that you can encounter. It is a very complicated process if you would have been working with the linear line of development, i.e., every commit succeeds the previous commit without the concept of branches.

Different Operations On Branches

Branches are the heart of Git. From the time of creating the repository until its deletion, everything will involve branches. So this is a concept to remember and analyze carefully. In this tutorial, we discussed the definition and importance of branches. This complete section of branches on ToolsQA will take you through some operations and concepts that will help you in efficiently using Git. Please refer to the below explanation:

  • Create a Branch: This is the first step in the process, you can start on a default branch or create a new branch for the development.
  • Merge A Branch: An already running branch can merge with any other branch in your Git repository. Merging a branch can help when you are done with the branch and want the code to integrate into another branch code.
  • Delete A Branch: An already running branch can delete from your Git repository. Deleting a branch can help when the branch has done its job, i.e., it's already merged, or you no longer need it in your repository for any reason.
  • Checkout A Branch: An already running branch can pull or checkout to make a clone of the branch so that the user can work on any of them. Pulling a branch can help when you don't want to disturb the older branch and experiment on the new one.

I hope the concept of branches is clear now. Does this post leave us with any queries, such as how do we create branches in Git? And how does that work behind the scenes? Don't worry; we will explain this in our next tutorial in this section to answer all your queries. By now, you must have some idea about the concept of branches in Git and why they are so crucial in Git. In the next tutorial, we will cover how the users can create Git branches directly on their local systems and a few more operations on branches.

Common Questions On Branches

What does the local branch mean in Git?

A local branch in Git means the branch, which is available only on your local machine. The team members can not see and access this branch.

Is it necessary to name the main branch as a master branch?

No, the main branch can be any branch you want. By default, when you create a repository, the main branch is named the master branch. It is the base branch or the default branch. You can go through How to set up default branches in Git to know more.

What is a Hot-Fix branch in Git (By convention)?

Hot-fix branches are the branches that patch and deliver a release quickly. O remembered that none of the branch names is necessary to use. A user can name any branch anything. But, as a part of GitFlow, the names such as master, feature, develop, hot-fix, etc. should be followed as everyone knows them, and their meanings are precise. It is similar to UML diagrams. They are universal.

What is Gitflow Workflow?

Gitflow workflow is a branching model that organizations strictly follow to avoid confusion. It does not have extra features or commands. Gitflow workflow determines the branch roles and meanings that everyone knows. It is instrumental since the distribution of the team members may be global.

Can I rename a branch after creation?

Yes, branches can be renamed by executing the command git -m <old_name> <new_name>. Although one should avoid this operation since the branch name can confuse the team members.

How to save branch commits in the Git repository?

Saving and referencing Branch commits, along with almost everything in a Git repository happens through a unique hash code. Whenever a user commits on a branch, a unique hash code generates. To know more about these concepts, please go through Git Commit and Git Log tutorials.

Git Fetch and Git Pull
Git Fetch and Git Pull
Previous Article
Git Create Branch
Git Create Branch
Next Article

Similar Articles