How To Fix Git Merge Conflicts With Github Interface.

isiagi geofreyisiagi geofrey
6 min read

Summary

This article covers handling Git merge conflicts by creating conflicts between branches and resolving them. You'll learn to create a conflict by making diverging changes in a new branch (dev) and the master branch. The steps include: initializing a Git repository, creating branches, pushing changes, and simulating a conflict. The resolution process involves comparing branches, understanding conflict markers in the code, and choosing which changes to accept or reject. After resolving the conflict, you'll merge the dev branch into the master branch successfully.

This article presumes you are familiar with fundamental basic knowledge of Git and GitHub, this would include;

  • What is Git, GitHub, and it's importance?

  • How to Initialize a Git Repo.

  • Create and Push Branches.

  • And More...

To have a great overview of git, click here

What Are Git Merge Conflicts?

๐Ÿ’ก
Click on the first image above to visit the site in the images.

Create And Solve A Merge Conflict.

1. Create Conflict.

To create a conflict, we are going to follow the steps below.

  1. Create a folder, add an HTML file, and push it to Git Hub.

Above is a sample of my pushed HTML file to GitHub. It's on the branch master and in the conflict remote GitHub repository with a commit message of chore .

  1. Create a new branch from the master branch for this case, it can be called any name, here the dev branch will be used for the new branch.

git checkout -b dev

Creating a new branch off another branch create a new copy with the exact content of the branch being branched off from with a new branch name.

๐Ÿ’ก
The new dev branch is a copy of master
  1. Make changes to the HTML document on the new branch (dev) and push the branch plus the changes to Git Hub. Below is dev the git remote branch.

    <h2> added, <h1> got from the master branch after branch off.

You will be alerted of the push event and you can compare, create pull requests, and merge branches.

Click on Compare & Pull Request button to check if the two branches can be merged with no issues.

if we want now to merge the dev branch with it's changes and master branch, the two branches can merge successfully.

๐Ÿ’ก
NB : dev branch is a copy of the master branch and the master branch has not changed from the state used when branching to the dev branch thus no conflict.

We won't be merging the two branches in this article but to merge, click Create pull request and from there you will be able to merge.

๐Ÿ’ก
If a merge is made, the content (code) in dev branch will be moved to master branch and master will have the same content as dev

Cause Conflict

To cause a conflict, we need to update the master branch so that it's not in the same state as it was when branching off to create a new branch dev and try to merge them.

Let's use the recent files used.

Initial master file before checkout to dev branch (initial state). Html document with a <h1>

dev branch created by checkout off master with initial state and adding a <h2> from dev branch.

Check merge, Merge possible

Update master after creating dev branch

Changes master branch
<h2> has been added to the master branch and pushed to GitHub. Initially master branch had no <h2> when we branched off to dev branch.
dev Branch
dev branch has remained as it was with the initial state of the master branch having Html document with <h1> and adding a <h2> with Dev Branch Content .

Now master branch has been updated after creating the dev branch.

The initial state of master branch from which the dev branch was created has changed thus the two copies don't match anymore leading to a conflict.

Merging dev and master Branch.

On dev Branch

The dev branch is 1 commit ahead of, 1 commit behind master

Ahead - The dev branch has one commit that is not yet in the master branch. This indicates that there has been a new change or addition in the dev branch that hasn't been merged into master.

Behind - The dev branch is missing one commit that is present in the master branch. This indicates that there has been a change or addition in the master branch that hasn't been merged into dev.

From the screen above click on Contribute and then compare .

Now, a merge between master and dev git branches is not possible. There is an issue / conflict with the two branches.

Conflict Details
The new state/changes of the master branch caused the conflict. The copy from which dev the branch was created has changed and we want to merge the two different copies thus causing a conflict. Git wants to know what to include or ignore from the two different copies of the new master one with changes and the dev branch when merging the two copies to the initial master branch state.

2. Resolve Conflict.

Click on Create pull request to solve the conflict.

After creating pull request , scroll down to where the conflict notification is shown, and click on Resolve Conflicts .

View of conflict below.

Explanation of conflict
The conflict begins in line 10 to line 14. line 10 to line 12 represent the changes made when creating the dev branch, <h2> with Dev Branch Content . Line 12 to line 14 represent the new changes made to the master branch after the creation of dev the branch.
// = comments

// changes from the dev branch
<<<<<<< dev // branch name with conflict
    <h2>Dev Branch Content</h2>
======= // conflict seperator
// new changes from the master branch
    <h2>Master branch changed after creating dev branch</h2>
>>>>>>> master //branch name with conflict

What Next?

To resolve the conflict, we need to accept or reject the code differences presented. We can Accept or Reject changes from any one branch with the conflict or Accept or Reject all changes on conflicting branches.

Removing the <<< , === , >>> indicates a step toward resolving the conflict.

Sample below shows when only dev changes are accepted, the same can be done to accept the new master changes.

๐Ÿ’ก
Below is a code example of accepting only dev changes, we accepted both changes from the branches as shown below the code example.
 // Accepting only dev changes, ignoring new master changes.
// '<<<', '===', '>>>' with <h2> from updated master are removed
// to accept only dev changes.

<body>
    <h1>Initial Content</h1>

    <h2>Dev Branch Content</h2>

  </body>

Both Changes Accepted

After choosing what to accept or reject by removing the <<< , === , >>> , click on Mark as resolved on the top right and then Commit merge

After resolving the conflict, Merge possible

๐Ÿ’ก
The changes accepted when resolving the conflict, will be shipped to the master from the dev branch.

Thanks, Happy Coding

0
Subscribe to my newsletter

Read articles from isiagi geofrey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

isiagi geofrey
isiagi geofrey