Introduction
In my previous
article, we have discussed git branch and steps to create and manage git branch. Also, we discussed on Importance of DevOps In Agile Methodology and git branching in real time strategy. This article is part 2 of my previous article based on git branch.
In this article, I will describe the below-mentioned points in detail.
- Create branch in local git repo
- Push the branch from local git repo to GitHub
- Important Git commands to create and push branch
- Understand one scenario about git branches relationship
Note
Before going through this session, please visit my below-mentioned session related to Git and GitHub.
Here, I will show you the steps to create a new branch in local git repository and then push this same branch to GitHub repository. I already created a GitHub repo called SecondRepo. 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.
Here the branch called main is the default branch. I clone this repository in my local as shown below.
I will check the list of branches using 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 * mark on left side of the branch name.
Next, I will create a new branch using command git branch Testing in local git repository as shown below. Here Testing is not but my branch name in local git repository.
Now, I will check the list of branches using command git branch as shown below. Here we will see 2 branches called main and Testing branch.
Here I want to make my Testing branch as current working branch or active branch instead of main branch in my local git repository as shown below. For this we use a command called git checkout Testing. Here Testing is nothing but my branch name. Here you can see the Testing branch is switched successfully for current working branch.
Now check for current working branch as shown below. You can see * mark on left side of Testing branch. That means the Testing branch is the current working branch.
Now I connect to Testing branch. So, the changes or modification will happen that will affect our Testing branch only. Here we check this current connected branch in HEAD file of our local directory as shown below.
The current working branch is pointing to path of our local git repository as shown below. If we switch branches using command git checkout main then main branch will be the current working branch and the HEAD file will contains the path as ref: refs/heads/main.
We can see the latest commit ID of Testing branch as shown below.
Steps To Add, Commit And Push Changes Under Testing Branch
Here I will modify one file called DateDetails.cs under Testing branch as shown below.
Now we check the status as shown below. This file is modified and not available for staging.
Here I added to staging and commit changes to local git repo and Testing branch as shown below.
Pic 1
Pic 2
Now, I want to push the changes from local git repo to GitHub repo using command git push origin Testing under Testing branch. After successfully pushed these changes to GitHub repo my new branch called Testing will be shown with modified file in GitHub repo as shown below.
Pic 1
Pic 2
Now check the new content of the file DateDetails.cs made under Testing branch as shown below as compare with same file DateDetails.cs under main branch.
DateDetails.cs content under Testing branch,
DateDetails.cs content under main branch,
Steps To Create New Branch In Local Git Repo Under Testing Branch
To make new branch under Testing branch, we need to point our local git repo to current working branch called Testing branch. That means the new branch called Preprod will contain same changes as there under Testing branch instead of main branch.
Let's Understand One Scenario As Below,
But here I create one scenario that I create a Development branch and through Development branch we connect to Preprod branch. For this we create Development branch using command git checkout -b Development . Using this single command we can create new branch as well as current working branch as shown below.
Here I will rename the branch name from Preprod to Prod under Development branch as shown below using command git branch -m 'Old branch name' 'New Branch name'.
Now if any changes will be made and push it then the branch called Prod will be pushed into my GitHub repository.
Summary
In this write-up, we have learned the below details,
- Create branch in local git repo
- Push the branch from local git repo to GitHub
- Important Git commands to create and push branch
- Understand one scenario about git branches relationship
Thank You & Stay Tuned For More