Git: Resolving Merge Conflicts

I am taking the following example to ask a question about a merge conflict that I encountered at work.

Suppose I have a file named mainFile.txt in the main branch , and its contents are:

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch

      

From the main branch, I created two branches, branchA and branchB, each with newlines (from line 4 in both branches) added to the same mainFile.txt file:

Content of mainFile.txt in branchA :

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch
  4 This is the fourth line in branchA
  5 this is the fifth line in branchA

      

MainFile.txt content in branch :

  1 this is the first line in master branch
  2 this is the second line in master branch
  3 this is the third line in master branch
  4 This is the fourth line in branchB
  5 This is the fifth line in branchB
  6 This is the sixth line in branchB

      

Now I need to merge both branches back to master. No problem when I first merge branchA to take over. However, merge conflicts occur when I merge branch B to master. In the following screenshot, for merge conflicts, I need to keep all lines from branches and branches. I was wondering how I should resolve the merge conflicts. I'm trying to use vimdiff, but I feel like I need to make a decision to keep either branch (not both).

Thanks for the help! enter image description here

+3


source to share


2 answers


It's better to use any three-way merge tool like meld http://meldmerge.org/ , which also takes into account a common ancestor for both of these branches, which means it's easier for you to identify changes and work through the merge. Using vi or a text editor to combine a large and conflicting piece of code becomes difficult as you can't see the comparison on the same screen without scrolling down. You can choose to keep both branches when starting the merge with git mergetool

on conflicts



+3


source


First you can merge branchA with master. After that, when you try to merge branch B with master from the command line, you will see an error like

Automatic merge error; fix conflicts and then commit the result

Now open your mainFile.txt branch. You will get one line with the code



<<<<<<<HEAD

Because of the code below this line, your branch has a conflict. Make changes to your code manually according to your final requirements.

After that add, commit and push your mainFile.txt file. Then you can resolve conflicts.

+1


source







All Articles