Git Mergetool (with examples)
Code Example 1: Launch the default merge tool to resolve conflicts
git mergetool
Motivation: The git mergetool
command is used to launch the default merge tool provided by Git in order to resolve merge conflicts. Merge conflicts occur when there are conflicting changes in different branches that cannot be automatically merged by Git. In such cases, a visual merge tool can help in resolving the conflicts manually.
Explanation: This command runs the default merge tool configured in Git to resolve conflicts. The merge tool can be a command-line tool or a graphical tool, depending on the configuration.
Example Output:
Merging:
file.txt
Normal merge conflict for 'file.txt':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (code conflicts):
Code Example 2: List valid merge tools
git mergetool --tool-help
Motivation: Git allows you to configure different merge tools in order to handle merge conflicts according to your preference. This command helps you to list all the valid merge tools available on your system.
Explanation: The --tool-help
option provides a list of valid merge tools that can be used with git mergetool
command. It prints out the name of each merge tool along with a brief description.
Example Output:
git mergetool --tool=<tool> may be set to one of the following:
vimdiff
vimdiff2
vimdiff3
meld
tortoisemerge
gvimdiff
gvimdiff2
gvimdiff3
opendiff
diffmerge
kdiff3
tkdiff
xxdiff
emerge
vimdiff2
vimdiff3
araxis
kaleidoscope
ecmerge
p4merge
bc3
codecompare
sublime-merge
Code Example 3: Launch the merge tool identified by a name
git mergetool --tool <tool_name>
Motivation: This command is used to launch a specific merge tool identified by its name. It can be useful when you want to use a different merge tool than the default one configured in Git.
Explanation: The --tool
option allows you to specify the name of the merge tool that you want to use. The name should be one of the valid merge tools listed by the --tool-help
option.
Example Output:
$ git mergetool --tool=meld
Merging:
file.txt
Normal merge conflict for 'file.txt':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (meld):
Code Example 4: Don’t prompt before each invocation of the merge tool
git mergetool --no-prompt
Motivation: By default, Git prompts before each invocation of the merge tool to resolve conflicts. However, in some cases, you may want to disable this prompt and allow the merge tool to resolve conflicts automatically without manual intervention.
Explanation: The --no-prompt
option allows you to run the merge tool without being prompted for each merge conflict. The tool will be invoked automatically, and the conflicts will be resolved according to the merge tool’s behavior.
Example Output:
Merging:
file.txt
Normal merge conflict for 'file.txt':
{local}: modified
{remote}: modified
Starting merge tool to resolve conflicts...
Merge tool automatically resolved the conflict for 'file.txt'.
Code Example 5: Explicitly use the GUI merge tool
git mergetool --gui
Motivation: Git allows you to use GUI-based merge tools for resolving conflicts, which can provide a more intuitive and visual interface for resolving conflicts. This command is used to explicitly use the GUI merge tool configured in Git.
Explanation: The --gui
option is used to explicitly use the GUI merge tool configured in Git. It reads the merge.guitool
configuration variable to determine the GUI merge tool to be used.
Example Output:
Merging:
file.txt
Normal merge conflict for 'file.txt':
{local}: modified
{remote}: modified
Starting GUI merge tool to resolve conflicts...
Code Example 6: Explicitly use the regular merge tool
git mergetool --no-gui
Motivation: In contrast to the previous example, this command is used to explicitly use the regular merge tool configured in Git. The regular merge tool can be a command-line tool or a non-GUI-based tool.
Explanation: The --no-gui
option is used to explicitly use the regular merge tool configured in Git. It reads the merge.tool
configuration variable to determine the regular merge tool to be used.
Example Output:
Merging:
file.txt
Normal merge conflict for 'file.txt':
{local}: modified
{remote}: modified
Starting regular merge tool to resolve conflicts...