How to Use Merge Tool in Git: Simple Guide
Use the
git mergetool command to open a visual merge tool that helps you resolve conflicts during a merge. Configure your preferred merge tool with git config --global merge.tool [toolname], then run git mergetool after a merge conflict to fix issues interactively.Syntax
The basic syntax to use the Git merge tool is:
git mergetool: Launches the configured merge tool to resolve conflicts.git config --global merge.tool [toolname]: Sets your preferred merge tool globally.git mergetool --tool-help: Lists available merge tools on your system.
bash
git config --global merge.tool meld git mergetool
Example
This example shows how to set up the meld merge tool and use it to resolve conflicts after a merge.
bash
git config --global merge.tool meld # Assume you have a merge conflict after git merge branch-name git mergetool
Output
Merging:
file.txt
Launching merge tool...
# Meld window opens showing conflicting changes for you to resolve
Common Pitfalls
Common mistakes when using git mergetool include:
- Not configuring a merge tool before running
git mergetool, which causes Git to ask for a tool or fail. - Running
git mergetoolwithout conflicts, which does nothing. - Forgetting to save and close the merge tool window, so Git thinks the conflict is unresolved.
Always ensure you save your changes in the merge tool and then close it to let Git continue.
bash
git mergetool # Wrong: No conflicts, so no tool opens # Correct: After a conflict # Resolve conflicts in the tool, save, and close it
Quick Reference
| Command | Description |
|---|---|
| git config --global merge.tool [toolname] | Set preferred merge tool globally |
| git mergetool | Launch merge tool to resolve conflicts |
| git mergetool --tool-help | List available merge tools |
| git merge [branch] | Merge branch and may cause conflicts |
| git status | Check files with conflicts |
Key Takeaways
Configure your preferred merge tool before using git mergetool for smooth conflict resolution.
Run git mergetool only when there are merge conflicts to resolve.
Save and close the merge tool window to mark conflicts as resolved.
Use git mergetool --tool-help to see available merge tools on your system.
Common merge tools include meld, kdiff3, vimdiff, and beyond compare.