0 votes
20 views

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

in Technology by (191k points)

1 Answer

0 votes

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.
by (191k points)

Related questions

0 votes
1 answer 34 views
0 votes
1 answer 38 views
0 votes
1 answer 16 views
16 views asked Nov 2, 2020 in Technology by JackTerrance (191k points)
0 votes
1 answer 17 views
0 votes
1 answer 23 views
0 votes
1 answer 22 views
0 votes
1 answer 37 views
37 views asked Jan 8 in Technology by JackTerrance (191k points)
0 votes
1 answer 27 views
0 votes
1 answer 22 views
0 votes
1 answer 21 views
0 votes
1 answer 25 views
0 votes
1 answer 29 views
0 votes
1 answer 21 views
0 votes
1 answer 22 views
0 votes
1 answer 22 views
0 votes
1 answer 24 views
0 votes
1 answer 32 views
0 votes
1 answer 32 views
32 views asked Jan 8 in Technology by JackTerrance (191k points)
0 votes
1 answer 23 views
23 views asked Jan 7 in Technology by JackTerrance (191k points)
0 votes
1 answer 22 views
0 votes
1 answer 22 views
0 votes
1 answer 21 views
...