Configuring TortoiseGit as Git Mergetool | Generated by AI
To configure TortoiseGit as the merge tool for Git, you need to set up TortoiseGitMerge (the merge tool included with TortoiseGit) in your Git configuration. Below are the steps to achieve this:
Prerequisites
- Ensure TortoiseGit is installed on your system. You can download it from tortoisegit.org.
- Verify that the TortoiseGitMerge executable is available (typically located at
C:\Program Files\TortoiseGit\bin\TortoiseGitMerge.exe
).
Steps to Configure TortoiseGitMerge as Git Mergetool
- Open a Command Prompt or Git Bash
- You can use the Windows Command Prompt, PowerShell, or Git Bash to run the necessary Git configuration commands.
-
Set TortoiseGitMerge as the Merge Tool Run the following commands to configure Git to use TortoiseGitMerge:
git config --global merge.tool tortoisegitmerge git config --global mergetool.tortoisemerge.cmd "\"C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe\" -base:\"$BASE\" -theirs:\"$REMOTE\" -mine:\"$LOCAL\" -merged:\"$MERGED\""
Explanation:
merge.tool tortoisegitmerge
: Sets the merge tool name totortoisegitmerge
(you can choose any name, but this is a convention).mergetool.tortoisemerge.cmd
: Specifies the command to run TortoiseGitMerge with the appropriate parameters:-base:"$BASE"
: The common ancestor file.-theirs:"$REMOTE"
: The file from the branch being merged.-mine:"$LOCAL"
: The file from your current branch.-merged:"$MERGED"
: The output file where the resolved merge will be saved.
- Use forward slashes (
/
) in the path and escape quotes as needed, especially if the path contains spaces.
Note: Adjust the path (
C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe
) if TortoiseGit is installed in a different location (e.g.,E:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe
). -
Optional: Disable Mergetool Prompt To avoid being prompted each time you run
git mergetool
, you can disable the prompt:git config --global mergetool.prompt false
- Optional: Ensure TortoiseGitMerge is in the System PATH
If Git cannot find TortoiseGitMerge, ensure its directory is in your system’s PATH environment variable:
- Right-click on “This PC” or “My Computer” → Properties → Advanced system settings → Environment Variables.
- Under “System Variables,” find and edit the
Path
variable to includeC:\Program Files\TortoiseGit\bin
. -
Alternatively, explicitly set the path in the Git configuration:
git config --global mergetool.tortoisemerge.path "C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe"
- Test the Configuration
- Create a merge conflict in a Git repository (e.g., by merging two branches with conflicting changes).
-
Run the following command to launch the merge tool:
git mergetool
- TortoiseGitMerge should open, displaying a three-pane view with the base, theirs, and mine versions of the conflicting file. The bottom pane is the merged result.
- Resolve Conflicts in TortoiseGitMerge
- In the three-pane view, TortoiseGitMerge shows:
- Left pane: The “theirs” version (from the branch being merged).
- Right pane: The “mine” version (from your current branch).
- Middle pane: The base (common ancestor) version.
- Bottom pane: The merged result where you resolve conflicts.
- Right-click on conflicting sections to choose options like “Use text block from ‘theirs’,” “Use text block from ‘mine’,” or manually edit the merged file.
- Once resolved, save the file (File → Save) and close TortoiseGitMerge.
- Git will mark the file as resolved if TortoiseGitMerge exits successfully (exit code 0). If prompted, confirm to mark the conflict as resolved.
- In the three-pane view, TortoiseGitMerge shows:
-
Commit the Resolved Merge After resolving conflicts, commit the changes:
git commit
Note: If the conflict occurred during a rebase or cherry-pick, use the respective TortoiseGit dialogs (Rebase or Cherry-pick) to continue the process instead of the standard commit dialog.
Using TortoiseGitMerge via TortoiseGit GUI
If you prefer using the TortoiseGit GUI to resolve conflicts:
- Right-click on the conflicted file in Windows Explorer.
- Select TortoiseGit → Edit Conflicts.
- TortoiseGitMerge will open, allowing you to resolve conflicts as described above.
- After saving, right-click again and select TortoiseGit → Resolved to mark the file as resolved.
- Commit the changes using TortoiseGit’s Commit dialog.
Troubleshooting
- Error: “Unsupported merge tool ‘tortoisemerge’“
- Spaces in File Paths
- Cygwin Users
-
If using Cygwin, adjust the path to use Cygwin’s mount point, e.g.:
git config --global mergetool.tortoisemerge.cmd '"/cygdrive/c/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe" -base:"$BASE" -theirs:"$REMOTE" -mine:"$LOCAL" -merged:"$MERGED"'
-
- TortoiseGitMerge Not Found
Additional Notes
- TortoiseGitMerge supports a three-pane view for conflict resolution, which is ideal for merging. It automatically merges non-conflicting changes and highlights conflicts for manual resolution.
-
If you want to use TortoiseGitMerge as a diff tool as well, configure it similarly:
git config --global diff.tool tortoisediff git config --global difftool.tortoisediff.cmd "\"C:/Program Files/TortoiseGit/bin/TortoiseGitMerge.exe\" -mine:\"$REMOTE\" -base:\"$LOCAL\""
- For advanced settings, you can configure TortoiseGit to wait for the merge tool to close and automatically mark files as resolved based on the exit code. In TortoiseGit Settings → External Programs → Merge Tool, select “Block TortoiseGit while executing the external merge tool” and enable “Trust exit code.”
Sources
By following these steps, you should be able to use TortoiseGitMerge as your Git mergetool effectively, either from the command line or via the TortoiseGit GUI.