git.pngGit

Git Merge

In this article, we will learn How to merge two branches (or a specific commit) using git merge command.

Total Views:   5384


In this article, we will learn How to Merge two branches (or a specific commit) using git merge command. While working with real projects, a developer needs to manage multiple branches like production, Test/QA, Development, etc. Suppose a branch is created for adding a new feature or functionality. Once, the functionality is added it needs to be merged back to the master branch (or main branch). In git, merging is done with the use of the “git merge” command.  Depending upon the situation, git merge the changes in several ways.

1. Fast Forward Merge

2. Three-way Merge

How to use branches in Git?

Fast-Forward Merge:

Fast-Forward is the default merge behavior in Git. A Fast-Forward merge happens when you are trying to merge a branch that already contains all the content (changes) of the destination branch, which means there are no changes made in the destination branch since the creation of the branch contains the changes for the merging.

 

Git moves the branch pointer to the latest commit you are merging in. In the below image, you can see that the Master branch pointer moved to the latest commit merging in.

 

Let’s try to do it with git commands. For demonstration, I already have a master branch with some commits in the repository (use “git log” command in order to view the commit history).

 

Let’s create a new branch “NewFeature” and check out it as shown in the below images.

 

Add and edit few contents in the “NewFeature” branch. You can see that the index.html file is modified and contact.html is added.

 

Commit all the changes in the “NewFeature” branch.

 

You can check the logs as well in order to identify the commit’s Hash. Now “NewFeature” branch has more changes as compared to the master.

 

Check out the “master” branch and merge the “NewBranch” changes in the “master” branch with “git merge <branch_name>”. You can see that git merge behavior is Fast-Forward.

 

Now, check the git log. It’s clearly visible that the master branch pointer is shifted to the last commit.

 

Three-way merge:

In a three-way merge, both branches have the changes since they diverge at a particular commit. Git looks at three points in the commit history. One is the latest commit in the source branch, the second is the latest commit in the destination branch, and the third is the commit they have common in those branches.

 

On merging, Git merges the changes and creates a new commit which is the result of the merge command, also known as the Merge Commit

 

In order to demonstrate, I have created a master branch that already has some commit log history.

 

Create a new branch, let say “NewFeature” and checkout ithat branch.

 

Do changes in the “NewFeature” branch and commit it as well. As in the below image, You can see, I have done changes in About page.

 

Now check out the master branch and do some changes in the content. For demonstration, I have done changes in the Index HTML page.

 

Now, use the “git merge <branch_name>” command in git to merge the changes. As you can see this time changes are merged by the “recursive” strategy.

 

You can also check the log for checking the merge commit.

 

Resolving Conflicts while merging:

If two branches that are going to merge have content changes in the same file, then git is unable to identify which version needs to be pick. Such a situation is called a Merge conflict. In that situation, merge conflicts need to be resolved manually. For demonstration, I have a master branch with some commits.

 

Let’s create and checkout another branch(name it as “NewFeature”).

 

I have committed some changes in index.html file.

 

Let’s checkout the master branch, and do some changes in the index.html file.

 

Now merge the “NewFeature” branch into the master branch with “git merge <branch_name>”. As both branches have changes in the index.html file. It will give the conflict message in the git bash as well.

 

On checking the status, it is clearly visible that Merge is still not completed (“Merging”).

 

Open Index.html file in order to merge the conflicts. For ease understating, git adds the visual marker like “>>>>>>>>”,”========” and “<<<<<<<<”.

 

Mostly content above” ======” is the content of the receiving branch and the part after it is merging a branch. Resolve the conflict according to the changes required. (If need to abort the merge, use “git merge –abort

 

After resolving the conflict, add the commit changes to the repository. You can check the logs to verify the changes merged successfully.