What is merge conflict? How is it resolved?

What is merge conflict?

A merge conflict is?an event that occurs when Git is unable to automatically resolve differences in code between two commits. When all the changes in the code occur on different lines or in different files, Git will successfully merge commits without raising merge conflict. But when the changes in the code occur on same lines in same file, that is when merge conflict occurs.

Let us see this with an example. Consider there is a repository on github.com named demo_merge_conflict. This repo has a file called 'demo_file" hosted on main branch. Please find below the contents of the file:

No alt text provided for this image

Notice that line number 3 outputs "text"

Let us assume 2 developers - Developer A and Developer B are working on this code simultaneously.

Developer A, created a feature branch - 'Developer A' and edits the file with the output "I am a tech enthusiast, but I don't follow Shruthi". He commits the file and merges this branch with main.

So the 'demo_file" hosted on main branch looks like below:

No alt text provided for this image

At the same time developer B is working on the same demo_file and makes changes in a new feature branch 'DeveloperB'. The demo_file in feature branch 'DeveloperB' looks like below:

No alt text provided for this image

Now since both developers A and B have made changes to same line '3' on same file 'demo_file', this will lead to a merge conflict when B tries to merge his branch 'DeveloperB'. This can be seen in the picture below:

No alt text provided for this image

Notice that the 'merge pull request' button is greyed out. You will not be able to merge until the conflict is resolved. You can use the 'resolve conflicts' button to edit code and resolve the conflict.

How to resolve merge conflict?

In order to resolve the conflict both developers A and B sit together and after discussing they come to a common ground and decide on what code should go into line 3 and then use the resolve conflict button to update the decided code. Later this can be committed and merged.

In our example, developer A and B decide to put code 'System.out.println("I am a tech enthusiast. I follow Shruthi on LinkedIn and I have also subscribed to TechBytes")' on line 3. So now the same is updated in the code line 3 by clicking on resolve conflict. Later this is committed and DeveloperB branch is merged successfully with main. Now the code on main looks like below image:

No alt text provided for this image

要查看或添加评论,请登录

Shruthi Rajkumar的更多文章

  • Lets Decode some security Jargons! CSRF, XSS and DoS

    Lets Decode some security Jargons! CSRF, XSS and DoS

    CSRF Cross-Site Request Forgery (CSRF) is an attack that forces authenticated users to submit a request to a Web…

    1 条评论
  • What is 'git merge'? Difference between 'git merge' and 'git rebase'

    What is 'git merge'? Difference between 'git merge' and 'git rebase'

    Git merge collates all the commits from the feature branch and adds it as a new commit on the master or main branch. As…

  • What is Git Rebase?

    What is Git Rebase?

    Rebasing is the process of moving or combining a sequence of commits to a new base commit. Let us try to understand…

  • Reality of Metaverse!

    Reality of Metaverse!

    As Web 3.0 is evolving, many new inventions are coming up, of which Metaverse is a popular one.

  • What is NFT?

    What is NFT?

    NFT stands for 'Non-Fungible Token' NFT is built upon blockchain technology. To understand more about blockchain check…

  • What is BlockChain technology?

    What is BlockChain technology?

    A brief history: The blockchain technology was described by the research scientist Stuart Haber and W. Scott Stornetta…

  • What is Terraform Workspaces?

    What is Terraform Workspaces?

    Working with Terraform involves managing collections of infrastructure resources, and most organizations manage many…

  • Difference between GCP Cloud build and GCP Cloud Deploy?

    Difference between GCP Cloud build and GCP Cloud Deploy?

    There is a difference between GCP Cloud Build and GCP Deploy. Cloud Build is a service that executes your builds on…

  • What are the differences between "git pull", "git pull origin master", and "git pull origin/master"?

    What are the differences between "git pull", "git pull origin master", and "git pull origin/master"?

    We should remember, a git pull is a combination of git fetch and a git merge. There are different variations of 'git…

  • What is GCP Cloud Shell?

    What is GCP Cloud Shell?

    Below are few points that simplify the understanding of GCP Cloud Shell Cloud Shell provides command-line access to…

社区洞察

其他会员也浏览了