Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Resolving Merge Conflicts in Git

Wednesday, October 25th, 2017 by Servage

gitSooner or later every developer working with Git encounters a merge conflict. Especially if you are working with other developers, it’s possible that the code you are working on is changed by someone else at the same time. When that happens, you have to choose whose work you want to keep and what to discard, or perhaps you want to combine them by taking a piece from both versions. Let’s see what tools are available to do this and how the “git mergetool” command works.

How Merge Conflicts Happen

A merge conflict happens when you edit a file that changes in the remote repository at the same time. When one developer first commits and pushes his changes, the second developer is unable to push his commits to the same branch without a conflict. Conflicts can happen regardless of how many branches there are in your workflow. Even when working on a single master branch, if multiple developers are working on it at the same time, you can end up with conflicts.

How to Solve Conflicts

When a merge conflict happens, Git tells you about this on the command line. You will also see a list of conflicted files. When you open any of these files, you will notice some lines Git has automatically added to those files. They look like “<<<<<<< HEAD” and “>>>>>>> origin/master”. The conflicted changes are found inside these lines, and your job is to choose which version of the two you want to keep: your version or the version of some other developer who worked on the file at the same time with you.

One way to solve conflicts is to manually remove the lines Git added and one of the conflicting version by editing the file with any text editor.

Another way is to set up a merge tool, a visual program that allows you to compare the two conflicting versions in a side-by-side comparison. Merge tools also often let you select a resolution for a conflicting line from either of the versions with a single click. This is usually a faster way and the visual feedback you get makes it easier to see the differences between the versions. You can start your merge tool by running “git mergetool” when a merge conflict has happened. You may have to configure what program to use as your default merge tool in a .gitconfig file in your home directory. When you run the command, the tool will open and detect all conflicted files for you.

A third option, and perhaps the easiest one, is to use an IDE with a built-in merge tool. A good example of this is the IntelliJ product family, which consists of IDEs like PhpStorm, IntelliJ IDEA and WebStorm.

When you have modified the files to your liking and no more merge conflicts are found, you can run “git status” to see if the conflicts have been dealt with correctly. After this, you can push your changes as usually.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.