Git

Git Merge Conflict Example

We are not living in a perfect world. There are conflicts everywhere even in our code. In this example, we shall learn how to deal with these conflicts in our code.

Merge is the process to integrate changes from another branch to our current working branch. This task require communication with our teammates.

In most cases, Git know what to do in merge process but there are some cases when conflicts happens and we have to enter in action. At this point, we have a questions How conflicts are produced in Git? First, when two people change the same line in the same file. Second, when one person delete the file while the other person add changes in that file. Usually, conflicts are solved manually by the person in charge of the merge.

1. Dealing with conflicts

In the example attached, we can find two branches (master, typo). This example contains everything we need to obtain a merge conflict. The repository looks like this:

git-merge-conflict-example-1

  1. Validate if branches already exist using git branch -a.
    * master
    typo
    
  2. Proceed with merge using git merge typo.
    Auto-merging README.md
    CONFLICT (content): Merge conflict in README.md
    Automatic merge failed; fix conflicts and then commit the result.
    

    Our first conflict has arrived. In this case, Git is now able to do the merge automatically. In this step, Git need our help and we will give a hand in order to solve the merge conflict.

  3. git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
    
    Unmerged paths:
      (use "git add ..." to mark resolution)
    
    	both modified:   README.md
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    git status -s

    UU README.md
    

    If we open the README.md file we will find something like this:

    <<<<<<< HEAD
    Master branch
    =======
    Typo branch
    >>>>>>> typo
    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    

    As we can see, file contains the following characters <<<<<<<, ======= and >>>>>>>. That means, we have a conflict. Content before ======= is from our current branch and content after ======= is from the another branch.

    We can also use mergetool then execute git mergetool and terminal will display something similar to the image below:

    git-merge-conflict-example-2

    At the left hand we can find the master’s content whereas at the right hand typo’s content. Both files haven been edited in the same line which produces the conflict. At the bottom, we can find a screen where we will choose which is the correct text.

2. Merge strategies

Git provides a list of merge strategies but in this example we will show you some options from the default merge strategy: recursive. Check the official documentation to know more about them.

In order to execute a strategy you need to perform the following command git merge -s recursive typo. But, since recursive is the default strategy, we can omit and execute git merge typo.

There is a easy way to merge avoiding manually tasks in our example. The recursive strategy can take some options. We have to use -X to tell Git which option we will use. Now, we will take a look two of them:

  1. ours: This option automatically accept changes from our current branch. In this case, changes in master branch has priority. git merge -X ours typo
  2. theirs: This option automatically accept changes from the other branch. In this case, changes in typo branch has priority. git merge -X theirs typo

3. How to undo a merge

If we want to abort the merge process we just need to execute the following command git merge --abort and all changes from the external branch will be deleted. Then, we will be in the state before merge.

4. Conclusion

Merge conflicts always happens in our local machine, never in the git server. Conflicts are solved manually by the person who is merging branches but you can use merge strategies to avoid human interaction. During the merge process, Git allow us to abort merge.

To know more about merge run the following command in our terminal git help merge.

5. Download the source code

Download
You can download the full source code of this example here: GitMergeConflictExample.zip

Eddu Melendez

Eddu is a Peruvian software engineer. He is interested in Java, Spring Framework and Mule ESB. He is also very enthusiastic to learn new stuff.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button