Let's Learn About Merging The Git Branches 📥📤 And Create Pull Request In Details

Introduction

In my previous article, we discussed about steps to create a branch in a local git repo and how to push this new branch with commit changes from the local git repo to GitHub.

Also, we discussed the relationship between branches.

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

  1. Know about merging code from one branch to another branch
  2. Steps for merging git branches
  3. Steps to create a pull request
  4. Perform merging using GitHub and Git Bash
  5. Check the log history of branches using GitHub and Git Bash
  6. Difference between merging process Using GitHub and Git Bash

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
  7. Let's Understand About Git Branches And Its Real-Time Uses

What Is Merging Between Git Branches

The merging process is moving the changes to file contents from one branch to another branch. This process can be done using 2 ways as mentioned below.

  1. GitHub
  2. Git Bash

Steps For Merging Git Branches

Here I will make changes in MyFile.cs under the main branch of the SatyaTwo repository as shown below. In my previous article, I mentioned details about SatyaTwo repo and under this repo, the available branches are there. Under these branches how to modify these files I already described.

Merging Git Branches

Then I will make changes in MyFile2.cs under the main branch as shown below.

MyFile2

Now we check the total no. of commits under the main branch along with the commit description given during the Commit changes of the above files under the main branch as shown below.

Here we got a total of 10 commits.

 10 commits

Let's compare the total no. of commits of the same files under the Testing branch of the SatyaTwo repo as shown below. Here we got a total of 5 commits.

 5 commits

Now I want these commit changes that are the 6th, 7th, 8th, 9th, and 10th of MyFile.cs and MyFile2.cs files from the main branch to the same MyFile.cs and MyFile2.cs files of the Testing branch. For this, I want to create a new pull request as shown below.

Testing branch

Now we can see an interface as shown below. That tells us from which branch to which branch you want to merge the changes. So, as per the requirements I want to move the changes from the main branch to the Testing branch as shown below.

Requirements

So, there is no issue with merging. If there is any issue then we get a merge conflict issue which means the same file has been modified in both the branches. So as of now for me, it works fine for merging between branches. Here I have written one message Merging from the main to the Testing branch as shown below. Then click on Create pull request.

Merge conflicts

Here you get an interface as shown below after creating a pull request.

Creating pull request

Then using the same interface as mentioned above I click on the Merge pull request button as shown below and then it asks for Confirm merge.

Pic 1

 Confirm merge

Pic 2

 Confirmation

Now we can see an interface for successful merging between branches as shown below.

Successful merging

Here I got the pull request URL as shown below.

Pull request URL

Now compare the no. of commits between branches are main and Testing as shown below.

No. of commits of the main branch

Main branch

No. of commits of the Testing branch

Here you get the number of pull request messages shown in the Testing branch from the main branch. I have created one pull request. So, it shows a message like Merge pull request #1 from satyaCsgithub/main.

Testing branch

Here it shows the total no. of commits is 11 because the merging between git branches is also a commit. So, it shows a total of 11 commits. Check the image as shown below.

Git branches

Now we check the updated content of MyFile.cs and MyFile2.cs under the Testing branch as shown below. These file contents should be the same as under the main branch.

Check Commit messages

Check Commit messages

Check new properties added in MyFile.cs

New properties

Check new properties added in MyFile2.cs

Check new properties

Check the contents under branches locally using the git command

First let me clone this SatyaTwo repository to my local as shown below.

SatyaTwo repository

Now create a directory to this SatyaTwo repository and check the files in this local repo under the main branch as shown below.

Local repo

Check new properties added in MyFile.cs under the main branch,

Properties added

Check new properties added in MyFile2.cs under the main branch,

Under main branch

Check the files in this local repo under the Testing branch as shown below.

Check the files

Check new properties added in MyFile.cs under the Testing branch,

Under Testing branch

Check new properties added in MyFile2.cs under the Testing branch,

MyFile check

Now I am connected to the main branch and modify the MyFile2.cs file as shown below.

Modify the MyFile

Now add these changes to the staging area. Then commit these changes to local git and finally push these changes to the GitHub repository as shown below.

GitHub repository

Then commit and push these changes as shown below.

Push these changes

After refreshing the GitHub repository under the main branch, We can see 11 commits with commit messages as shown below.

Refresh the GitHub

Now check the content of MyFile2.cs in the GitHub repository as shown below.

Check the content

Now I switch to the Testing branch in locally using Git Bash as shown below. Then I will merge the main branch changes to the Testing branch.

 Git Bash

Then we get one interface for giving merge description as here default merge message is given as Merge branch 'main' into Testing and save changes. So, for save changes I put one key combination like:wq. Here:wq means that we want to save and quit the file.

Merge description

After successfully merging the changes from the main branch to the Testing branch then we get the details as shown below.

After successful merged

Now check the list of files and new modifications in MyFile2.cs under the Testing branch as shown below.

New modification

Now I have to push the changes to the GitHub repository to reflect the same in MyFile2.cs under the Testing branch as shown below.

Push the changes

Now go to the GitHub repository and check the MyFile2.cs contents under the Testing branch as shown below.

Now go to GitHub

Let's check the total commits between branches after new changes as shown below.

For the main branch

Total commits

After merge from the main to the Testing branch using Git Bash,

Merge from main

It shows a total of 13 commits because the merge process using Git Bash is itself another commit.

13 commits

Check the Log History Of Git Branches

For the main branch using Git Bash,

Check Log History

For the main branch using the GitHub repo,

GitHub repo

For Testing branch using Git Bash

Git Bash

For the Testing branch using the GitHub repo,

 Testing branch

In this way, we can check the log history for each and every git branch with commit IDs and commit messages as shown above.

Difference Between Merging Process Using GitHub And Git Bash

  1. Using GitHub we need to create pull requests for merging code between the main and Testing branch.
  2. Using Git Bash we don't need to create a pull request for merging code between the main and Testing branches. For this, we use a command called git merge 'branch name' e.g. git merge main.

Summary

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

  • Know about merging code from one branch to another branch
  • Steps for merging git branches
  • Steps to create a pull request
  • Perform merging using GitHub and Git Bash
  • Check the log history of branches using GitHub and Git Bash
  • Difference between merging process Using GitHub and Git Bash

Thank You & Stay Tuned For More


Similar Articles