How do I make git automatically open the mergetool if there is a merge conflict?

git, merge, mergetool

Solution

You cannot (yet) make git do this.

This may or may not be an acceptable workaround.

Create a function in your `~/.bashrc`:

git() 
{ 
  if [[ $1 == "merge" ]] || [[ $1 == "rebase" ]] || [[ $1 == "pull" ]]; then 
    command git "$@" 
    rc=$?
    if [[ $rc == 1 ]]; then
      echo "There are conflicts, better run git-mergetool!!!"
      # There might be some other condition that returns a '1',
      # if so you can add another check like this:
      # if grep Conflicts $(git --git-dir)/MERGE_MSG;
      command git mergetool
    fi
  else 
    command git "$@"
  fi
}

Mergetool isn't invoked when it merges:

$ git merge non_conflicting_branch
Merge made by the 'recursive' strategy.
 bar | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 bar

Mergetool is called when there are conflicts:

$ git merge conflicting_branch
Auto-merging foo
CONFLICT (content): Merge conflict in foo
Automatic merge failed; fix conflicts and then commit the result.
There are Conflicts, better run git-mergetool!!!

Mergetool is not called on other errors:

$ git merge adasds
fatal: adasds - not something we can merge

Problem

How do I make git automatically run `git mergetool` for any merge conflict? This should apply for all merges, using `merge`, `rebase`, `pull`, etc.

Original source