Resolving Merge Conflicts

When working with other people on a Git repository, you’ll eventually encounter merge conflicts. This happens when two people attempt to make modifications to the repository, causing one to be pushed while the other must either replace the changes made by the first or somehow merged. The easiest way to avoid merge conflicts is to work on different files from other people, which is usually easy with OOP languages. Sometimes I will forget to push changes though, and if someone else edits that file under the impression it’s the latest version for everyone then when I go to push my version I’ll encounter an error as the person who modified it already pushed their changes.

The easiest way to resolve conflicts is to simply decide if you want your changes to overwite or to keep their changes. Usually this isn’t ideal, since sometimes you want your changes along with theirs. The IntelliJ IDE (and most IDEs) has an option which allows you to select which changes to keep individually in a file, going change by change. For example, in one function I added an event that gets fired.

Someone else in their version of the file added some debug statements. I want both these changes, but because both files are different Git doesn’t know how to add the differences together without duplicate code. IntelliJ allows you to select which sections to add or remove from both files, giving you the result file. If two changes conflict you can simply choose one and ignore the other.

I had to do this recently when I forgot to push my changes from a project I did weeks ago, and it was recently revamped with package names changing and some code optimization done in most of the classes. I had to go through each conflicting file and use IntelliJ’s conflict resolving feature to allow both changes to merge.

Leave a Reply

Your email address will not be published. Required fields are marked *