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

9 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

  4. i’ve solved git configuration to use IntelliJ as merge tool like this in he .gitconfig file :

    [mergetool “intellij”]
    cmd = \”C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2022.1/bin/idea64.exe\” merge \”$LOCAL\” \”$REMOTE\” \”$BASE\” \”$MERGED\”
    trustExitCode = true
    [difftool “intellij”]
    cmd = \”C:/Program Files/JetBrains/IntelliJ IDEA Community Edition 2022.1/bin/idea64.exe\” diff \”$LOCAL\” \”$REMOTE\”

    i wrote my answer here : https://stackoverflow.com/questions/72018546/ideas-community-edition-merge-tool-configuration-in-gitconfig-for-windows-plat

    Like

Leave a comment