How To Fix Git Merge Conflicts With Github Interface.
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?
Create And Solve A Merge Conflict.
1. Create Conflict.
To create a conflict, we are going to follow the steps below.
- 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
.
- Create a new branch from the
master
branch for this case, it can be called any name, here thedev
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.
dev
branch is a copy of master
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 andmaster
branch, the two branches can merge successfully.
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.
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
master
branch had no <h2> when we branched off to dev
branch.dev Branch
master
branch having Html document with <h1> and adding a <h2> with Dev Branch Content
.Now
master
branch has been updated after creating thedev
branch.The initial state of
master
branch from which thedev
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 behindmaster
Ahead - The
dev
branch has one commit that is not yet in themaster
branch. This indicates that there has been a new change or addition in thedev
branch that hasn't been merged intomaster
.Behind - The
dev
branch is missing one commit that is present in themaster
branch. This indicates that there has been a change or addition in themaster
branch that hasn't been merged intodev
.From the screen above click on
Contribute
and thencompare
.
Now, a merge between
master
anddev
git branches is not possible. There is an issue / conflict with the two branches.
Conflict Details
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
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.
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
Thanks, Happy Coding
Subscribe to my newsletter
Read articles from isiagi geofrey directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by