Concept of Branch in Git

Last updated on May 28 2022
Meetesh Jain

Table of Contents

Concept of Branch in Git

Git Branch

A branch is a version of the repository that diverges from the main working project. It is a feature available in most modern version control systems. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes. So, it is complex to merge the unstable code with the main code base and also facilitates you to clean up your future history before merging with the main branch.

Concept of Branch in Git
Concept of Branch in Git

Git Master Branch

The master branch is a default branch in Git. It is instantiated when first commit made on the project. When you make the first commit, you’re given a master branch to the starting commit point. When you start making a commit, then master branch pointer automatically moves forward. A repository can have only one master branch.

Master branch is the branch in which all the changes eventually get merged back. It can be called as an official working version of your project.

Operations on Branches

We can perform various operations on Git branches. The git branch command allows you to create, list, rename and delete branches. Many operations on branches are applied by git checkout and git merge command. So, the git branch is tightly integrated with the git checkout and git merge commands.

The Operations that can be performed on a branch:

Create Branch

You can create a new branch with the help of the git branch command. This command will be used as:

Syntax:

$ git branch  <branchname>

Output:

Concept of Branch in Git
Concept of Branch in Git

This command will create the branch B1 locally in Git directory.

List Branch

You can List all of the available branches in your repository by using the following command.

Either we can use git branch – list or git branch command to list the available branches in the repository.

Syntax:

$ git branch –list

or

$ git branch

Output:

Concept of Branch in Git
Concept of Branch in Git

Here, both commands are listing the available branches in the repository. The symbol * is representing currently active branch.

Delete Branch

You can delete the specified branch. It is a safe operation. In this command, Git prevents you from deleting the branch if it has unmerged changes. Below is the command to do this.

Syntax:

$ git branch -d<branchname>

Output:

Concept of Branch in Git
Concept of Branch in Git

This command will delete the existing branch B1 from the repository.

The git branch d command can be used in two formats. Another format of this command is git branch D. The ‘git branch D’ command is used to delete the specified branch.

$ git branch -D <branchname>

Delete a Remote Branch

You can delete a remote branch from Git desktop application. Below command is used to delete a remote branch:

Syntax:

$ git push origin -delete <branchname>

Output:

Concept of Branch in Git
Concept of Branch in Git

As you can see in the above output, the remote branch named branch2 from my GitHub account is deleted.

Switch Branch

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command. To switch between the branches, below command is used:

$ git checkout<branchname>

Switch from master Branch

You can switch from master to any other branch available on your repository without making any commit.

Syntax:

$ git checkout <branchname>

Output:

Concept of Branch in Git
Concept of Branch in Git

As you can see in the output, branches are switched from master to branch4 without making any commit.

Switch to master branch

You can switch to the master branch from any other branch with the help of below command.

Syntax:

$ git branch -m master

Output:

Concept of Branch in Git
Concept of Branch in Git

As you can see in the above output, branches are switched from branch1 to master without making any commit.

Rename Branch

We can rename the branch with the help of the git branch command. To rename a branch, use the below command:

Syntax:

$ git branch -m <oldbranch name><new branch name>

Output:

Concept of Branch in Git
Concept of Branch in Git

As you can see in the above output, branch4 renamed as renamedB1.

Merge Branch

Git allows you to merge the other branch with the currently active branch. You can merge two branches with the help of git merge command. Below command is used to merge the branches:

Syntax:

$ git merge <branchname>

Output:

Concept of Branch in Git
Concept of Branch in Git

From the above output, you can see that the master branch merged with renamedB1. Since I have made no-commit before merging, so the output is showing as already up to date.

 

Git Origin Master

 

The term “git origin master” is used in the context of a remote repository. It is used to deal with the remote repository. The term origin comes from where repository original situated and master stands for the main branch. Let’s understand both of these terms in detail.

 

Git Master

 

Master is a naming convention for Git branch. It’s a default branch of Git. After cloning a project from a remote server, the resulting local repository contains only a single local branch. This branch is called a “master” branch. It means that “master” is a repository’s “default” branch.

Concept of Branch in Git
Concept of Branch in Git

In most cases, the master is referred to as the main branch. Master branch is considered as the final view of the repo. Your local repository has its master branch that always up to date with the master of a remote repository.

Do not mess with the master. If you edited the master branch of a group project, your changes will affect everyone else and very quickly there will be merge conflicts.

 

Git Origin

 

In Git, the term origin is referred to the remote repository where you want to publish your commits. The default remote repository is called origin, although you can work with several remotes having a different name at the same time. It is said as an alias of the system.

Concept of Branch in Git
Concept of Branch in Git

The origin is a short name for the remote repository that a project was initially being cloned. It is used in place of the original repository URL. Thus, it makes referencing much easier.

Origin is just a standard convention. Although it is significant to leave this convention untouched, you could ideally rename it without losing any functionality.

In the following example, the URL parameter acts as an origin to the “clone” command for the cloned local repository:

$ git clone https://github.com/ImDwivedi1/Git-Example

Some commands in which the term origin and master are widely used are as follows:

Git push origin master
Git pull origin master

Git has two types of branches called local and remote. To use git pull and git push, you have to tell your local branch that on which branch is going to operate. So, the term origin master is used to deal with a remote repository and master branch. The term push origin master is used to push the changes to the remote repository. The term pull origin master is used to access the repository from remote to local.

So, this brings us to the end of blog. This Tecklearn ‘Concept of Branch in Git’ blog helps you with commonly asked questions if you are looking out for a job in DevOps. If you wish to learn Git and GitHub and build a career in DevOps domain, then check out our interactive, Version control with Git and GitHub Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Version Control with Git and GitHub

 

Version Control with Git and GitHub Training

 

About the Course

 

Tecklearn has specially designed this Git and GitHub Training Course to advance your skills for a successful career in this domain. The course will cover different components of Git and GitHub and how they are used in software development operations. The course consists of important concepts like: branching & merging, how to deal with conflicts, rebasing, merge strategies, Git workflows and so on. You will get an in-depth knowledge of these concepts and will be able to work on related demos. Upon completion of this online training, you will hold a solid understanding and hands-on experience with Git.

Why Should you take Git and GitHub Training?

 

Average salary of Git and GitHub Professional is $85k – Indeed.com
Amazon, Google, Facebook, Microsoft, Twitter, & many other MNC’s worldwide use Git across industries.
According to Grand View Research, the DevOps market size is estimated to be worth $12.85 billion by 2025. DevOps professionals are highly paid and in-demand throughout industries including retail, eCommerce, finance, and technology.

What you will Learn in this Course?

 

Introduction to DevOps

What is Software Development
Software Development Life Cycle
Why DevOps?
What is DevOps?
DevOps Lifecycle
DevOps Tools
Benefits of DevOps
How DevOps is related to Agile Delivery
DevOps Implementation

Git and GitHub

What is version control
Version Control System (VCS) Products
Types of VCS
What is Git
Why Git for your organization
Install Git
Common commands in Git
Working with Remote Repositories
GitHub
Git Installation
Git Lifecycle
GitHub (Push, Pull Request)
GitHub Workflow

 

 

 

0 responses on "Concept of Branch in Git"

Leave a Message

Your email address will not be published. Required fields are marked *