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.
Steps For Merging Git Branches
Here I will make changes in MyFile.cs under main branch of SatyaTwo repository as shown below. In my previous article i have mentioned details about SatyaTwo repo and under this repo the available branches are there. Under these branches how to modify these files I already have described.
Then I will make changes in MyFile2.cs under main branch as shown below.
Now we check the total no. of commit under main branch along with the commit description are given during Commit changes of above files under main branch as shown below.
Here we got total 10 commits.
Let's compare these total no. of commits of same files under Testing branch of SatyaTwo repo as shown below. Here we got total 5 commits.
Now I want these commit changes that is 6th, 7th, 8th, 9th and 10th of MyFile.cs and MyFile2.cs files from main branch to the same MyFile.cs and MyFile2.cs files of 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 merge the changes. So, as per the requirements I want to move the changes from main branch to Testing branch as shown below.
So, there is no issue for merging. If there is any issue then we get a merge conflicts issue that means 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 is Merging from main to Testing branch as shown below. Then click on Create pull request.
Here you get an interface as shown below after creating pull request.
Then using same interface as mentioned above I click on Merge pull request button as shown below and then it asks for Confirm merge.
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 main branch
No. of commits of Testing branch
Here you get the number of pull request messages shown in Testing branch from main branch. I have created one pull request. So, that it shows message like Merge pull request #1 from satyaCsgithub/main.
Here it shows total no. of commits is 11 because the merging between git branches also a commit. So, that it shows total 11 commits. Check the image as shown below.
Now we check the updated content of MyFile.cs and MyFile2.cs under 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 in local using git command
First let me clone this SatyaTwo repository to my local as shown below.
Now create directory to this SatyaTwo repository and check the files in this local repo under main branch as shown below.
Check new properties added in MyFile.cs under main branch,
Check new properties added in MyFile2.cs under main branch,
Check the files in this local repo under Testing branch as shown below.
Check new properties added in MyFile.cs under Testing branch,
Check new properties added in MyFile2.cs under Testing branch,
Now I am connected to main branch and modify the MyFile2.cs file as shown below.
Now add these changes to staging area. Then commit these changes to local git and finally push these changes to GitHub repository as shown below.
Then commit and push these changes as shown below.
After refresh the GitHub repository under main branch, We can see 11 commits witg commit messages as shown below.
Now check the content of MyFile2.cs in GitHub repository as shown below.
Now I switch to Testing branch in local using Git Bash as shown below. Then I will merge the main branch changes to 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 successful merged the changes from main branch to Testing branch then we get the details as shown below.
Now check list of files and new modification in MyFile2.cs under Testing branch as shown below.
Now I have to push the changes to GitHub repository to reflect the same in MyFile2.cs under Testing branch as shown below.
Now go to GitHub repository and check the MyFile2.cs contents under Testing branch as shown below.
Let's check total commits between branches after new changes as shown below.
For main branch,
After merge from main to Testing branch using Git Bash,
It shows total 13 commits because the merge process using Git Bash is itself another commit.
Check Log History Of Git Branches
For main branch using Git Bash,
For main branch using GitHub repo,
For Testing branch using Git Bash,
For Testing branch using GitHub repo,
In this way we can check log history for each and every git branches with commmit IDs and commit messages as shown above.
Difference Between Merging Process Using GitHub And Git Bash
- Using GitHub we need to create pull request for merging code between main and Testing branch.
- Using Git Bash we don't need to create pull request for merging code between main an Testing branch. For this we use command called git merge 'branch name' e.g. git merge main.
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 pull request
- Perform merging using GitHub and Git Bash
- Check log history of branches using GitHub and Git Bash
- Difference between merging process Using GitHub and Git Bash
Thank You & Stay Tuned For More