In software development, the process during which you combine two (or possibly more) source code files together to form a new file that incorporates all features from both previous files. This concept is vital if you are to allow two or more developers to work on files concurrently. Merge functionality is often incorporated into version control systems, and is used extensively when a code branch is added to another code branch.

  1. Here we have a simple source code file, in Java (I've numbered the lines for clarity):
    1 /**
    2  * The HelloWorld class implements an application that
    3  * simply displays "Hello World!" to the standard output.
    4  */
    5 class HelloWorld {
    6     public static void main(String[] args) {
    7         System.out.println("Hello World!");
    8     }
    9 }
    
  2. In this simple example, imagine we have two developers. The first engineeer, Alice, will modify the code to provide a more verbose message. The second engineer, Bob, will add code to display a goodbye message. Alice makes a copy of the file and changes the output line (line 7) to show the new greeting (changed text in bold):
    1 /**
    2  * The HelloWorld class implements an application that
    3  * simply displays "Hello World!" to the standard output.
    4  */
    5 class HelloWorld {
    6     public static void main(String[] args) {
    7         System.out.println("Greetings World from Globalex Corporation!");
    8     }
    9 }
    
    Bob now makes another copy of the file and adds his line (added text in bold):
    1 /**
    2  * The HelloWorld class implements an application that
    3  * simply displays "Hello World!" to the standard output.
    4  */
    5 class HelloWorld {
    6     public static void main(String[] args) {
    7         System.out.println("Hello World!");
    8         System.out.println("Goodbye to all our valued customers!");
    9    }
    10 }
    
    Note that Bob's copy of the file does not show Alice's changes.
  3. Now we have 2 versions of the file, each with valuable changes. We need to merge these two files together to form a new file. To accomplish this, we compare each line of each file side by side, and add or change a new file to match both versions. In a small file, such as the one used in this example, we can do this manually, but there are many software tools to automatically merge files together:
    1. no changes for this line
    2. no changes
    3. no changes
    4. no changes
    5. no changes
    6. no changes
    7. Here, there is a conflict between Alice's and Bob's file. A good merge tool will bring this to the attention of the person performing the merge, possibly like so:
      <<<<<<< AliceHelloWorld.java
              System.out.println("Greetings, World, from Globalex Corporation!");
      =======
              System.out.println("Hello World!");
      >>>>>>> BobHelloWorld.java
      
      The merge tool will add these lines to the merged file, expecting the developers to edit the code to resolve the conflict. A version control tool like CVS will not allow a file to be checked in unless these conflicts have been resolved.
    8. Here, a new line has been added so the tool will add the line to the merged output file. There is no conflict here because most merge tools are intelligent enough to realise that Bob's file is one line longer than Alice's, and lines 8 and 9 in Alice's file haven't changed, therefore Bob must have added a line.
    9. no changes
    10. no changes
    The outputted merge file will look like this:
    1  /**
    2   * The HelloWorld class implements an application that
    3   * simply displays "Hello World!" to the standard output.
    4   */
    5  class HelloWorld {
    6      public static void main(String[] args) {
    7  <<<<<<< AliceHelloWorld.java
    8          System.out.println("Greetings, World, from Globalex Corporation!");
    9  =======
    10         System.out.println("Hello World!");
    11 >>>>>>> BobHelloWorld.java
    12         System.out.println("Goodbye to all our valued customers!");
    13    }
    14  }
    
  4. As Alice is the developer whose line conflicts, she would normally be the one to resolve the conflict. Once the conflict has been resolved, the completed file will look like this:
    1  /**
    2   * The HelloWorld class implements an application that
    3   * simply displays "Hello World!" to the standard output.
    4   */
    5  class HelloWorld {
    6      public static void main(String[] args) {
    7      System.out.println("Greetings, World, from Globalex Corporation!");
    8      System.out.println("Goodbye to all our valued customers!");
    9     }
    10  }