DEV Community

Billy Okeyo
Billy Okeyo

Posted on • Originally published at on

How To Deal With Merge Conflicts in Git & Github

You might have one time tried to merge your branches in github but you get greeted with this merge conflict error and have no idea in how you'll solve it?

In this article we'll get into how to solve those conflicts and how do they come about, so let's get started.

What are Merge Conflicts

These are conflicts which arise when you are trying to merge a branch to master branch and you didn't initially pull the changes of the master branch to your local machine, this is common when working with teams.

Example of a Merge Conflict

Let's try to have a conflict in action so that you see what I am talking about.

I am convinced you know how to use Git and Github and executing git commands, if not check this series I did on Introduction to Git and GitHub.

So let's create a repo and give it any name you want, I'll name mine conflicts-demo.

After creating a repo, create an index.html file and add this code

    <h1> Merge Conflicts </h1>
    <p> I love Coding </p>

Enter fullscreen mode Exit fullscreen mode

After that you can clone the repo to your local machine or alternatively just create the project in your local machine and push it to github.

Now in your local machine's text editor create a branch and name it feature-conflicts, to create a branch run

git checkout -b feature-conflict

After that switch to the branch by running

git checkout feature-conflict

Then let's now edit our index.html file and add one line on line 4 with this code

<p> I love Chicken Curry </p>
Enter fullscreen mode Exit fullscreen mode

Now you can commit your changes, then checkout to master branch again and add this code at the exact line 4 as we did in our feature-conflict branch.

<p> I love playing video games </p>
Enter fullscreen mode Exit fullscreen mode

What we are trying to do there is create different changes to our file in the same line. So let's try now to merge the two branches now.

Let's do git checkout feature-conflict to take us to our feature-conflict branch, you might have noticed this wasn't possible because we never committed our changes we made in the master branch, and the error will look like this

This is one of the conflicts you might face, forgetting to commit changes before switching branches. So let's commit the changes in our master branch by running

git commit -am "Added favorite meal"

After that we can now switch to the other branch again by running

git checkout feature-conflict

Now let's try merging our feature-readme to our master branch, and we do that by running

git merge master

This will now throw a conflict as shown

Now that's a merge conflict, but don't worry, I'll show you how to fix this. You can fix the errors in GitHub or locally in your machine, I'll show you how to fix it locally because it always seem the easiest way and most of the time you'll be working locally.

Now a good text editor will show you the errors, I'm using vscode and errors will be shown as below

As shown above vscode gives you options which you can choose from which are

  • Accept incoming changes

  • Accept Both changes

  • Compare changes

But again it still shows us which lines have the conflicts and we can just edit them directly by deleting the conflict markups and allowing another line to go below the other line, so that our code look like the one below, thensave the changes.

After that we now need to do another commit so as to save the changes, and the conflict will be gone.

That's all I had for you in this article and that's how it's super easy to solve a merge conflict. Hope you enjoyed the article...

See you soon

Top comments (0)