in Technology by

What is a conflict in Git and how to resolve it?

1 Answer

0 votes
by

Answer: Git has an automatic merging feature that handles the merge commits on its own, provided the code changes have occurred on different lines and in different files.

But, in case of competing for commits where there are changes in the same lines of code of a file or a file has been deleted in one branch but exists and modified in another, Git is unable to automatically resolve differences and thus raises merge conflict.

In such cases, it requires your help to decide which code to include and which code to discard in the final merge.

A merge conflict can occur during merging a branch, rebasing a branch, or cherry-picking a commit. Once a conflict is detected, Git highlights the conflicted area and asks you to resolve it. Once the conflict is resolved, you can proceed with the merge.

Follow the below steps to resolve a competing line change merge conflict:

  1. Open Git Bash (Git command line).
  2. Use cd <repository-name> command to go to the local Git repository which is having the merge conflict.
  3. Use the git status command to produce the list of files affected by the merge conflict.
  4. Open the text editor that you use and traverse to the file that has merge conflicts.
  5. To see the start of the merge conflict in your file, look the document for the conflict marker <<<<<<<. At the point when you open the file, you’ll observe the modifications from the HEAD or base branch after the line <<<<<<< HEAD. Then, you’ll observe =======, which partitions your modifications are from the modifications in the other branch, trailed by >>>>>>> BRANCH-NAME.
  6. Choose in the event that you need to keep just your branch’s changes, just keep the other branch’s changes, or make a fresh change, that may include changes from the two branches. Erase the conflict markers <<<<<<<, =======, >>>>>>> and do the changes that you need in the final merge.
  7. Use git adds. command to add or stage your changes.
  8. Finally, use the git commit -m “message” command to commit your changes with a comment.

To resolve the removed file merge conflict, you need to follow the below steps:

  1. Open Git Bash (Git command line).
  2. Use cd <repository-name> command to go to the local Git repository that has the merge conflict.
  3. Use the git status command to produce the list of files affected by the merge conflict.
  4. Open the text editor that you use and traverse to the file that has merge conflicts.
  5. Choose if you wish to keep the removed file. You can check the latest changes done in the removed file in your text editor.
  6. Use git add <filename> command to add the removed file back to the repository. Or, Use git rm <filename> command to remove the file from your repository.
  7. Finally, use the git commit -m “message” command to commit your changes with a comment.

Related questions

0 votes
    How to resolve a conflict in Git?...
asked Nov 2, 2020 in Technology by JackTerrance
0 votes
    As asked in this question, I also want to know how to resolve a conflicting git stash pop without ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    What is a merge conflict in Git, and how can it be resolved?...
asked Oct 4, 2020 in Technology by Editorial Staff
0 votes
    How to resolve merge conflicts in Git repository?...
asked Jan 7, 2021 in Technology by JackTerrance
0 votes
    I used git pull and had a merge conflict: unmerged: _widget.html.erb You are in the middle of a conflicted merge. ... can I do this? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I have created a branch named dev. I have done a pull request to send dev code to master, when I do ... to resolve these conflicts. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    Known exploits have a confirmation of and measures can be taken against it to resolve them. (a) True (b) False ... Security questions and answers pdf, mcq on Cyber Security pdf,...
asked Nov 4, 2021 in Education by JackTerrance
0 votes
    If I want to use the validation framework that you can use with ASP.NET MVC, will the JavaScript ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    How can we abort the merge if there is a merge conflict?...
asked Jan 8, 2021 in Technology by JackTerrance
0 votes
    How is India trying to resolve its differences with China? Please answer the above question....
asked Aug 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to build my app on a physical device. Each time I try to build I get the following ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have created new controller in the project with gii but I am getting Not Found (#404) error when ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 2, 2022 in Education by JackTerrance
0 votes
    This one has been bugging me for a while now. Is there a way I can stop Intellj IDEA from ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    I recently moved my website to a shared hosting solution at asmallorange.com, but I had to set my ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
...