How To Deal With Merge Conflicts in Git & Github
Tags: git, github, tutorial, codenewbie
Published: 2020-06-24 12:04:53

---
title: How To Deal With Merge Conflicts in Git & Github
published: true
date: 
tags: git, github, tutorial, codenewbies 
canonical_url: http://billyokeyo.codes/articles/how-deal-merge-conflicts-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

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

</div>
```

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>
```

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>
```

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 

![](https://billyblog.blob.core.windows.net/media/uploads/2020/06/24/master-commit.jpg)

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

![](https://billyblog.blob.core.windows.net/media/uploads/2020/06/24/merge-conflict.jpg)

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

![](https://billyblog.blob.core.windows.net/media/uploads/2020/06/24/conflicts.jpg)

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.

![](https://billyblog.blob.core.windows.net/media/uploads/2020/06/24/master-updated.jpg)

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

![](https://billyblog.blob.core.windows.net/media/uploads/2020/06/24/final.jpg)

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