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:

  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

  • How to discard the changes I don't commit?
  • Version control setup for a tutorial
  • Adding an user to the repository with read access for specific directories
  • Synchronizing code with two subversion repositories
  • How do I specify a merge-base to use in a 'hg merge'
  • “Frankensteining” a fork from a git repository - allow merge-backs of moved files
  • Does Perforce support distributed version control systems?
  • TFS vs SVN
  • 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.