Using IDEA for Git merging and diffing

A cool bit of CLI trickery that James Humphrey shared with me. This allows you to use IDEA for diffing and merging via git’s mergetool and difftool commands.

First, add IDEA to your path (everything is in Mac-speak here):

Bash

export INTELLIJ_HOME /Applications/IntelliJ IDEA 12.app/Contents/MacOS
PATH=$IDEA_HOME $PATH

Fish

set INTELLIJ_HOME /Applications/IntelliJ IDEA 12.app/Contents/MacOS
set PATH $INTELLIJ_HOME $PATH

Next, add these lines to your ~/.gitconfig file:

[merge]
   tool = intellij
[mergetool "intellij"]
   cmd = idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
   trustExitCode = true
[diff]
   tool = intellij
[difftool "intellij"]
   cmd = idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")

That’s it. Now you can use IDEA to diff and merge from git CLI like this:

$ git mergetool
$ git difftool

8 thoughts on “Using IDEA for Git merging and diffing

  1. If you’re having trouble getting this to work in MacOS/Linux (in particular, if invoking git diff/merge fails to bring up the IDEA diff/merge window), please note that JetBrains recommends creating a command-line launcher (Tools | Create Command-line Launcher) instead of putting the idea binary on your PATH directly: https://www.jetbrains.com/help/idea/running-intellij-idea-as-a-diff-or-merge-command-line-tool.html

    See also: https://youtrack.jetbrains.com/issue/IDEA-183500

    Like

  2. This doesn’t work if idea is opened already. As command exits instantly, and files got deleted after that, Idea has not files to compare.

    Like

  3. If there are multiple files which need to be merged when you run git mergetool it seems like only the first file gets opened properly in IntelliJ. After that I get errors about unable to find LOCAL file.

    Like

    1. That’s strange. I wonder what Git is sending as parameters. That’s probably either an IJ bug of not handling multiple files passed in, or a git issue where it isn’t spawning the sub-process multiple times correctly. I’ll have to play around with it and see what is going on. Let me know if you figure it out.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s