16. Git Merge (Part - 1)

16. Git Merge (Part - 1)

Introduction

One of the fundamental operations in Git is merging, which combines changes from different branches into a single branch. This process is commonly used when integrating new features, bug fixes, or updates from one branch into another, such as merging a feature branch into the main branch (often called the "master" or "main" branch).

Merging is the process of taking the changes from one branch and applying them to another branch. It allows you to bring together the work done in different branches and create a unified version of the project.

Assume that we created a branch to implement a new feature. And we complete the implementation of that new feature. Once that implementation is completed for that branch, we have to combine those changes from the Child Branch with the Parent Branch.

This process of combining the changes from the Child Branch with the Parent Branch is known as Merging.

And then we can push it to the Remote Repository.

Example

Let's consider we are working on the Master Branch for a project and suddenly we need to create a new feature in that, so we have to create a new feature and we don’t wanna create any mess in the project's main code and want that the work on the project should be continuing as well. Therefore, we’ll create a branch (Child branch) for working on the new feature.

After completing the work, that branch (Child Branch) can be either merged with Parent/Main/Master Branch and then push to the Remote Repository or directly pushed to the Remote Repository.

Note: In companies, it’s better to merge/combine the new feature with the Parent/Main/Master Branch and then push it to the Remote Repository.

Pictorial Representation

How To Perform Merging

To do merging of a Child Branch to the Parent / Main / Master Branch we’ll use the command git merge staying at the Parent / Master Branch and also specifying the name of the Child Branch

Syntax

git merge <Child Branch Name>

Example

Let’s create two commits on the Master Branch. By default, we’ll be on the Master Branch.

Pictorial Representation

Suddenly, a new requirement came to implement a new feature. To implement a new feature without affecting the Master Branch, we’ll create a Child Branch. And suppose in the Child Branch / Feature Branch we did two commit operations.

In between the work on the Child Branch i.e. on the new feature, the work on the other things i.e. the Master Branch is also going on.

Now, after the completion of the work on the feature branch. We’ll merge that branch to the Master Branch.

Practical

Create a new directory named “merging”. And move inside into that directory using the command cd

Now, initialize that directory using the command git init

Create two empty files in that directory named “a.txt” & “b.txt”. To create the empty files, we’ll use the command touch

touch a.txt b.txt

Now, check the status of the files. To check the status of the files, we’ll use the command git status . The files will be Untracked.

Add a file “a.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add a.txt; git commit -m "Commit 1"

And after that add the file “b.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add b.txt; git commit -m "Commit 2"

Now, there are two commits available at the Master Branch. To confirm that we’ll check the log using the command git log --oneline

Now, we have to work on a feature, so we’ll create a child branch named “feature”. So, we’ll create a child branch and switch to that branch using the command git checkout -b <branch name>

In our case, it’ll be git checkout -b feature

We’ll now confirm the total no. of branches and whether we switched to the new Child Branch “feature” or not. To confirm both, we’ll use the command git branch

We’ll see there are total two branches

  • master

  • feature <Child Branch>

And we’ll also see the position of the star “*” is on the “feature” i.e Child Branch; This indicates that we have been switched to the Child Branch.

Create two empty files in that directory named “x.txt” & “y.txt”. To create the empty files, we’ll use the command touch
touch x.txt y.txt

Now, check the status of the files. To check the status of the files, we’ll use the command git status

We are on the feature branch and the files are Untracked.

Now add the file “x.txt” to the Staging Area and then Commit that file to the Local Repository.

git add x.txt; git commit -m "Child Commit 1"

And after that add the file “y.txt” to the Staging Area and then Commit that file to the Local Repository.

git add y.txt; git commit -m "Child Commit 2"

Now, we’ll check the total no. of files on the Feature Branch using the command ls

There will be a total of four files in the working directory. Two files “a.txt” & “b.txt” from the Master Branch that got inherited from the Parent Branch while creating the Child Branch. And the other two files “c.txt” & “y.txt” have been created in the Child branch.

Now, if we’ll check the logs using the command git log --oneline

Then there will be a total of four commits. Two commits from the Master Branch & the other two commits from this branch i.e. the child branch.

Now we’ll merge the feature Branch to the Master Branch. So, as Master Branch requires these changes, therefore we have to execute the command i.e. we’ll merge the Child branch to the Master Branch by staying/switching to the Master Branch.

To merge the Child Branch with the Master Branch we’ll use the command git merge <Branch Name>

In our case, it’ll be git merge feature

But, before that, we have to switch back to the Master Branch, as we are on the Child Branch “feature”. So, to switch the branch, we’ll use the command git checkout <Branch Name>

In our case, it’ll be git checkout master

Now we’ll merge the feature Branch to the Master Branch. Therefore, we’ll use the command git merge feature

The Child branch “feature” will be merged with the Parent Branch “master”. Two files were added to the Master Branch.

If we want to confirm, then we’ll check the branch we are residing currently using the command git branch

We’ll be on the Master Branch.

Note: After the Merging of the Branch, the Child Branch will still be present. If we want we can delete it.

We’ll also confirm by checking all the files available in the Working directory of the Master Branch using the command ls

To confirm we’ll check the log on the Master Branch using the command git log --oneline

Previously, there were only two commits in the logs on the Master Branch. But now there will be four commits. And the HEAD will be pointing to the last commit on the Child Branch.

Types Of Merge

There are Two-types of Merge:

  • Fast-forward Merge

  • Three-way Merge

Fast-forward Merge

Imagine we are having two branches (In the project)

  • Parent Branch (master)

  • Child Branch (feature)

At the second commit the Feature Branch (Child Branch) got created.

And after creating the Feature Branch (Child Branch), we made changes & commit to the Feature Branch only. We didn’t make any changes in the Master/Parent Branch.

So, after creating the Child Branch, if we don’t do any changes or commit in the Parent / Master Branch in that case GIT will perform Fast-forward Merge.

If there will be only two branches Parent & Child, and the updation are only available in the Child Branch and not in the Parent/Master Branch in this case the GIT will perform Fast-forward Merge.

In the Fast-forward Merge, GIT simply moves the pointer from the Parent Branch to the last commit of the Child Branch.

To confirm we’ll check the log on the Master Branch using the command git log --oneline

Here both, Master & Child (feature) are pointing to the same commit i.e. the last commit of the Child Branch.

In the Fast-forward Merge, there is no chance of Conflicts because there is an update in the Child Branch only, not on the Master Branch.

Example (Pictorial Representation)

Create a file named “a.txt” and add some data to that file.

After that commit that file in the Local Repository.

Now, again add some data in that file “a.txt” and commit that changes in the Local Repository.

Now, create a new branch (Child). Then automatically the properties i.e. files in the Working directory & Commits will be inherited to the Child branch.

Now, add some data in that file “a.txt” present in the Child branch. And then commit those changes to the Local Repository.

Now, again add some data in that file “a.txt” and commit that changes in the Local Repository of the Child Branch.

So, now we’ll merge the Child branch to the parent branch.

Now, as we see there are only 2 data in the parent branch whereas there are 4 data present in the child branch.

So, if we merge the Child branch to the Master Branch, then the Master will be pointing to the Last commit of the Child branch. And also, the final output after the change in the file a.txt will be having data 4.

So, even though the same file will be modified, in the Fast-Forward merge there will be no chance of conflicts.

Practical

Create a new directory named “fastforward”. And move inside that directory using the command cd

Now, initialize that directory using the command git init

Create a file “a.txt” and write “data 1” in that file using the command echo. Therefore, the command will be echo “Content” > <filename>

In our case, it’ll be echo “data 1” > a.txt

Add that file “a.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add a.txt; git commit -m "Commit 1"

Now, again add some content by writing “data 2” in that file using the command echo. Therefore, the command will be
echo "Content" >> <filename>

In our case, it’ll be echo "data 2" >> a.txt

Add that file “a.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add a.txt; git commit -m "Commit 2"

Now, there are two commits available at the Master Branch. To confirm that we’ll check the log using the command git log --oneline

Now, we have to work on a feature, so we’ll create a child branch named “feature”.

So, we’ll create a child branch and switch to that branch using the command git checkout -b <branch name>

In our case, it’ll be git checkout -b feature

We’ll now confirm the total no. of branches and that had we switched to the new Child Branch “feature” or not. To confirm both, we’ll use the command git branch

We’ll see there are total of two branches

  • Master

  • feature <Child Branch>

And we’ll also see the position of the star “*” on the “feature” i.e. Child Branch; This indicates that we have been switched to the Child Branch.

Now, again add some content by writing “data 3” in the file “a.txt” using the command echo. Therefore, the command will be
echo "Content" >> <filename>

In our case, it’ll be echo “data 3” >> a.txt

Add that file “a.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add a.txt; git commit -m "Commit 3"

Now, again we’ll add some content by writing “data 4” in that file “a.txt” using the command echo. Therefore, the command will be
echo "Content" >> <filename>

In our case, it’ll be echo "data 4" >> a.txt

Add that file “a.txt” to the Staging Area and then Commit that file to the Local Repository.

We’ll use the command git add a.txt; git commit -m "Commit 4"

Now, there will be four commits available at the Child Branch. To confirm that we’ll check the log using the command git log --oneline

So, HEAD will be pointing to the last commit of the Child Branch.

Now, we’ll switch back to the Master Branch, as we are on the Child Branch “feature” and we have to now merge the Child Brange to the Master Branch. So, to switch the branch, we’ll use the command
git checkout <Branch Name>

In our case, it’ll be git checkout master

Now we’ll merge the feature Branch to the Master Branch. So, as Master Branch requires these changes, therefore we have to execute the command i.e. we’ll merge the Child branch to the Master Branch by staying/switching to the Master Branch.

To merge the Child Branch with the Master Branch we’ll use the command git merge <Branch Name>

In our case, it’ll be git merge feature

Now, there will be four commits available at the Master Branch. To confirm that we’ll check the log using the command git log --oneline

Now, both HEAD & Master will be pointing to the last commit of the Child Branch.

Now, if we’ll check the content of the file “a.txt” present in the working directory of the Master Branch, then we’ll see there will now 4 lines added.

To check the content of the file “a.txt” we’ll use the command cat

So, we didn’t get any Conflict.