+1 vote
in Git by
What is a conflict in Git and how to resolve it?

2 Answers

0 votes
by

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:

Open Git Bash (Git command line).

Use cd <repository-name> command to go to the local Git repository which is having the merge conflict.

Use the git status command to produce the list of files affected by the merge conflict.

Open the text editor that you use and traverse to the file that has merge conflicts.

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.

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.

Use git adds. command to add or stage your changes.

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:

Open Git Bash (Git command line).

Use cd <repository-name> command to go to the local Git repository that has the merge conflict.

Use the git status command to produce the list of files affected by the merge conflict.

Open the text editor that you use and traverse to the file that has merge conflicts.

Choose if you wish to keep the removed file. You can check the latest changes done in the removed file in your text editor.

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.

Finally, use the git commit -m “message” command to commit your changes with a comment.

0 votes
by

When GIT is not able to predict which change has to be given preference, a conflict arises. It usually happens when you want to merge a commit, and there is a change in one place. If the same change already exists while merging, GIT will not understand whether the former or latter change has to be implemented, leading to conflict.

To resolve such conflicts, you need to edit files and fix conflicting changes. After this, you have to run the ‘git add’ command to add the resolved files. Further, run the ‘git commit’ command for incorporating the repaired damage.

Related questions

0 votes
asked Mar 26, 2021 in Git by rajeshsharma
+1 vote
asked May 14, 2020 in DevOps by sharadyadav1986
...