Resolving Merge Conflicts

1 min read

What are merge conflicts?

While git is intelligent enough to merge changes from one branch to another, sometimes it may get into a confusing situation where it doesn't know what changes to merge and what to discard.

Such situations arise when both the branches have changes made to the exact lines of code and git cannot figure out alone what changes to pick for the merge. This is known as a merge conflict.

Resolving Conflicts

Imagine, you have two branches: main and dev. You made some changes on line number 3 in the README.md file on dev.

My first commit
My second commit
My third commit

Someone else also made some changes in the exact line and in the exact file on main.

My first commit
My second commit
My fourth commit

When you try to merge main into dev or vice-verca, you'll come across this error message:

> git merge main
Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

If you open the file, you'll see the conflicts marked like this:

My first commit
My second commit
<<<<<<< HEAD
My third commit
=======
My fourth commit
>>>>>>> main

To resolve the conflict, you must edit the file to remove the conflict markers and choose the final changes. You can also keep both of the changes if you wish to:

My first commit
My second commit
My third commit
My fourth commit

Then add these changes to the staging area and continue the merge.

> git add README.md
> git merge --continue

To avoid large merge conflicts, it's advised to regularly keep updating your development branch with the main branch.

Table of Contents

Related Articles

Take Your Job Search To The Next Level

Searching for a job is a stressful and frustrating process, especially when you've put in a lot of effort and time into crafting the perfect application and you don't even hear back from the employer. The waiting game can be frustrating, and it's…

Read more

How To Teach Yourself Data Structures & Algorithms

Self studying can be difficult and intimidating. If you want to learn data structures and algorithms but are stuck on how to begin, then keep reading! Though there are many paid courses available to you that can fast track your learning, in this post…

Read more

All You Need To Know About Building An Ergonomic Setup

What makes a setup ergonomic? For decades, scientists and medical professionals have been attempting to understand the human body and how it interacts with its environment. An ergonomic setup's purpose is to help you maintain excellent posture and a…

Read more