What is a Branch in Git and How to Use It - Ultimate Guide

Developing a website or software isn't easy, a team of developers will be developing a new feature, other team will be testing whether the built feature works as expected, other might be fixing the bugs and so on. Managing these different versions of same code base must be a little tricky. Here comes the concept called branch in git which is used as a pointer to a snapshot of your changes.

When we talk about branches in git these are the major questions that arises in our mind.

  1. What does a branch really do?
  2. Why should you ever merge or rebase a branch?
  3. What's a good way to organize your branches?

If you are new to git check on our previous blog Complete Guide About Git For Beginners to learn the basic usage in Git.

I'll cover the following topics in this article.

  1. What are Git Branches?
  2. Why Do We Need a Branch in Git?
  3. Workflow In Git Branch
  4. Creating Branches
  5. Deleting Branches
  6. Options In Branches
  7. How do I Turn My Local Branch Into a Remote Branch?

1. What are Git Branches?

In the world of software development, a branch is a code (or source) change that diverges from the original codebase. The branch and its code can represent a new feature, a bug fix, or perhaps a new release candidate (RC) of an existing product.

A branch is simply a pointer to the latest commit in a given context.

Branching is one of the most commonly used features of version control systems which helps you and your team to work on multiple versions of your code in parallel, like Git. In Git, it's a way of keeping your work separate from your master branch, without creating a new branch altogether. While it can also be used as a means of working on different features and different version combinations at the same time, it is also used to collaborate with other developers.‌

Branch
Branch

In fact, a branch is a useful tool in many scenarios, as we'll see:

  • to work on a new project feature without affecting the master branch
  • to isolate the current working version from the master branch
  • to check that the latest version works as expected
  • to test a new release of your own project

2. Why Do We Need Branch In Git?

Git Branches can be used at many different places in the development process. It creates another line of development which is completely different form the Master branch.

Problem In Linear Development:

You might have developed a feature and ask your client to take a look at it. When your clients are too busy, so you send them the link and ask your client to check it. At the mean time you develop another feature named abc and wait for the client to approve the previous developed feature.

The client checked and might be unhappy with the previous development and asks you to delete it. If you are following the linear development process you have to delete the complete code and you have to start developing from the scratch again.

Solution Using Git Branch:

This kind of issues can be resolved using Branching in git.

You can create a new branch called feature in the default branch master and start working on it. When you ask your client to review the newly developed feature, you can also create a new branch and develop some other new feature too.

Incase your client disapproves the feature you can simply delete the branch instead of deleting the entire code and start working on another one without any frustration.

Branches can be developed for different lines of development. You can work independently on different modules using git branches and merge them once you complete developing them. Git branches can be easily created and deleted by performing simple process.

3. Workflow In Git Branch

The branching workflow consists of five types of branches with different roles.

  1. Master Branch
  2. Feature/Topic Branch
  3. Develop Branch
  4. Hotfix Branch
  5. Release Branch
Git Branch and Its Workflow
Git Branches and Its Workflow

#1 Master Branch

Master is the default branch in Git. Once you are done cloning a project from remote repository the resulting local repository contains a single branch which is called "Master". This branch is currently known as Feature Base.

Master Branch
Master Branch

Until you create or switch to a new branch your commits will be automatically stored in the Master Branch.

#2 Featire/Topic Branch

It is the short-lived branch in Git. To develop a new feature and to avoid overriding each other's changes in a team, you can create a new branch when you are about to develop a new feature. Feature Branch is the copy of the main code base. This branch will be in your local repository throughout the development lifecycle.

Feature Branch

Once it is tested and reviewed by your clients you can integrate it with the rest of the team by merging the changes into the remote repository.

#3 Develop Branch

This is the stable branch which contains pre-production code. Once the features are completely developed they are merged into the Develop branch.

#4 Hotfix Branch

Every major software project has hotfix branches. These branches are created directly off the latest commit on the master / main branch. Hotfixes are a fairly common way of fixing bugs or feature enhancements in software development.

They're easy to create, relatively fast to deploy, and can be released more quickly than a complete release.

#5 Release Branch

By creating a dedicated branch for releases makes it possible for the team to focus on both release and developing a new feature at the same time. It is a supporting branch which lets you to do preparation for a production release i.e. bug fixing, documentation and so on.

Once you are ready to release, you can merge the changes to the master and add a tag such as v1.0, v1.1 etc.. to the newly merged commit. You should also merge the changes to the development branch since the release branch may contain bug fixes or other latest changes.

5. Creating Branches

One of the biggest advantages in using git is the power and flexibility of its branching model. In the Git version control system, I think people use the term "branch" a lot more than they should. You can create, merge, and pull branches.

#1 To Create a New Branch Based On the Current Head

git branch <new-branch>

You can create a new branch with the currently checkedout (HEAD) branch using the simple command git branch.

#2 To Create a Branch Based On the Existing One

git branch <new-branch> <base-branch> 

(OR) 

git checkout -b <new_branch_name> <base-branch>

You can use the above two commands to create a branch in the existing one. The alternative command creates a new branch on the base branch and switches to the newly created one.

#3 How do I create a new branch from a specific commit?

 git branch <new_branch> <commit_id>

#4 How do I create a new branch from a specific tag?

git branch <new_branch> <tag> 

#example 

git branch testing v1.2

#5 How do I create a new branch in a remote repository?

git push -u origin <local-branch>

It is obvious that once we complete working in our local branch we would want to commit the changes in the remote repository.

The "-u" flag tells Git to establish a "tracking connection", which will make pushing and pulling much easier in the future.

6. Deleting Branches

You can delete an already running branch from git repository once the branch has done its job. Checkout the current working branch using git checkout command before deleting any branch.

#1 Delete an Existing Branch

git branch -d <branch_name>

The above command lets you delete a branch which has already been pushed into the remote repository.

To forcefully delete a branch you can use the below command.

git branch -D <branch_name>

This command lets you delete the branch even if it hasn't been merged or pushed.

#2 Delete a Branch In Remote Repository

git push <remote> --delete <branch>

You can delete the branch in the remote repository with the above command.

6. Options In Branches

There are also commands in Git Branch other than creating and deleting.

To Rename a Current Branch

git branch -m <branch_name>

You can rename a branch you wish using the above command.

To List all the Branches

git branch 

# OR

git branch -a

List all the branches in your repository with the foll0wing command.

To List Branches In Remote Repositories

git branch -r

7. How do I Turn My Local Branch Into a Remote Branch?

We often work in local branches but when you want to share the changes you made you will need a remote repository using which your team mate or colleague can view and test the changes made. It's a popular way to keep code up to date and avoid having to keep the old version and the new version in sync.

Developers can use git to collaborate with other developers, without needing to be in the same physical space.

Initially when you create branches using the git command it will not reflect in the github repository. I have created a branch called development locally.

Before Converting Local Branch Into Remote Branch

To convert your local branch into remote branch follow the below steps:

Step 1: Create a branch locally using the command

git branch <branch_name>
Create Branch In Git

Here I have created a branch named development with the command:

git branch development

Step 2: To ensure the branch has been created or not run the following command:

git branch 

(or) 

git branch -a
Listed Branches

This command lists the branches in your repository.

Step 3: To convert the development branch into remote branch run the following command:

git push origin development
Remote Branch

To make sure whether the branch has been converted or not type the following command in the terminal:

git branch -r

This command fetches all the remote branches in your repository.

Listed Remote Branches

In the above screenshot I have run the command to check whether it converted the development branch into remote branch or not using the git command. It listed all the remote branches in the repository.

Refresh your github repository to check whether the changes are reflected or not. You can see the changes in the below screenshot.

After Converting Local Branch Into Remote Branch

These are some basic steps to convert your local branch into a remote branch using git commands.

Wrapping Up

One of the most frequently used operations in Git are Branches. Without knowing how to develop branches and the workflow of it one cannot master in Git. Understand how git branches works and organize your code using them. This helps for the larger independent development teams to collaborate without conflicting with each other.

To know the complex operations in Git you can refer the official documentation in Git. To operate further in resulting branches Git checkout command is used often. Learn more about Git Checkout in our previous blog post.

Hope you all find this article useful. Share your thoughts with us in comment section!!!

Atatus

#1 Solution for Logs, Traces & Metrics

tick-logo APM

tick-logo Kubernetes

tick-logo Logs

tick-logo Synthetics

tick-logo RUM

tick-logo Serverless

tick-logo Security

tick-logo More

Vaishnavi

Vaishnavi

CMO at Atatus.
Chennai