0
0
GitHow-ToBeginner · 3 min read

How to Set Merge Tool in Git: Simple Guide

To set a merge tool in Git, use the command git config --global merge.tool <toolname> to specify your preferred tool. You can also set the command Git uses to launch the tool with git config --global mergetool.<toolname>.cmd <command>.
📐

Syntax

The basic syntax to set a merge tool in Git is:

  • git config --global merge.tool <toolname>: Sets the default merge tool globally.
  • git config --global mergetool.<toolname>.cmd <command>: Defines the command to run the merge tool.
  • git mergetool: Runs the configured merge tool to resolve conflicts.
bash
git config --global merge.tool meld

git config --global mergetool.meld.cmd 'meld "$LOCAL" "$BASE" "$REMOTE" --output="$MERGED"'

git mergetool
💻

Example

This example sets meld as the merge tool and runs it to resolve conflicts.

bash
git config --global merge.tool meld
git config --global mergetool.meld.cmd 'meld "$LOCAL" "$BASE" "$REMOTE" --output="$MERGED"'
git mergetool
Output
Merging: file.txt Launching merge tool...
⚠️

Common Pitfalls

Common mistakes include:

  • Not installing the merge tool before setting it in Git.
  • Using incorrect command syntax for the tool's launch command.
  • Forgetting to quote file paths in the command, causing errors on spaces.
  • Setting the tool locally instead of globally when you want it for all repos.
bash
Wrong:
git config --global mergetool.meld.cmd meld $LOCAL $BASE $REMOTE --output=$MERGED

Right:
git config --global mergetool.meld.cmd 'meld "$LOCAL" "$BASE" "$REMOTE" --output="$MERGED"'
📊

Quick Reference

CommandDescription
git config --global merge.tool Set default merge tool globally
git config --global mergetool..cmd Set command to launch merge tool
git mergetoolRun the configured merge tool to resolve conflicts
git config --global merge.conflictstyle diff3Show base version in conflict markers (optional)

Key Takeaways

Use git config --global merge.tool <toolname> to set your preferred merge tool globally.
Define the exact command to launch the tool with git config --global mergetool.<toolname>.cmd <command> including proper quoting.
Run git mergetool to start the merge tool when conflicts occur.
Always install the merge tool software before configuring it in Git.
Check for correct quoting and syntax to avoid command errors.