6 EASY Steps to Resolve Git Merge Conflicts in IntelliJ IDEA

6 EASY Steps to Resolve Git Merge Conflicts in IntelliJ IDEA

As a beginning software engineer, encountering pull request merge conflicts can be a daunting experience. However, resolving these conflicts is a crucial skill in collaborative software development. In this guide, we’ll walk you through the process of resolving pull request merge conflicts using IntelliJ Community Edition, ensuring you can handle these situations with confidence.

Understanding Git Merge Conflicts

Merge conflicts occur when two branches have changes that contradict each other. This typically happens when multiple developers work on the same codebase simultaneously. When you try to merge a pull request with conflicting changes, Git will flag these conflicts, requiring manual resolution.

Preparing Your Environment

Before diving into conflict resolution, ensure you have IntelliJ Community Edition installed. If not, you can download it from JetBrains’ official site.

Steps to Resolve Merge Conflicts

1. Pull the Latest Changes

First, ensure your local repository is up-to-date with the remote repository. Open terminal in IntelliJ and run following command, replace [branch_name] with the name of the branch you want to merge your changes in.

git pull origin [branch_name]

This command fetches and merges changes from the remote branch into your current branch.

Note: After running above command you might encounter following warning message, this prompt is shown to choose one of the git reconciliation strategy.

Software engineer resolving git merge conflicts in IntelliJ Idea

Run the following command

git config pull.rebase false

And then re-run the above git pull command.

2. Open IntelliJ IDEA and Locate Conflicts

Open IntelliJ Community Edition and navigate to your project. IntelliJ will automatically detect merge conflicts and notify you.

To see the list of files with conflicts:

  1. Go to Git > Commit.
  2. Click on the Resolve.
  3. Expand the Merge Conflicts section.

3. Start Resolving Conflicts

Double click on a file listed under Merge Conflicts to open the merge conflict editor. IntelliJ provides a three-way merge tool, showing:

  • Left: The changes from your branch.
  • Center: The base version (common ancestor).
  • Right: The changes from the feature branch.

Choosing Changes

You can choose which changes to keep:

  • Accept Left: Keeps your branch’s changes.
  • Accept Right: Keeps the feature branch’s changes.
  • Accept Both: Combines both changes.

Use the buttons in the editor to accept changes:

  • Click the left arrow to accept your changes.
  • Click the right arrow to accept the incoming changes.
  • Use the center area to manually edit and combine changes if necessary.

4. Manual Edits

Sometimes, automated tools can’t resolve conflicts accurately. In such cases, manually edit the center pane to combine the changes logically. Once done, mark the conflict as resolved by clicking the Mark as Resolved button.

5. Continue the Merge Process

After resolving all conflicts, complete the merge process with the following command:

git merge --continue

After the above command, terminal editor will open similar to following, type :q and press enter to exit.

This command finalizes the merge and commits the resolved changes.

6. Push the Changes

Finally, push the resolved changes to the remote repository:

git push origin your-branch

You can also use IntelliJ git UI to push the changes instead of using the above command.

Best Practices for Avoiding Merge Conflicts

  1. Frequent Pulls: Regularly pull changes from the main branch to stay updated.
  2. Small, Incremental Changes: Make small and frequent commits to minimize conflicts.
  3. Clear Communication: Coordinate with your team to avoid working on the same files simultaneously.
  4. Feature Branches: Use feature branches to isolate new features and bug fixes from the main codebase.

Conclusion

Resolving pull request merge conflicts is a vital skill for any software engineer. Using IntelliJ robust tools makes this process more manageable. By following this guide, you should be able to resolve conflicts efficiently and continue your development work with minimal disruption. Happy coding!


Read more beginner’s friendly blogs here