mergetool Git Command Guide
The git mergetool command runs one of several merge utilities to resolve merge conflicts interactively. It is typically run after git merge when conflicts occur, providing a visual interface for comparing and resolving differences between conflicting file versions.
git mergetool Syntax:
Section titled “git mergetool Syntax:”git mergetool [--tool=<tool>] [-y | --[no-]prompt] [<file>...]Tool Selection Options:
Section titled “Tool Selection Options:”| Option | Description |
|---|---|
-t <tool>, --tool=<tool> | Use specified merge resolution program |
--tool-help | Show available merge tools |
User Interaction Options:
Section titled “User Interaction Options:”| Option | Description |
|---|---|
-y, --no-prompt | Don’t prompt before each file |
--prompt | Prompt before each file (default) |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<file>... | Specific files to resolve conflicts in |
Supported Merge Tools:
Section titled “Supported Merge Tools:”Built-in Tools:
Section titled “Built-in Tools:”| Tool | Description | Platform |
|---|---|---|
vimdiff | Vim with diff highlighting | Unix/Linux |
vimdiff2 | Vim with 2-way diff | Unix/Linux |
vimdiff3 | Vim with 3-way diff | Unix/Linux |
gvimdiff | GUI Vim diff | Unix/Linux with GUI |
gvimdiff2 | GUI Vim 2-way diff | Unix/Linux with GUI |
gvimdiff3 | GUI Vim 3-way diff | Unix/Linux with GUI |
External Tools:
Section titled “External Tools:”| Tool | Description | Platform |
|---|---|---|
kdiff3 | Advanced 3-way merge tool | Cross-platform |
meld | Visual diff and merge tool | Cross-platform |
emerge | Emacs merge tool | Unix/Linux |
tortoisemerge | TortoiseSVN merge tool | Windows |
diffuse | Simple diff viewer | Cross-platform |
ecmerge | ECMerge tool | Cross-platform |
p4merge | Perforce merge tool | Cross-platform |
araxis | Araxis merge tool | Windows/macOS |
bc | Beyond Compare | Windows |
codecompare | Devart Code Compare | Windows |
deltawalker | DeltaWalker | macOS |
winmerge | WinMerge | Windows |
xxdiff | Visual diff tool | Unix/Linux |
Basic Usage Examples:
Section titled “Basic Usage Examples:”Launch Mergetool for All Conflicts:
Section titled “Launch Mergetool for All Conflicts:”# Launch mergetool for all conflicted filesgit mergetool
# Don't prompt before each filegit mergetool -y
# Use specific merge toolgit mergetool --tool=kdiff3
# Show available toolsgit mergetool --tool-helpProcess Specific Files:
Section titled “Process Specific Files:”# Resolve conflicts in specific filesgit mergetool conflicted-file.txt another-conflict.c
# Process all conflicts in directorygit mergetool src/
# Process files matching patterngit mergetool *.jsTool Configuration:
Section titled “Tool Configuration:”# Configure default merge toolgit config merge.tool vimdiff
# Configure tool with custom pathgit config mergetool.kdiff3.path /usr/local/bin/kdiff3
# Configure tool optionsgit config mergetool.meld.cmd "meld \$LOCAL \$REMOTE \$BASE \$MERGED"Advanced Mergetool Configuration:
Section titled “Advanced Mergetool Configuration:”Custom Tool Setup:
Section titled “Custom Tool Setup:”# Configure VS Code as merge toolgit config merge.tool vscodegit config mergetool.vscode.cmd "code --wait --merge \$REMOTE \$LOCAL \$BASE \$MERGED"
# Configure IntelliJ IDEAgit config merge.tool intellijgit config mergetool.intellij.cmd "/Applications/IntelliJ\ IDEA.app/Contents/bin/idea merge \$LOCAL \$REMOTE \$BASE \$MERGED"
# Configure Sublime Textgit config merge.tool sublimemergegit config mergetool.sublimemerge.cmd "subl --wait --new-window \$MERGED"git config mergetool.sublimemerge.trustExitCode truePlatform-Specific Configuration:
Section titled “Platform-Specific Configuration:”# Windows configurationgit config merge.tool winmergegit config mergetool.winmerge.path "C:/Program Files/WinMerge/WinMergeU.exe"git config mergetool.winmerge.cmd "\"C:/Program Files/WinMerge/WinMergeU.exe\" -u -e \$MERGED"git config mergetool.winmerge.trustExitCode true
# macOS configurationgit config merge.tool diffmergegit config mergetool.diffmerge.path "/Applications/DiffMerge.app/Contents/MacOS/DiffMerge"git config mergetool.diffmerge.cmd "/Applications/DiffMerge.app/Contents/MacOS/DiffMerge --merge --result=\$MERGED \$LOCAL \$BASE \$REMOTE"Advanced Tool Configuration:
Section titled “Advanced Tool Configuration:”# Configure tool with argumentsgit config merge.tool kdiff3git config mergetool.kdiff3.path /usr/bin/kdiff3git config mergetool.kdiff3.cmd "kdiff3 --auto --L1 \"\$MERGED (Base)\" --L2 \"\$MERGED (Local)\" --L3 \"\$MERGED (Remote)\" -o \$MERGED \$BASE \$LOCAL \$REMOTE"git config mergetool.kdiff3.trustExitCode false
# Configure Meld with custom optionsgit config merge.tool meldgit config mergetool.meld.cmd "meld --diff \$LOCAL \$BASE \$REMOTE --output \$MERGED"git config mergetool.meld.trustExitCode trueIntegration with Development Workflows:
Section titled “Integration with Development Workflows:”Standard Conflict Resolution Workflow:
Section titled “Standard Conflict Resolution Workflow:”#!/bin/bash# Complete conflict resolution workflow
resolve_merge_conflicts() { echo "=== Merge Conflict Resolution ==="
# Check for conflicts if ! git ls-files -u | grep -q .; then echo "No merge conflicts detected" return 0 fi
echo "Found $(git ls-files -u | wc -l) conflicted entries"
# Launch mergetool for all conflicts if git mergetool -y; then echo "✓ All conflicts resolved"
# Check if all conflicts are resolved if git ls-files -u | grep -q .; then echo "✗ Some conflicts remain - manual resolution needed" return 1 else echo "✓ All conflicts resolved successfully" return 0 fi else echo "✗ Mergetool failed or was cancelled" return 1 fi}
resolve_merge_conflictsInteractive Resolution with Backup:
Section titled “Interactive Resolution with Backup:”# Interactive conflict resolution with safetyinteractive_merge_resolution() { local backup_branch="backup-before-resolution-$(date +%Y%m%d-%H%M%S)"
# Create backup git branch "$backup_branch" echo "Backup created: $backup_branch"
# Show conflicted files echo "Conflicted files:" git ls-files -u | awk '{print $4}' | sort | uniq
# Launch mergetool interactively git mergetool
# Verify resolution if git ls-files -u | grep -q .; then echo "Conflicts remain. Options:" echo "1. Continue with git mergetool" echo "2. Edit files manually and run git add" echo "3. Abort with git merge --abort" echo "4. Restore from backup: git reset --hard $backup_branch" return 1 else echo "✓ All conflicts resolved" echo "Clean up backup branch: git branch -D $backup_branch" return 0 fi}
interactive_merge_resolutionBatch Conflict Processing:
Section titled “Batch Conflict Processing:”# Process conflicts in organized batchesbatch_conflict_resolution() { local batch_size="${1:-5}"
# Get list of conflicted files conflicted_files=$(git ls-files -u | awk '{print $4}' | sort | uniq)
if [ -z "$conflicted_files" ]; then echo "No conflicts to resolve" return 0 fi
echo "Processing $(echo "$conflicted_files" | wc -l) conflicted files in batches of $batch_size"
# Process in batches echo "$conflicted_files" | \ xargs -n "$batch_size" | \ while read batch; do echo "=== Processing batch: $batch ==="
for file in $batch; do echo "Resolving: $file" git mergetool "$file"
if [ $? -eq 0 ]; then echo "✓ $file resolved" else echo "✗ $file needs manual attention" fi done
echo "--- Batch complete ---" done
# Final status check remaining=$(git ls-files -u | wc -l) if [ "$remaining" -eq 0 ]; then echo "✓ All conflicts resolved" else echo "✗ $remaining conflicts remain" fi}
batch_conflict_resolution 3Tool-Specific Configuration:
Section titled “Tool-Specific Configuration:”Vimdiff Configuration:
Section titled “Vimdiff Configuration:”# Configure vimdiff for mergegit config merge.tool vimdiffgit config mergetool.vimdiff.path vimgit config mergetool.vimdiff.cmd "vim -f -d -c 'wincmd J' \$MERGED \$LOCAL \$BASE \$REMOTE"
# Advanced vimdiff setupgit config mergetool.vimdiff.layout "LOCAL,REMOTE"git config mergetool.vimdiff.trustExitCode trueKDiff3 Configuration:
Section titled “KDiff3 Configuration:”# Configure KDiff3 for advanced merginggit config merge.tool kdiff3git config mergetool.kdiff3.path kdiff3git config mergetool.kdiff3.cmd "kdiff3 --auto --L1 \"\$MERGED (Base)\" --L2 \"\$MERGED (Local)\" --L3 \"\$MERGED (Remote)\" -o \$MERGED \$BASE \$LOCAL \$REMOTE"git config mergetool.kdiff3.trustExitCode false
# Enable auto-save and backupgit config mergetool.kdiff3.keepBackup falsegit config mergetool.kdiff3.keepTemporaries falseMeld Configuration:
Section titled “Meld Configuration:”# Configure Meld for intuitive merginggit config merge.tool meldgit config mergetool.meld.path meldgit config mergetool.meld.cmd "meld \$LOCAL \$BASE \$REMOTE --output \$MERGED"git config mergetool.meld.trustExitCode true
# Custom Meld layoutgit config mergetool.meld.layout "LOCAL,BASE,REMOTE"Troubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Tool Not Found:
Section titled “Tool Not Found:”# Check if tool is installedwhich kdiff3 meld vimdiff
# Check tool configurationgit config --list | grep mergetool
# Test tool manuallykdiff3 --versionmeld --versionTool Launch Failures:
Section titled “Tool Launch Failures:”# Check tool permissionsls -la /usr/bin/kdiff3ls -la /usr/bin/meld
# Test tool with sample filesecho "test" > test1.txtecho "test2" > test2.txtkdiff3 test1.txt test2.txt
# Check display environmentecho $DISPLAY # For GUI toolsFile Permission Issues:
Section titled “File Permission Issues:”# Check file permissionsgit ls-files --stage conflicted-file.txt
# Fix permissionschmod 644 conflicted-file.txt
# Handle read-only fileschmod u+w conflicted-file.txtLarge File Issues:
Section titled “Large File Issues:”# Check file sizesdu -h conflicted-file.txt
# Configure tool for large filesgit config mergetool.largefiletool vimdiff
# Split large files if neededsplit -l 1000 large-file.txt part-git mergetool part-aacat part-a* > resolved-large-file.txtReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Team Development Workflow:
Section titled “Team Development Workflow:”#!/bin/bash# Team conflict resolution workflow
team_conflict_resolution() { local feature_branch="$1" local target_branch="${2:-main}"
echo "=== Team Conflict Resolution ===" echo "Feature: $feature_branch" echo "Target: $target_branch"
# Ensure we're on target branch git checkout "$target_branch" git pull origin "$target_branch"
# Start merge if git merge "$feature_branch"; then echo "✓ Clean merge - no conflicts" return 0 fi
echo "Conflicts detected - launching resolution tools"
# Show conflicted files echo "Conflicted files:" git ls-files -u | awk '{print $4}' | sort | uniq
# Launch mergetool for all conflicts if git mergetool -y; then echo "✓ All conflicts resolved"
# Verify resolution if git ls-files -u | grep -q .; then echo "✗ Some conflicts remain" return 1 else # Complete merge git commit -m "Merge $feature_branch with conflict resolution" git push origin "$target_branch" echo "✓ Merge completed and pushed" fi else echo "✗ Mergetool failed" echo "Manual resolution needed" return 1 fi}
# Usageteam_conflict_resolution "feature/user-authentication" "develop"Quality Assurance Integration:
Section titled “Quality Assurance Integration:”# QA merge validation with mergetoolqa_merge_validation() { local merge_commit="$1"
echo "=== QA Merge Validation ==="
# Check for unresolved conflicts if git ls-files -u | grep -q .; then echo "✗ Merge has unresolved conflicts" echo "Run: git mergetool" return 1 fi
# Validate merge quality echo "Validating merge quality..."
# Check for conflict markers in committed files if git show "$merge_commit" | grep -q "^<<<<<<<\|^=======\|^>>>>>>>"; then echo "✗ Found conflict markers in merge commit" return 1 fi
# Validate file integrity git show "$merge_commit" --name-only | \ while read file; do if [ -f "$file" ]; then # Check for syntax errors in code files case "${file##*.}" in py) python -m py_compile "$file" 2>/dev/null && echo "✓ $file syntax OK" ;; js) node -c "$file" 2>/dev/null && echo "✓ $file syntax OK" ;; *) echo "✓ $file present" ;; esac fi done
echo "✓ Merge validation passed" return 0}
# Use in QA processqa_merge_validation "HEAD"Enterprise Conflict Management:
Section titled “Enterprise Conflict Management:”#!/bin/bash# Enterprise conflict resolution with tracking
enterprise_conflict_resolution() { local ticket_id="$1" local resolution_strategy="${2:-interactive}"
echo "=== Enterprise Conflict Resolution ===" echo "Ticket: $ticket_id" echo "Strategy: $resolution_strategy" echo "Timestamp: $(date)"
# Pre-resolution validation conflicted_files=$(git ls-files -u | awk '{print $4}' | sort | uniq) if [ -z "$conflicted_files" ]; then echo "No conflicts detected" return 0 fi
echo "Found $(echo "$conflicted_files" | wc -l) conflicted files"
# Create resolution record cat > "conflict-resolution-$ticket_id.log" << EOFConflict Resolution RecordTicket: $ticket_idDate: $(date)Strategy: $resolution_strategyUser: $(git config user.name) <$(git config user.email)>
Conflicted Files:$(echo "$conflicted_files" | sed 's/^/ /')
EOF
# Resolution strategy selection case "$resolution_strategy" in "automatic") echo "Attempting automatic resolution..." if git merge-index git-merge-one-file -a; then echo "✓ Automatic resolution successful" else echo "✗ Automatic resolution failed - falling back to interactive" resolution_strategy="interactive" fi ;; "interactive") echo "Launching interactive resolution..." git mergetool -y ;; "guided") echo "Launching guided resolution..." git mergetool # With prompts ;; esac
# Post-resolution validation remaining_conflicts=$(git ls-files -u | wc -l) if [ "$remaining_conflicts" -eq 0 ]; then echo "✓ All conflicts resolved"
# Update resolution record cat >> "conflict-resolution-$ticket_id.log" << EOF
Resolution Summary:Status: SUCCESSResolved files: $(echo "$conflicted_files" | wc -l)Strategy used: $resolution_strategyResolution time: $(($(date +%s) - $(date -r "conflict-resolution-$ticket_id.log" +%s))) seconds
EOF
echo "Resolution record: conflict-resolution-$ticket_id.log" return 0 else echo "✗ $remaining_conflicts conflicts remain"
# Update resolution record with failure cat >> "conflict-resolution-$ticket_id.log" << EOF
Resolution Summary:Status: PARTIALRemaining conflicts: $remaining_conflictsManual intervention required
EOF
echo "Resolution record: conflict-resolution-$ticket_id.log" return 1 fi}
# Usageenterprise_conflict_resolution "PROJ-1234" "interactive"What’s the difference between git mergetool and git merge —no-commit?
Section titled “What’s the difference between git mergetool and git merge —no-commit?”git mergetool resolves conflicts interactively after merge fails; git merge —no-commit stops before committing to allow manual conflict resolution.
How do I configure custom merge tools?
Section titled “How do I configure custom merge tools?”Use git config merge.tool
Can mergetool work with binary files?
Section titled “Can mergetool work with binary files?”Most visual merge tools are designed for text files. For binary files, use git checkout —ours or git checkout —theirs to choose one version.
What’s the relationship between mergetool and merge drivers?
Section titled “What’s the relationship between mergetool and merge drivers?”Merge drivers handle automatic merging; mergetool provides interactive resolution when automatic merging fails or conflicts occur.
How do I handle mergetool in headless environments?
Section titled “How do I handle mergetool in headless environments?”Use text-based tools like vimdiff, or configure tools that don’t require GUI. Some tools support —no-gui options.
Can mergetool work with Git LFS files?
Section titled “Can mergetool work with Git LFS files?”Yes, works normally with LFS pointer files. LFS handles the large file content separately from the Git merge process.
What’s the performance impact of mergetool?
Section titled “What’s the performance impact of mergetool?”Depends on tool and file size. Visual tools may be slower for very large files. Use —no-prompt for batch processing.
How do I handle mergetool timeouts?
Section titled “How do I handle mergetool timeouts?”Configure tool timeouts in git config or use external timeout commands. Some tools have built-in timeout settings.
Can mergetool work in bare repositories?
Section titled “Can mergetool work in bare repositories?”No, requires working directory with files. Use in non-bare repositories or create temporary working directory for bare repo merges.
What’s the difference between vimdiff and vimdiff3?
Section titled “What’s the difference between vimdiff and vimdiff3?”vimdiff shows 2-way diff; vimdiff3 shows 3-way diff with base, local, and remote versions for better conflict understanding.
How do I integrate mergetool with IDEs?
Section titled “How do I integrate mergetool with IDEs?”Most IDEs provide Git integration with mergetool support. Configure IDE as merge tool or use IDE’s built-in merge resolution.
Can mergetool handle multiple files simultaneously?
Section titled “Can mergetool handle multiple files simultaneously?”Most tools handle one file at a time. Use git mergetool -y for batch processing or process files individually.
What’s the relationship between mergetool and git rerere?
Section titled “What’s the relationship between mergetool and git rerere?”git rerere records conflict resolutions for reuse; mergetool provides the interface for initial resolution that rerere can learn from.
How do I handle mergetool in CI/CD pipelines?
Section titled “How do I handle mergetool in CI/CD pipelines?”Use text-based tools or automated resolution. Interactive tools don’t work in headless CI environments.
Can mergetool work with custom merge strategies?
Section titled “Can mergetool work with custom merge strategies?”Yes, mergetool works with any merge strategy. Configure appropriate tools for different file types and merge scenarios.
Applications of the git mergetool command
Section titled “Applications of the git mergetool command”- Interactive Conflict Resolution: Provide visual interface for resolving merge conflicts with three-way diff visualization
- Team Collaboration: Enable team members to resolve conflicts using familiar tools and interfaces
- Code Review Integration: Support conflict resolution during code review and merge request processes
- Development Workflow Enhancement: Integrate conflict resolution into development environments and IDEs
- Quality Assurance: Validate merge quality and ensure proper conflict resolution in QA processes
- Enterprise Merge Management: Support complex merge requirements with tracked and audited conflict resolution