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.
- 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
Note. Before going through this session, please visit my below-mentioned session related to Git and GitHub.
- Introduction To Git
- Steps For Configuring Git With Details
- Let's Know About Git Add, Git Commit, Git Status, And Git Push-In Details
- Let's Understand How Git Maintains History Of Version Or Commit IDs with Details
- Steps To Initialize A Git Repository And Push The Changes To GitHub In Details
- Let's Know About Git Branches And How To Implement It
- 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.
- GitHub
- 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.
Then I will make changes in MyFile2.cs under the main branch as shown below.
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.
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.
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.
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.
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.
Here you get an interface as shown below after creating a 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
Pic 2
Now we can see an interface for successful merging between branches as shown below.
Here I got the pull request URL as shown below.
Now compare the no. of commits between branches are main and Testing as shown below.
No. of commits of the 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.
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.
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 new properties added in MyFile.cs
Check new properties added in MyFile2.cs
Check the contents under branches locally using the git command
First let me clone this SatyaTwo repository to my local as shown below.
Now create a directory to this SatyaTwo repository and check the files in this local repo under the main branch as shown below.
Check new properties added in MyFile.cs under the main branch,
Check new properties added in MyFile2.cs under the main branch,
Check the files in this local repo under the Testing branch as shown below.
Check new properties added in MyFile.cs under the Testing branch,
Check new properties added in MyFile2.cs under the Testing branch,
Now I am connected to the main branch and modify the MyFile2.cs file as shown below.
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.
Then commit and push these changes as shown below.
After refreshing the GitHub repository under the main branch, We can see 11 commits with commit messages as shown below.
Now check the content of MyFile2.cs in the GitHub repository as shown below.
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.
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.
After successfully merging the changes from the main branch to the Testing branch then we get the details as shown below.
Now check the list of files and new modifications in MyFile2.cs under the Testing branch as shown below.
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.
Now go to the GitHub repository and check the MyFile2.cs contents under the Testing branch as shown below.
Let's check the total commits between branches after new changes as shown below.
For the main branch
After merge from the main to the Testing branch using Git Bash,
It shows a total of 13 commits because the merge process using Git Bash is itself another commit.
Check the Log History Of Git Branches
For the main branch using Git Bash,
For the main branch using the GitHub repo,
For Testing branch using Git Bash
For the Testing branch using the GitHub repo,
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
- Using GitHub we need to create pull requests for merging code between the main and Testing branch.
- 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