Let's Know About Git Branches 📥📤 And How To Implement It

Introduction

 
In my previous article, we have discussed about empty repository with setup details and steps to initialize a git repository. We have also discussed how to push the changes to GitHub empty repository and add multiple files to staging area using a single command. 
 
In this article, I will describe the below-mentioned points in detail. 
  1. Know about git branch
  2. Steps to create branches in GitHub
  3. Why use of git branches
  4. Git branching real time strategy
  5. DevOps Importance In Agile Methodology
  6. Using Branching strategy to solve deployment issue 
Note
Before going through this session, please visit my below-mentioned session related to Git and GitHub.
  1. Introduction To Git 
  2. Steps For Configuring Git With Details  
  3. Let's Know About Git Add, Git Commit, Git Status And Git Push In Details 
  4. Let's Understand How Git Maintains History Of Version Or Commit Ids With Details 
  5. Steps To Initialize A Git Repository And Push The Changes To GitHub In Details 

What Is Git Branch

 
Git branch is nothing but the copy of source code. The source code is when the application is written using backend code like Java, .NET etc and for user interface the code is written using Html, CSS, jQuery, Angular and Bootstrap. Also database related scripts using SQL Server, Oracle DB script, MySQL etc. So, Git branch is the container of all these things. So, that way a developer can easily find the source code, by whom it was written, and using what language and it can be tracked easily. See the below diagram.
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here the left side symbol identifies the git branch. In git branch there is one default branch which is auto created during creation of new repository and another option for creating new branch as shown in diagram.
 
Default Git Branch 
 
Let's Know About Git Branches 📥📤 And How To Implement It
 
New Git Branch
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 

Git Branch With An Example

 
As per the above image, I already have a default branch called main with committed files but I want set of copy of those files. To understand better I describe it with the below Git Branch diagram.
 
Let's Know About Git Branches 📥📤 And How To Implement It
 
Here I have one GitHub repo and in this repo I have 3 branches called Master, Testing and Production. Here two developers, DEV ONE and DEV TWO, are connected to default branch called Master branch. So, whatever files they will commit and push, those changes will be pushed to Master branch only.
 
Let's take an example as these 2 developers are working in a common project called an online food delivery site. So, they finished the user registration page and pushed coding part to Master branch inside that GitHub repo as shown in diagram. Once they commit their code to the Master branch, we are going to take this code and deploy that code to one of the application servers called Dev Application Server. Once deployed then these developers connect to Dev application server and test their code to see whether code works fine or not.
 
Now, whatever set of code is deployed in Dev Application Server, that same set of code will be deployed in Testing Application Server because the developers are doing a round of testing called Sanity testing by sanity integration using RedisInsight. So, as per earlier discussion once the same code is deployed in Testing Application Server or you can call it as Quality Assurance (QA server) then testers will test the application thoroughly with positive and negative test cases. 
 

DevOps Importance In Agile Methodology 

 
Here what Agile says is instead of developing an entire application and releasing it after 6 months and 1 year, we have to release the application feature by feature.  As per earlier discussion on food delivery site, first we release the user registration page then release the sign-in page. After then we add some module to that food delivery site. Next, we add the options called add to cart and payments. Using the same process they are going to release feature by feature. So, as per earlier discussions, our first release for user registration page is pushed to Master branch, and these codes deployed to Dev Application Server and then deployed to Testing Application Server or QA Server. Finally, we move the code to Production Application Server where our end user will be using our application. 
 
So, as per Agile methodology once one functionality is completed and it's deployed to Testing Application Server and testers are started testing these implementations of code it takes 3 days. For these 3 days, the developers will not sit idle. So, they have to start working on the next functionality called sign-In page. After then they move the code to Master branch. As per DevOps automation flow, the developers will take the code from Master branch and they will deploy in Testing Application Server. So, what happens is the testers will not get the stable code because the Sign-In functionality is available in Testing Application Server. The user registration page functionality is not working here that is Testing Application Server. So, what developers deployed the new Sign-In functionality because of that tester will face issue. 
 

The Solution To Above Issue Using Branching Strategy

 
Here I will maintain a separate copy of code. Let's explain this scenario using Branching strategy. First these developers complete a user registration page functionality. Then these developers will push code to Master branch and deploy to Dev Application Server. Once the sanity testing is done by developers thoroughly, next, instead of deploying the code to Testing Application server, these developers will move the copy of code from Master branch to Testing branch. Here the code will be deployed from Testing branch to Testing Application Server and the testers will start testing the code for User Registration functionality. The developers will not start idle and they start implementing functionality of Sign-In page and push the code to Master branch. Next these developers started sanity testing in Dev Application Server. After then developers will push code to Testing branch. Here in Testing branch both User Registration and Sign-In functionality will be available here and both functionality codes are deployed both from Testing branch to Testing Application Server. Finally the testers can test these functionality without any issue because there is no dependency or relation between these copies of codes in Master and Testing branches. 
 
Next these developers will start working on next functionality called payment option. After these changes the developers will push code to master branch and after sanity testing is done, these changes will move to Testing branch and then deploy to Testing Application Server. If the testers will find any defects then these defects will be assigned or informed to developers to fix that issue. After that defects is fixed the developers will push code Master branch then move the same copy to Testing branch. Now once we have tested that functionality in Testing Application Server and if everything is working fine then we will move copy from Testing branch to Production branch as shown in above diagram. Now we deploy the code from Prodcution branch to Production Application Server. Like this we need different copies of branches. This one is the best example for understanding of Branching strategy in realtime scenario. Here we can create n number of branches and also we can use any branch for any environment i.e. Development, Testing or Production. There is no restrictions at all.
 

Understanding The Uses Of Branches For Environment In Real-Time World

 
In Real-Time scenarios, there are various environments as shown below.
  1. Development -- It can be used for Development. Developers will use this server.
  2. Testing / QA --It can be used for testing or for QA purpose. Here QA can be split into QA1, QA2 .... QA6 as per requirements. Testing or QA people will use this server.
  3. Preprod --Before go for production or live server, This environment will be uded. Mostly QA people or senior most people of this specifc project will use.
  4. Production --This can be used for end user or client purpose. Client can use this server. This is the final server and it reaches to end user or client. 
Here, I show you the branch is used for the environment as shown below.
  1. Development branch -- For Development environment.
  2. Testing branch -- For Testing environment.
  3. Preprod branch -- For Preprod environment.
  4. Master branch -- For Production environment.
There is no mandatory rule that for each environment we create n number of branches. That is based on decision of our Organization. It can be less than that or more than that. 
 

Steps To Create And Manage New Branch 

 
As per previous article, I created a repository called SatyaTwo and it comes under branch called main as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here I will create a branch called Testing as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here I type the name of the branch called Testing and below you can see a line that shows Create branch: Testing from 'main'. That means when we create a new branch that should not be empty. We have to create this new branch from some other existing branches. Here my from branch is main branch That means whatever changes are present in my main branch that will be copied to this Testing branch. Testing branch can have same copy of files as there in main branch.
 
Here you can see your new branch is created as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here we can see the same no. of commits and no. of files are shown in Testing branch as there in main branch as shown below. Here we can see the new branch with selected option. 
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here we can see same no. of files with commit messages as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here we check one scenario that whatever changes made in main branch will not affect the Testing branch. Let me explain with an example.
 
Here I select main branch and select MyFile.cs file for modification as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here I modified the MyFile.cs with below lines.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
After Commit changes the file MyFile.cs will be shown as mentioned below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
If I go to SatyaTwo repo for main branch and check the no. of commits are 6. The MyFile.cs with new commit message as modified in main branch. The committed time is shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Let's check the same file called MyFile.cs status i.e. no. of commits, commit message of MyFile.cs, Committed time under Testing branch as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here we got that the change made in file called MyFile.cs under main branch. That change does not affect the same file MyFile.cs under Testing branch.
 
Let's check also the content of MyFile.cs under Testing branch. The new content added in MyFile.cs under main branch is not affected in same file under Testing branch.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Like this you can maintain this separate copy of source code using branches.
 
 

Maintain Separate Copy Of Source Code Using New Branch And Testing Branch

 
Here I create a new branch called Preprod from Testing branch instead of main branch. So, for this we need to select the Testing branch and then create new branch called Preprod as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here after the successful creation of Preprod branch, We can see same no. of commits, files with commit messages and committed time as same under Testing branch.
 
Let's Know About Git Branches 📥📤 And How To Implement It 
 
Here you can see total no. of branches that are created in SatyaTwo repository as shown below.
 
Let's Know About Git Branches 📥📤 And How To Implement It
 

SUMMARY

 
In this write-up, we have learned the below details,
  • Know about git branch
  • Steps to create branches in GitHub
  • Why use of git branches
  • Git branching real time strategy
  • DevOps Importance In Agile Methodology
  • Using Branching strategy to solve deployment issue
Thank You & Stay Tuned For More****