What are merge conflicts in Git and how to resolve them?

What are merge conflicts in Git and how to resolve them?

Trust me, it's easy ๐Ÿ˜ƒ

Usman Sabuwala
ยทNov 11, 2022ยท

4 min read

Subscribe to our newsletter and never miss any upcoming articles

Table of contents

  • What is a merge conflict โ‰
  • How can a conflict arise? ๐Ÿš—๐Ÿ’ฅ๐Ÿš™
  • How to resolve it? ๐Ÿ› ๏ธ
  • Conclusion

So hi again coders ๐Ÿ‘‹

I am Usman! Back again with another article. And this time about resolving merge conflicts in Git!

And honestly, no matter how professional you are, you can't escape from merge conflicts, because they are inevitable. It's neither too easy nor too hard of a thing to do, if you know how to do it ๐Ÿ˜‰!

SO LET'S BEGIN!

What is a merge conflict โ‰

In simple words, a merge conflict in Git is a term used to describe when two codebases are merged together, and they have a conflict where Git can make no decision and the decision has to be made by the committer (YOU)!

How can a conflict arise? ๐Ÿš—๐Ÿ’ฅ๐Ÿš™

The main branch

So, let's assume you are working in a team, and the team members are working on a different branch (or on the same branch). Now, this is what happens:

Let's say you work on the main/master branch and you added this code

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>QOTD</title>
  </head>
  <body>
    <h1>Quote of the day</h1>
    <blockquote>
      <!-- Work in progress -->
    </blockquote>
  </body>
</html>

Then you commit the changes on main:

main commit.gif

The other branch

Now let's say, John, a team member, creates a new branch from the main branch

john branch.gif

Then, John modifies the index.html file, adds a quote, and commits the code.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>QOTD</title>
  </head>
  <body>
    <h1>Quote of the day</h1>
    <blockquote>
      Inspiration does exist, but it must find you working
    </blockquote>
  </body>
</html>

john commit.gif

Beginning of the conflict

So, after John makes the changes, meanwhile if you modify the index.html file and that line, in particular, it will be fine until John merges the branch.

For example, if you add a quote inside the blockquote tag, commit the code, and when John tries to merge his branch with yours, a conflict will arise.

Say you modified the file this way, and committed the changes

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>QOTD</title>
  </head>
  <body>
    <h1>Quote of the day</h1>
    <blockquote>
      You can get everything in life you want if you will just help enough other
      people get what they want
    </blockquote>
  </body>
</html>

main commit 2.gif

Finally, when John merges his branch into main, the conflict occurs

conflict.gif

How to resolve it? ๐Ÿ› ๏ธ

The diagram below ๐Ÿ‘‡ explains what exactly happened

image.png

Because of the commit in the middle, the conflict took place. And now to resolve it, here are the steps

The changes

If you look at the file after the merge, you will notice that the file is quite different and weird

image.png

Now, this is done by Git in order for us to understand the conflict and make the changes.

Because the changes in both branches were totally different from the first commit, Git could not decide which one to keep after the merge.

Hence, Git went ahead and added this extra stuff to our file

    <blockquote>
<<<<<<< HEAD
      You can get everything in life you want if you will just help enough other
      people get what they want
=======
      Inspiration does exist, but it must find you working
>>>>>>> john
    </blockquote>

The line with HEAD means the main branch has this content.

The line after the ======= means the john branch has this content.

Now, what we have to do is simply, choose what we want to keep, and edit the code as follows.

Resolution

Resolving the conflict is very simple. You can just keep what you want to keep and remove everything else.

Keep main code resolution1.gif

Keep john code resolution2.gif

Keep both changes resolution3.gif

Or better, you can click one of the above buttons provided by VS Code in order to do the job for you

image.png

Finally, commit the changes after resolving the conflicts

resolve.gif

And that's what there is about Merge Conflicts in Git. It's a fairly simple thing but sometimes it can make you bang your head on the table. So don't freak out when you see this.

Conclusion

I hope this article helped you out. If it did, let me know in the comments. If it didn't help or if you are stuck somewhere, still let me know in the comments below. I am open to feedback.

Finally, make sure to leave a like and share it with others if was helpful and that can help other people as well ๐Ÿ˜Š.

ย 
Share this