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:

  • Git says a file is unmerged and I can't commit, but the file seems to be merged
  • Managing trunk and feature branches: local delete, incoming delete upon merge
  • GIT 2 or more merge conflicts in a single file - how p4merge handles?
  • git: solving conflicts caused by rebase
  • git stash and edited hunks
  • git submodule merge conflict: how to visualize?
  •   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 merge conflict since I deleted a file
  • git how to find commit hash where branch originated from
  • How can I checkout out the HEAD version of my remote/tracking branch
  • Remove last commit from remote git repository
  • Undo a git rerere resolution that was done in a rebase
  • Cloning a git-from-svn repository and getting all the remote branches
  • 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.