Skip to content

Instantly share code, notes, and snippets.

@dfrommi
Last active June 5, 2022 20:54
Show Gist options
  • Select an option

  • Save dfrommi/4d467d6b84584e484ce73fc35d142af1 to your computer and use it in GitHub Desktop.

Select an option

Save dfrommi/4d467d6b84584e484ce73fc35d142af1 to your computer and use it in GitHub Desktop.
Use IntelliJ IDEA as Mergetool in Git

Use IntelliJ IDEA as Mergetool in Git

IntellJ IDEA contains a very good merge UI, ideal to resolve conflict during merge or rebase. The functionality is accessible also from command line, without having a project with the conflicting sources. Integration into the git commandline flow is quite easy, as git already has the concept of diff- and mergetools.

ℹ️
This description focuses on Mac OS, but the same pattern should be applicable to other operating systems as well.

Preconditions

To make the realpath command available, coreutils is required and can be installed with brew install coreutils

Configuration

To configure IntelliJ IDEA as diff- and mergetool, adjust the git-config - usually located at ~/.config/git/config - as follows.

[alias]
  gdiff=difftool --dir-diff -y -t intellij
  resolve=mergetool -t intellij --prompt

[diff]
  tool = intellij
[difftool "intellij"]
  cmd = /Applications/IntelliJ*.app/Contents/MacOS/idea diff $(realpath "$LOCAL") $(realpath "$REMOTE") 2>/dev/null

[merge]
  tool = intellij
[mergetool]
  keepBackup = false
[mergetool "intellij"]
  cmd = /Applications/IntelliJ*.app/Contents/MacOS/idea merge $(realpath "$LOCAL") $(realpath "$REMOTE") $(realpath "$BASE") $(realpath "$MERGED") 2>/dev/null
  trustExitCode = true

Usage

For the IntelliJ-based diff, use git gdiff as base-command. Specific files, additional parameters etc. can be added, just as with the regular diff-command.

Conflict-resolution is performed with the alias git resolve.

💡

Due to the "prompt"-parameter in the resolve alias, you have to hit enter on the command-line before the merge of each file. If that’s not desired, just remove the --prompt parameter.

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