How to fix merge conflict in Pull Request

Reference

Create Pull Request

Create a repository called pullrequest-conflict

Then create a file called cat.txt and paste the following content,

1. In terms of development, the first year of a cat’s life is equal to the first 15 years of a human life. After its second year, a cat is 25 in human years. And after that, each year of a cat’s life is equal to about 7 human years.
2. Cats can rotate their ears 180 degrees.
3. The hearing of the average cat is at least five times keener than that of a human adult.
4. In the largest cat breed, the average male weighs approximately 20 pounds.
5. Domestic cats spend about 70 percent of the day sleeping. And 15 percent of the day grooming.

git

Create a new branch called feature

Change the angle to 150 in second line of cat.txt in feature branch

git

Assume, one more Developer has updated the angle to 200 in cat.txt raised the pull request and merged to master branch.

Lets create this scenario, go to github, change the branch to master edit the cat.txt file and change the angle from 180 to 200 and commit the changes from Github UI

git

Now create a pull request from feature to master branch and you will see merge conflict

Click on Pull Requests tab and then click on compare & pull request

git

Github clearly showing, it has conflict and it cannot merge automatically

git

This is because, we have updated angle to 150 in cat.txt in feature branch, similary one more developer has updated the angle to 200 in in cat.txt in master branch and pushed the changes to github.

While we are creating the pull request, git executes the dry-run to merge the line no 2 from remote cat.txt in feature branch to cat.txt file in master branch , but it is not able to merge, because cat.txt in master branch also has new changes(200).

Lets complete the pull request creation and then we will fix the conflict.

Click on Create pull request git

git

To fix the conflict, clone the repository to local system, switch to feature branch

git clone https://github.com/vigneshsweekaran/pullrequest-conflict.git
cd pullrequest-conflict
git checkout feature

git

Be clear, currently we are in feature branch.

Pull the changes from master to fix the conflict

git pull origin master

We are trying to take the latest changes from master branch, so that we can clearly merge the feature branch to master branch.

git

It has conflict, so automatic merge has failed.

git

Update the cat.txt according to your need, I want to keep angle as 150

git

Add and commit the Fix.

git add .
git commit

git

Check the commit logs,

git log

git

Lets push the latest commit to feature branch,

git push origin feature

git

Go to Github click on Pull requests tab. Click on created pull request.

Now we can see, the lastest push to feature branch has been updated here and conflict has been fixed now.

Click on Merge pull request –> Confirm merge to merge the feature branch with master branch

git

git

How to fix pull request conflict from Github UI itself

Create a new pull request with merge conflict and Click on Resolve conflicts

git

git

Fix the conflict and click on Mark as resolved

git

Click on Commit merge

git

Click on Merge pull request –> Confirm merge

git

git