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? 🚗💥🚙
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
master branch and you added this code
<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
The other branch
Now let's say, John, a team member, creates a new branch from the
Then, John modifies the
index.html file, adds a quote, and commits the code.
<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>
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
<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>
Finally, when John merges his branch into
main, the conflict occurs
How to resolve it? 🛠️
The diagram below 👇 explains what exactly happened
Because of the commit in the middle, the conflict took place. And now to resolve it, here are the steps
If you look at the file after the merge, you will notice that the file is quite different and weird
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.
Resolving the conflict is very simple. You can just keep what you want to keep and remove everything else.
Keep both changes
Or better, you can click one of the above buttons provided by VS Code in order to do the job for you
Finally, commit the changes after resolving the conflicts
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.
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 😊.