Git: resolving merge conflicts

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

Suppose I have a file named mainFile.txt in the master branch, and its content is:

  • How do you see / show a git merge conflict resolution that was done, given a merge commit SHA1?
  • Why is a 3-way merge advantageous over a 2-way merge?
  • Git: currently in merge/conflict with private remote repo. How to tell Git to just use my local files?
  • Let git merge different filetypes with an external tool?
  • git: trying to merge branches, but there are TONS of conflicts, and most of them /shouldn't/ be conflicts
  • what causes submodule conficts in git, and how should they be resolved?
  •   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 master branch, I created two branches: branchA and branchB, each has new lines (from line 4 in both branches) added to the same mainFile.txt file:

    The content of the 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
    

    The content of the mainFile.txt in branchB:

      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. There is no problem when I merge branchA first to master. However, merge conflicts show up when I merge the branchB to master. From the following attached screenshot for the merge conflicts, I need to keep all the lines from both branchA and branchB. I was wondering how I should resolve the merge conflicts. I have been trying to use vimdiff, but it seems to me that I need to make a decision of keeping either branch (not both).

    Thanks for help!
    enter image description here

  • git pull *after* git rebase?
  • Merge conflict error, when there was no change in mentioned file
  • File not shown in git diff after a git add. How do I know it will be committed?
  • Git-Credential-Manager-for-Windows also saves invalid credentials
  • Store git fetch output in a txt file
  • How to get rid of “merge branch…” automatic messages in git repository
  • 2 Solutions collect form web for “Git: resolving merge conflicts”

    It is better to use any 3-way merge tool like meld http://meldmerge.org/ which also takes in account the common ancestor for both these branches which means it is easier for you to identify the changes and work through merge. Using vi or text editor to merge large and conflicting code snippet becomes difficult since you cannot see the comparison in the same screen without scrolling down. You can select to retain both branches when you launch the merge using git mergetool when you have conflicts

    First you can merge branchA to the master. After that as you will try to merge the branchB to master from command line you will see the error something like

    Automatic merge failed; fix conflicts and then commit the result

    Now open your mainFile.txt of branchB. You will get one line present with your code

    <<<<<<< HEAD

    Because of the code below this line your branch has conflict. Do changes in your code manually according to your final requirements.

    After that add, commit and push your mainFile.txt. You should be able to get your conflicts resolved after this.

    Git Baby is a git and github fan, let's start git clone.