Skip to content

Instantly share code, notes, and snippets.

@ffittschen
Last active August 13, 2024 11:39
Show Gist options
  • Save ffittschen/6d9be1720f30eb8dc0142cc0ed91c7d9 to your computer and use it in GitHub Desktop.
Save ffittschen/6d9be1720f30eb8dc0142cc0ed91c7d9 to your computer and use it in GitHub Desktop.
Use IntelliJ or WebStorm as git mergetool with SourceTree

Use IntelliJ or WebStorm as git mergetool

Configure Sourcetree

The merge tool that ships with JetBrain's IntelliJ IDEA or WebStorm is really awesome and way better than FileMerge. It is quite simple to configure SourceTree to use it as the default mergetool:

  1. Open the SourceTree preferences and select the Diff tab

  2. Choose Custom both as Visual Diff Tool and as Merge Tool

  3. Paste the following commands into the textfields:

    • Diff Command: /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea diff

      • Arguments: $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE")
    • Merge Command: /Applications/IntelliJ\ IDEA.app/Contents/MacOS/idea merge

      • Arguments: $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
  4. Close the settings. Happy merging!

Git in general

To manually change the mergetool for git in general follow these steps:

  1. Open the ~/.gitconfig file with your favorite editor

  2. Add these lines somewhere in the config:

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

Downsides

It takes a few more seconds to launch the IntelliJ merge tool than to launch FileMerge, but it is definitely worth waiting.

Credits

Based on this article on coderwall: https://coderwall.com/p/gc_hqw/use-intellij-or-webstorm-as-your-git-diff-tool-even-on-windows

@Rotemy
Copy link

Rotemy commented Dec 8, 2019

Thanks.

I've used this to work with webstorm:

[difftool]
    prompt = false

[merge]
        tool = webstorm
[difftool "webstorm"]
        cmd = /Applications/WebStorm.app/Contents/MacOS/webstorm diff $(cd $(dirname \"$LOCAL\") && pwd)/$(basename \"$LOCAL\") $(cd $(dirname \"$REMOTE\") && pwd)/$(basename \"$REMOTE\")
        trustExitCode = true
[mergetool "webstorm"]
        cmd = /Applications/WebStorm.app/Contents/MacOS/webstorm 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

[alias]
	dt = difftool --dir-diff

@maxjf1
Copy link

maxjf1 commented Jul 30, 2020

An easier solution (you need to enable run on command line on jetbrains toolbox) and works with almost all jetbrainds IDEs:

[diff]
    tool = phpstorm
[difftool "phpstorm"]
    cmd = phpstorm diff $LOCAL $REMOTE
[merge]
    tool = phpstorm
[mergetool "phpstorm"]
    cmd = phpstorm merge $LOCAL $REMOTE $BASE $MERGED
    trustExitCode = true

@7rulnik
Copy link

7rulnik commented Jun 2, 2021

With this configuration, I get this error

cmd = idea merge $LOCAL $REMOTE $BASE $MERGED

image

So tmp files deleted before the IDEA opened it.

As a workaround I added read:

cmd = idea merge $LOCAL $REMOTE $BASE $MERGED && read

but after this, I need to press enter after each file.

How did you solve it?

git version 2.31.1
IDEA 2021.1.2

@7rulnik
Copy link

7rulnik commented Jun 2, 2021

Figured out that I need --wait or -W flag:

cmd = webstorm merge $LOCAL $REMOTE $BASE $MERGED --wait

@cefn
Copy link

cefn commented Dec 14, 2021

I found the .gitconfig and git difftool --dir-diff solution at https://youtrack.jetbrains.com/issue/IDEA-284927#focus=Comments-27-5653070.0-0 was effective for me on Big Sur with a JetBrains Toolbox installation of IntelliJ Idea. This doesn't rely on the command line aliases for IntelliJ Idea being installed or working properly.

.gitconfig

[diff]
   tool = intellij
[difftool "intellij"]
   cmd = open -nWa 'IntelliJ IDEA.app' --args diff $(realpath "$LOCAL") $(realpath "$REMOTE")

Example invocation

git difftool --dir-diff -y -t intellij 59af7299a HEAD

@locofocos
Copy link

I recently updated from RubyMine some 2020 version to 2021.3.2. It started running into an error, saying it couldn't find the file. I tried the wait option, which didn't work. I also tried manually adding a sleep statement, like cmd = sleep 5 && mine merge .... but that didn't work either.

Ultimately I had to change trustExitCode = true to trustExitCode = false, and that causes Rubymine to successfully open the merge window. This requires typing "y" after each file though.

Before I was using this long command:

cmd = mine 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")

but now it's working with just

cmd = mine merge $LOCAL $REMOTE $BASE $MERGED

@zur4ik
Copy link

zur4ik commented Mar 21, 2023

Who has IDEA installed with Jetbrains Toolbox and using idea command like me, will occur problem when tmp files are removed before idea diff/merge window opens.
In this case --wait or -W flag will be handy, but it will not work util small fix in idea script file:

//script file: /Users/<USER>/Library/Application Support/JetBrains/Toolbox/scripts/idea

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

Finally my .gitconfig file looks like:

[difftool]
	prompt=false
[merge]
        tool = intellij
[difftool "intellij"]
        cmd = idea diff $LOCAL $REMOTE --wait
[mergetool "intellij"]
        cmd = idea merge $LOCAL $REMOTE $BASE $MERGED --wait
        trustExitCode = true

And all works as expected 🙂

Credit: https://youtrack.jetbrains.com/issue/IDEA-264471/idea-diff-command-does-not-recognize-wait-argument#focus=Comments-27-4944776.0-0

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