Let's Understand About Git Branches 📥📤 And Its Real Time Uses

Introduction

In my previous article, we discussed the git branch and the steps to create and manage the git branch. Also, we discussed the on Importance of DevOps In Agile Methodology and branching in real-time strategy. This article is part 2 of my previous article based on the git branch.

In this article, I will describe the below-mentioned points in detail.

  1. Create a branch in a local git repo
  2. Push the branch from the local Git repo to the GitHub
  3. Important Git commands to create and push the branch
  4. Understand one scenario about the git branches relationship

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
  6. Let's Know About Git Branches And How To Implement It

Create and manage a Branch in the Local Git repository

Here, I will show you the steps to create a new branch in a local git repository and then push this same branch to the GitHub repository. I already created a GitHub repo calledSecondRepo. Please go to my previous articles link as mentioned above. In this repo, I got 4 commits, 1 branch called main, and 3 files as mentioned below.

Git Repository

Here the branch called main is the default branch. I clone this repository in my local as shown below.

Files

I will check the list of branches using the command git branch and here * indicates the current selected branches in GitHub as shown below. If there are more branches then we can identify the currently connected branch by checking the * mark on the left side of the branch name.

Git branch

Next, I will create a new branch using the command git branchTestingin local git repository as shown below. HereTesting is not but my branch name is in a local git repository.

Local git

Now, I will check the list of branches using the command git branch as shown below. Here we will see 2 branches called the main and Testing branches.

 Testing branch

Here I want to make my Testing branch as current working branch or active branch instead of the main branch in my local git repository as shown below. For this, we use a command called git checkout Testing. HereTesting is nothing but my branch name. Here you can see the Testing branch is switched successfully for a current working branch.

Working branch

Now check for the current working branch as shown below. You can see the * mark on the left side of the Testing branch. That means the Testing branch is the current working branch.

 Current working

Now I connect to the Testing branch. So, the changes or modifications will happen that will affect our Testing branch only. Here we check this current connected branch in the HEAD file of our local directory as shown below.

 Modification

The current working branch is pointing to the path of our local git repository as shown below. If we switch branches using the command git checkout main then the main branch will be the current working branch and the HEAD file will contain the path as ref: refs/heads/main.

HEAD file

We can see the latest commit ID of the Testing branch as shown below.

 Latest commit ID

Steps To Add, Commit, And Push Changes Under the Testing Branch.

Here I will modify one file calledDateDetails.cs under the Testing branch as shown below.

DateDetails

Now we check the status as shown below. This file is modified and not available for staging.

Staging

Here I added to staging and committed changes to the local git repo and Testing branch as shown below.

Pic 1

Commit changes

Pic 2

Commit

Now, I want to push the changes from the local git repo to the GitHub repo using the command git push origin Testing under the Testing branch. After successfully pushing these changes to the GitHub repo my new branch called Testing will be shown with a modified file in the GitHub repo as shown below.

Pic 1

File pushed

Pic 2

Testing

Now check the new content of the fileDateDetails.cs made under the Testing branch as shown below as compared with the same fileDateDetails.cs under the main branch.

DateDetails.cs content under the Testing branch.

Content under Testing

DateDetails.cs content under the main branch.

Main branch

Steps to create a new Branch in Local Git Repo under the Testing Branch

To make a new branch under the Testing branch, we need to point our local git repo to the current working branch called the Testing branch. That means the new branch called Preprod will contain the same changes as there under the Testing branch instead of the main branch.

Total branch

Let's Understand One Scenario As Below.

But here I create one scenario in which I create a Development branch and through the development branch, we connect to the Preprod branch. For this, we create a Development branch using commandgit checkout -b Development. Using this single command we can create a new branch as well as the current working branch as shown below.

Preprod to prod

Here I will rename the branch name from Preprod to Prod under the Development branch as shown below using the command git branch -m 'Old branch name' 'New Branch name'.

Old Branch

Now if any changes are made and pushed then the branch called Prod will be pushed into my GitHub repository.

Summary

In this write-up, we have learned the below details.

  • Create a branch in a local git repo
  • Push the branch from the local git repo to the GitHub
  • Important Git commands to create and push the branch
  • Understand one scenario about the git branches relationship

Thank You & Stay Tuned For More


Similar Articles