Skip to content

Instantly share code, notes, and snippets.

@rambabusaravanan
Last active March 17, 2024 10:31
Show Gist options
  • Save rambabusaravanan/1d1902e599c9c680319678b0f7650898 to your computer and use it in GitHub Desktop.
Save rambabusaravanan/1d1902e599c9c680319678b0f7650898 to your computer and use it in GitHub Desktop.
Git Diff and Merge Tool - IntelliJ IDEA
# Linux
# add the following to "~/.gitconfig" file
[merge]
tool = intellij
[mergetool "intellij"]
cmd = /usr/local/bin/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 = /usr/local/bin/idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")
# Mac
# add the following to "~/.gitconfig" file
[merge]
tool = intellij
[mergetool "intellij"]
cmd = /Applications/IntelliJ\\ IDEA.app/Contents/MacOS/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 = /Applications/IntelliJ\\ IDEA.app/Contents/MacOS/idea diff $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")
# Windows
# add the following to "C:\Users\<username>\.gitconfig" file
[merge]
tool = intellij
[mergetool "intellij"]
cmd = cmd.exe //c "\"C:/Program Files (x86)/IntelliJ IDEA Community Edition 12.0/bin/idea.bat\" merge \"$LOCAL\" \"$REMOTE\" \"$BASE\" \"$MERGED\""
trustExitCode = true
[diff]
tool = intellij
[difftool "intellij"]
cmd = cmd.exe //c "\"C:/Program Files (x86)/IntelliJ IDEA Community Edition 12.0/bin/idea.bat\" diff \"$LOCAL\" \"$REMOTE\""

Invoking IntelliJ IDEA from the command line

On OS X or UNIX:
  • Make sure IntelliJ IDEA is running.
  • On the main menu, choose Tools > Create Command-line Launcher. The dialog box Create Launcher Script opens, with the suggested path and name of the launcher script. You can accept default, or specify your own path.
  • Make notice of it, as you'll need it later. Outside of IntelliJ IDEA, add the path and name of the launcher script to your path.
On Windows:
  • Specify the location of the IntelliJ IDEA executable in the Path system environment variable. In this case, you will be able to invoke the IntelliJ IDEA executable and other IntelliJ IDEA commands from any directory.

For more information, visit https://www.jetbrains.com/help/idea/2016.2/running-intellij-idea-as-a-diff-or-merge-command-line-tool.html

Issues and Solutions

@mariohuq
Copy link

mariohuq commented May 20, 2018

On windows version there is some issue:

▒▒▒ 21, 2018 1:11:55 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not open/create prefs root node Software\JavaSoft\Prefs at root 0x80000002. 
Windows RegCreateKeyEx(...) returned error code 5.

I am using Windows 10.0.17133 Build 17133
jdk's release file contents

JAVA_VERSION="1.8.0_152"
OS_NAME="Windows"
OS_VERSION="5.2"
OS_ARCH="amd64"
SOURCE=""

IntelliJ IDEA Community Edition 2018.1
MINGW64 from Git for Widows.

What's the reason?

@contentfree
Copy link

Doesn't seem to work on macOS if Intellij is installed from the JetBrains Toolbox app. (Haven't found the workaround yet, either)

@dror-weiss
Copy link

@contentfree, did you manage to find a solution?

@Yamp
Copy link

Yamp commented Sep 19, 2019

@dror-weiss @contentfree have you found a solution?

@ImanMahmoudinasab
Copy link

ImanMahmoudinasab commented May 14, 2020

@Yamp @dror-weiss @contentfree
Follow steps of the following instruction and you will end up with having a global command idea in your terminal:
https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html

@rwe
Copy link

rwe commented Nov 30, 2020

The problem on macOS is that the shell script generated by JetBrains Toolbox looks like this:

open -na "/Users/…/202.7660.26/IntelliJ IDEA.app/Contents/MacOS/idea" --args "$@"

However, if you man open, you'll see that the additional flag -W is required:

-W Causes open to wait until the applications it opens (or that were already open) have exited. Use with the -n flag to allow open to function as an appropriate app for the $EDITOR environment variable.

Solution

Caveat: The JetBrains toolbox will likely clobber changes to the generated script, so perhaps instead make a new one named like idea-wait/intellij-wait and replace the name after cmd = in the config below. Alternatively again, you could inline entire open -Wna … command. This would make your .gitconfig non-portable, but would remove the need to muck around with the script files.

Add the additional -W flag to the script. It's also not a bad idea to use exec so that sh doesn't sit in the way of signals:

exec open -Wna "/Users/…/MacOS/idea" --args "$@"

Which allows the following portable and correctly-quoted .gitconfig to work:

[difftool "idea"]
	cmd = idea diff \
		\"$(cd \"$(dirname \"$LOCAL\")\" && pwd)/$(basename \"$LOCAL\")\" \
		\"$(cd \"$(dirname \"$REMOTE\")\" && pwd)/$(basename \"$REMOTE\")\" \

[diff]
	tool = idea
	guitool = idea

[mergetool "idea"]
	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\")\" \

[merge]
	tool = idea
	guitool = idea

Note also that if you have realpath available (via brew install coreutils), those awkward \"$(cd \"$(dirname…)\"…)/$(basename …)\" lines can just be replaced by e.g. \"$(realpath \"${LOCAL}\")\".

@tkolleh
Copy link

tkolleh commented Dec 30, 2020

@rwe suggestions worked 👍🏾

@bedge
Copy link

bedge commented Feb 1, 2021

open -na "/Users/…/202.7660.26/IntelliJ IDEA.app/Contents/MacOS/idea" --args "$@"

idea script generated by the Jetbrains toolbox app as of today appears to handle the case @rwe mentioned:
(assuming the caller knows to pass a --wait or -w)

#!/bin/sh
#Generated by JetBrains Toolbox 1.19.7784 at 2021-01-31T16:50:05.012390

declare -a ideargs=()
declare -- wait=""

for o in "$@"; do
  if [[ "$o" = "--wait" || "$o" = "-w" ]]; then
    wait="-W"
    o="--wait"
  fi
  if [[ "$o" =~ " " ]]; then
    ideargs+=("\"$o\"")
  else
    ideargs+=("$o")
  fi
done

open -na "$HOME/Library/Application Support/JetBrains/Toolbox/apps/IDEA-U/ch-0/203.7148.57/IntelliJ IDEA.app/Contents/MacOS/idea" $wait --args "${ideargs[@]}"

Perhaps the OP's config would be OK unmodified with this change?

@contentfree
Copy link

@bedge Still haven't got it to work. I assume that --wait or -w has to be added somewhere in the gitconfig. Have a hint?

@contentfree
Copy link

contentfree commented Mar 5, 2021

As far as I can tell, with the idea that's generated today, attempting idea --wait diff file1 file2 just shows an error in Intellij IDEA:

Cannot execute command
Can not open file
/path/to/pwd/diff

Where as idea diff file1 file2 works as expected.

@thiemok
Copy link

thiemok commented Mar 11, 2021

There is clearly a bug in the handling of the wait flag in the generated script. Hopefully it will be fixed soon. In the meantime you can easily fix it by replacing the contents of the scripts for loop with this:

  if [[ "$o" = "--wait" || "$o" = "-w" ]]; then
    wait="-W"
    o="--wait"
  elif [[ "$o" =~ " " ]]; then
    ideargs+=("\"$o\"")
  else
    ideargs+=("$o")
  fi

@ypkkhatri
Copy link

I'm facing a problem with rendering, when I close the IntelliJ mergetool the main IDE looks weird like the top navbar is gone, the breadcrumb is out of the screen, and so on. After restart, it looks fine again.

Am I the only one facing this?

@leechor
Copy link

leechor commented Nov 7, 2022

Is there any way to keep the code highlight of IDEA?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment