The super-easy way to resolve git merge conflicts

Posted by

What is merge conflict?

While building large projects, multiple developers are involved. Therefore there are situations when two developers working on two different branches trying to change the same line of code.

Git’s internal mechanism fails to solve this conflict. Finally, it leads to a merge conflict. In this blog, we shall discuss how we can resolve git merge conflicts in both github and local machine.

On the other hand, we shall also try of create merge conflict that will help us to understand merge conflicts in depth.

 

How to create a merge conflict?

 

 

This image above shows the initial code in the master branch. Now, we shall create a new branch called firstbranch. Therefore firstbranch will also have the same code as the master branch.

To create a new branch, execute the command as shown below

$git branch firstbranch

Remember now we are in the master branch. We shall add a new print statement in line number 5. Refer to the picture given below.

 

Now we shall commit the changes that we made in the master branch and push it to remote. The following code does this.

$ git add file.py
$ git commit -m “line5 added”
$ git push

Now we shall go to the firstbranch.

Remember, as we have created “firstbranch” branch before adding any print statement in the master branch. Therefore line number 5 in the “firstbranch” branch will be empty.

Again we shall add a print statement with some different text at line number 5.

git merge conflicts

Now we shall commit the changes of firstbranch.

$ git checkout firstbranch
$ git add file.py
$ git commit -m “line 5 added”.

In the present state, we have two branches and at line 5, have two different print statements. We shall push in changes to Github repo.

The command below will create and push the local branch to the remote.

$git push -u origin firstbranch

git merge conflicts

Now we shall try to make a merge request in the github webpage.

Click on Compare & pull request

git merge conflicts

Git will take us to the “Open a pull request page”

 

git merge conflicts PR

Now Github will say “This branch has conflicts that must be resolved”.

resolve conflict

How to resolve merge conflicts in Github webpage?

 

To see the conflicting code, click on “Resolve conflicts”.

git merge conflicts resolve

There are three different symbols: <<<<<< firstbranch, ======, >>>>>>>master 

The equal to the symbol is the separator between the two branches.

The code just below <<<<<< firstbranch, belongs to the first branch

Similarly, the code just above >>>>>>>master, belongs to the master branch.

Let’s say we want to keep the code of “firstbranch”. In that case, we shall simply delete >>>>>>>master, code, ====,<<<<<< firstbranch  symbols.

git merge conflicts resolve

After removal of the merge conflicts, the code below is the final one that will remain in the master branch.

in the image below, click on commit merge, this is merge the firstbranch to master branch

commit merge

How to resolve git merge conflicts in local machine?

The situation in the local machine is very similar to GitHub. Therefore, If branch A and branch B are having two different statement at the same line then there will be a merge conflict.

The picture below illustrates the same

 

git merge conflicts resolve

Conclusion:

we discussed about merge conflicts that often happen when we try to merge two branches. Similarly, we also discussed how to create a merge conflict and how to resolve it by using the GitHub webpage and any basic editor.

In our upcoming blog, we shall try to simulate more types of merge conflict and way resolve git merge conflicts

 

Leave a Reply

Your email address will not be published. Required fields are marked *