Merge Conflict Handling Examples in GitHub

Learn how to resolve merge conflicts in GitHub with practical examples to simplify your coding workflow.
By Taylor

Understanding Merge Conflicts in GitHub

Merge conflicts can be a common hurdle when collaborating on projects using Git and GitHub. They occur when multiple contributors make changes to the same line of code or file, leading to discrepancies that Git cannot automatically reconcile. In this guide, we’ll walk through three practical examples of handling merge conflicts in GitHub, breaking down each scenario to help you resolve them with confidence.

Example 1: Resolving a Simple Line Conflict

Imagine you and a colleague are working on a project. You both edit the same line in a file called index.html. You might have changed it to add a new feature, while your colleague has made a different change to improve the design.

Start by pulling the latest changes from the remote repository:

git pull origin main

When you try to merge, you get a message indicating a conflict. You can check which files are conflicting with:

git status

This will show index.html as having a conflict. Open index.html, and you’ll see sections marked like this:

<<<<<<< HEAD
    <h1>Welcome to My Website</h1>
=======
    <h1>Welcome to My Amazing Website!</h1>
>>>>>>> branch-name

To resolve the conflict, decide which change to keep. You could choose either version, or combine them. For instance, you might opt to merge the two changes:

<h1>Welcome to My Amazing Website</h1>

After making the edits, save the file, then mark it as resolved:

git add index.html

Finally, commit the resolved merge:

git commit -m "Resolved merge conflict in index.html"

This example shows how to handle a straightforward line conflict. Always ensure to communicate with your team to decide the best outcome.

Example 2: Working with Multiple File Conflicts

In a scenario where multiple files are changed simultaneously, such as style.css and script.js, the process can feel a bit overwhelming. Let’s say you’ve made changes to both files while your teammate has also modified them. When you attempt to merge:

git pull origin feature-branch

You receive a message indicating multiple conflicts. To see the conflicting files:

git status

You will see something like:

both modified:   style.css
both modified:   script.js

Now, open each file to resolve conflicts. In style.css, you might see:

<<<<<<< HEAD
body {
    background-color: white;
=======
body {
    background-color: lightblue;
>>>>>>> branch-name

Decide which background color to keep or combine them creatively:

body {
    background-color: white; /* with a light blue accent */
}

Repeat the same for script.js where you might see similar conflict markers. After resolving both files, add them to the staging area:

git add style.css script.js

And commit your changes:

git commit -m "Resolved multiple merge conflicts in style.css and script.js"

This example illustrates how to systematically approach multiple file conflicts, ensuring you manage them efficiently.

Example 3: Using Visual Merge Tools

Sometimes resolving conflicts directly in the code editor can be challenging, especially with complex changes. Using a visual merge tool can help clarify things. Let’s say you’ve been working on a project and have a conflict in app.py after pulling changes.

Start by pulling the changes:

git pull origin main

When you encounter a conflict, instead of resolving it manually, you can invoke a merge tool:

git mergetool

This command opens a visual tool (like KDiff3, Meld, or others depending on your configuration) where you can see both versions side by side. You can easily select which changes to keep or edit them directly in the tool. Once you resolve the conflicts visually, save and exit the tool.

Now, mark the file as resolved:

git add app.py

Finally, commit the resolution:

git commit -m "Resolved merge conflict in app.py using a visual merge tool"

This example highlights the efficiency of using visual merge tools, especially when dealing with complex code changes. It can make the process less daunting and more user-friendly.


These examples of handling merge conflicts in GitHub aim to provide clarity and practical solutions for common situations you may encounter in your coding journey. Whether it’s a simple conflict or a more complex situation, understanding how to resolve them is key to successful collaboration.