“git mergetool” Command Examples

The git mergetool command is a useful tool in Git that helps you resolve merge conflicts that occur during the merging process. When Git encounters conflicting changes between branches during a merge, it can’t automatically resolve the conflicts. In such cases, the git mergetool command comes into play.

Here’s how you can use the git mergetool command:

  • Start by executing the git merge command, which triggers a merge and identifies conflicting changes between branches.
  • When Git encounters conflicts, it pauses the merge process and provides information about the conflicted files.
  • To resolve these conflicts, you can run the git mergetool command.

When you run git mergetool, Git opens an external merge tool that helps you visualize and resolve conflicts within the affected files. The specific merge tool opened depends on your Git configuration and the tools available on your system.

The merge tool presents you with a side-by-side view of the conflicting changes, typically with the base version (common ancestor), the current branch version, and the version from the branch being merged. The tool provides options for accepting or rejecting changes from either side, or manually editing the file to craft a custom resolution.

Once you’ve resolved the conflicts using the merge tool, save the changes and close the tool. Git will detect that the conflicts have been resolved for that particular file.

After resolving all conflicts in all affected files, you can finalize the merge by executing the git commit command. This creates a new merge commit that records the resolution of conflicts and completes the merge operation.

It’s worth noting that the specific merge tool you use can be configured in your Git configuration. By default, Git tries to use common merge tools such as vimdiff, meld, kdiff3, or opendiff, depending on your operating system and available tools. You can customize the merge tool configuration by modifying the Git configuration file.

git mergetool Command Examples

1. Launch the default merge tool to resolve conflicts:

# git mergetool

2. List valid merge tools:

# git mergetool --tool-help

3. Launch the merge tool identified by a name:

# git mergetool --tool tool_name

4. Don’t prompt before each invocation of the merge tool:

# git mergetool --no-prompt

5. Explicitly use the GUI merge tool (see the merge.guitool config variable):

# git mergetool --gui

6. Explicitly use the regular merge tool (see the merge.tool config variable):

# git mergetool --no-gui
Related Post