How to make git automatically open mergetool if there is a merge conflict?

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

+25
git merge mergetool
Apr 05 2018-12-12T00:
source share
3 answers

You cannot (yet) do git to do this.




This may or may not be an acceptable solution.

Create a function in ~/.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 is not called upon merge:

 $ 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 conflicts occur:

 $ 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 
+7
Jul 17 '13 at 7:35
source share

You can always use an alias

 alias 'git-merge'='git merge && git mergetool' alias 'git-rebase'='git rebase && git mergetool' alias 'git-pull'='git pull && git mergetool' 

And / or write a helper script along these lines

 #/bin/bash git $* [ "$(git ls-files –abbrev –unmerged | wc -l)" -gt 0 ] && git mergetool 

and then

 alias git='~/.git/git-script' 

There is no direct way to call mergetool, because it is only one of several ways to merge (see "HOW TO SOLVE CONFLICTS" in man 1 git-merge).

+3
Apr 05 2018-12-12T00:
source share

As far as I know, there is no china way to do this.

You may have a wrapper around git like this ( git_mergetool.sh file, in your path, +x ):

 #!/bin/bash SEARCH="CONFLICT" OUTPUT=$(git "$@" 2>&1 | tee /dev/tty) if `echo ${OUTPUT} | grep -i "${SEARCH}" 1>/dev/null 2>&1` then git mergetool fi 

Then add an alias:

 echo alias git=\"git_mergetool.sh\" >> ~/.bashrc 

Each time you call git, the wrapper checks to see if the word "CONFLICT" appears. If so, wrapper launches mergetool.

This can be improved by adding a more precise phrase to $SEARCH (for example, “Auto-merge failed, resolve conflicts and then fix the result”), and also check if the list contains the first argument ( $1 ) of the commands leading to the merge conflict ( pull , merge , etc.). Otherwise, you will finish parsing a lot of unnecessary data if the output of the git command is too long.

+2
Jul 12 '13 at 16:52
source share



All Articles