How To Merge Git branch To Master And Handle Merge Conflicts

How To Merge Git branch To Master And Handle Merge Conflicts

30 Nov 2017 10 min read

In the previous article, we’ve gone through basic of Git Branching commands. And figured out how to create new branches and work effectively with them. Of course, we’ve mentioned how to merge branch back to master branch but there are certainly some key points to add.

For the beginning let say a few words about what a Git branch merge is.

Essential, it is a way Git putting a forked branch history back together. It allows us to unite created before parallel lines of development into main development line. As a result, the main line will be refreshed to reflect merging.

First, we have to ensure that all remote changes are updated with branches that going be merged. For this we run git fetch to pull latest remote commits, after that run git pull at master branch, to verify it has the latest updates.

Also, before merging we should use git command – git checkout, to choose the main branch to which we want to merge other branches. And just keep in mind that merging always performs on that branch TO which will merge, other words – on receiving branch.

There are two ways Git performs merging and it defines the merge algorithm automatically. But we suppose that you should better know about them. Because you know, forewarned is forearmed.

Fast Forward Merge

Say we are working on a website and already made some commits. At some point, we have an urgent task to add a new feature. So we create a new branch, make appropriate commits and when everything is done we move to merge, and eventually remove a new feature branch. This example is shown in the code below.

# Start a new feature
git checkout -b <new-feature file> master
# Edit some files
git add <new-feature file>
git commit -m "new-feature beginning"
# Edit some files
git add <file>
git commit -m "new-feature finish"
# Merge in the new-feature branch
git checkout master
git merge <new-feature>
git branch -d <new-feature>

In such case, Git automatically used Fast Forward Merge. To visualize it, look at pictures below.

fast-forward-merge

A fast-forward merge occurs here because while we were adding a new feature, the master branch remained the same and haven’t had any new commits. It means that main branch tip wasn’t moved. So there is a linear path from <new-feature> branch tip to the <master> branch tip. And thanks to that Git uses “fast forward” method to move one branch tip up to another and nicely integrate its histories.

3-way merge

But let imagine a bit different situation. We actually created a new branch, <new-feature>, but in meanwhile we got another call to fix a bug. So we left for a while our work on the new feature. Check out to master branch and create another <bug fixing> branch. Work on it, make some commits and merge it to the master branch.

# Start <new-feature>
git checkout -b <new-feature> master
# Edit some files
git add <new-feature file>
git commit -m <the new-feature begining>
# Return to master branch
git checkout master
# Create Bug fixing branch
git create - b <bug fixing> master
# Edit some files
git add <bug fixing file>
git commit -m <successful bug fixing>
# Merge in <bug fixing> branch
git checkout master
git merge <bug fixing>
git -d <bug fixing>
#Finish work on the new-feature
git checkout <new-feature>
git commit -m <the new-feature finish>
# Merge in branch
git checkout master
git merge <new-feature>

And only after all that we return to our <new-feature> branch. And while <bug fixing> branch was merged by the fast forward algorithm, the <new-feature> branch can’t be merged the same way. Because <master> branch tip was already moved forward by the previous merging. For such cases, Git uses a 3-way-merge.

For clarity see images below. They accurately demonstrate the process described above.

3-way-merge-step1
3-way-merge-step2
3-way-merge-step3
3-way-merge-step4

The last scheme illustrates how Git unites all the changes of each queued merge commit sequence. For this, it takes two branch tips, finds a common base commit, and then creates a new “merge commit”.

Conflict

In three-way merging, there may occur a conflict situation.

Note that it can only occur in 3-way merging.

It will happen when we’ll ask Git to merge branches on which we’ve made changes at the same part of the same file differently. So Git simply will be confused which version it should use.

Let’s take it back to our example. If we have changed on both <new-feature> and <bug fixing> branch the same part of the same  file, we’ll get a conflict message from Git that will look smth like this:

git merge <new-feature>
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed;
fix conflicts and then commit the result.

In such case, Git will pause merging and allow us to resolve occurred conflict manually.

To see what exactly we need to resolve run git status command.

And assuming the conflict occurred in the section of index.html we’ll see smth like this:

On branch master 
Unmerged paths: 
(use "git add/rm ..." as appropriate to mark resolution) 
both modified: index.html

Git will show us conflict-affected files by marking the conflicted content that needs to be resolved.

Those indicators are <<<<<<<, =======, and >>>>>>>.

<<<<<<< master:index.html

<div id="contact form">contact : [email protected]</div>

=======

<div id="footer">

 please contact us at [email protected]

</div>

>>>>>>> new-feature:index.html

Where, under the  <<<<<<< master line shown

conflicted text from master

=======

conflicted text from new-feature branch

>>>>>>> new-feature branch

content before the ======= marker is the receiving branch and the part after is the merging branch.

After a conflict resolved, we should tell Git that everything is fine now. For this, we run git add command. And eventually, finish the merge. For this, we simply need to commit a usual snapshot.

So, just to sum things up. Git merging:

  • can be performed in two ways: Fast Forward and 3-way merging:
  • unites line of commits into one unified history of commits;
  • and may face a conflict in case of different changes made at the same place in the one file in both commit lines.
ERP development final cta

Get a Custom Solution with Web Design Sun

At Web Design Sun, we specialize in building web applications for clients in every business and industry.  If you’re interested in custom applications for your business, contact us today.

Contact us today to get started

More From Blog

GitLab Installation Tutorial (Part2)

After GitLab is successfully configured, there is Plesk integration next. Create /etc/nginx/conf.d/gitlab.conf file with the following content
10 Apr 2016 12 min read

Elementor Custom Widget Tutorial

Sometimes there is a need for specific functionality on our site. For such case, you can create Elementor Custom Widget with our easy-to-follow tutorial.
20 Jun 2021 14 min read

Major SEO Trends in 2017

In order to get your site to the top of the search and generate more relevant traffic, it is essential to be updated about current SEO trends.
27 Jan 2017 9 min read