merge-one-file Git Command Guide
The git merge-one-file command is the standard helper program used with git merge-index to resolve merges after the trivial merge done with git read-tree -m. It handles the three-way merge of individual files during Git merge operations.
git merge-one-file Syntax:
Section titled “git merge-one-file Syntax:”git merge-one-file <filename>Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
<filename> | File to perform three-way merge on |
Understanding merge-one-file Operation:
Section titled “Understanding merge-one-file Operation:”Three-Way File Merge Process:
Section titled “Three-Way File Merge Process:”Input: Base file (common ancestor) Our file (current branch version) Their file (incoming branch version)
Output: Merged file with conflict markers if needed Exit code: 0=success, 1=conflictIntegration with merge-index:
Section titled “Integration with merge-index:”# merge-index calls merge-one-file for each conflicted filegit merge-index git-merge-one-file -a
# merge-one-file receives arguments from merge-index:# $1=base-sha, $2=our-sha, $3=their-sha, $4=filenameBasic Usage Examples:
Section titled “Basic Usage Examples:”Standard Merge Operation:
Section titled “Standard Merge Operation:”# Merge single file (called by merge-index)git merge-one-file conflicted-file.txt
# Check exit code for conflict detectionif git merge-one-file file.txt; then echo "File merged successfully"else echo "File has conflicts - manual resolution needed"fiManual Three-Way Merge:
Section titled “Manual Three-Way Merge:”# Manually perform three-way merge# Create base, ours, theirs versionsgit show :1:file.txt > base.txtgit show :2:file.txt > ours.txtgit show :3:file.txt > theirs.txt
# Perform mergegit merge-one-file file.txt
# Check resultcat file.txtBatch File Processing:
Section titled “Batch File Processing:”# Process multiple conflicted filesgit ls-files -u | awk '{print $4}' | sort | uniq | \while read file; do echo "Merging $file" if git merge-one-file "$file"; then echo "✓ $file merged successfully" git add "$file" else echo "✗ $file needs manual resolution" fidoneAdvanced merge-one-file Scenarios:
Section titled “Advanced merge-one-file Scenarios:”Custom Merge Logic Integration:
Section titled “Custom Merge Logic Integration:”#!/bin/bash# Enhanced merge-one-file with custom logic
enhanced_merge_one_file() { local filename="$1"
echo "Processing merge for $filename"
# Get file information local base_sha="$1" local our_sha="$2" local their_sha="$3" local file="$4"
# Custom merge logic based on file type case "${filename##*.}" in json|yml|yaml) # Use jq for JSON/YAML merging if command -v jq >/dev/null; then git cat-file -p "$our_sha" > .ours.tmp git cat-file -p "$their_sha" > .theirs.tmp jq -s '.[0] * .[1]' .ours.tmp .theirs.tmp > "$filename" 2>/dev/null if [ $? -eq 0 ]; then echo "✓ JSON/YAML merge successful" rm .ours.tmp .theirs.tmp return 0 fi fi ;; md|txt) # Use union strategy for documentation git merge-file --union "$filename" \ <(git cat-file -p "$base_sha" 2>/dev/null || echo "") \ <(git cat-file -p "$our_sha") \ <(git cat-file -p "$their_sha") return $? ;; esac
# Fall back to standard merge git merge-one-file "$filename"}
# Use with merge-indexgit merge-index enhanced_merge_one_file -aConflict Analysis and Reporting:
Section titled “Conflict Analysis and Reporting:”# Analyze merge resultsanalyze_merge_results() { local merge_log=$(mktemp)
# Create wrapper that logs results cat > "$merge_log" << 'EOF'#!/bin/bashfilename=$4echo "$(date): Processing $filename" >> merge-analysis.log
if git merge-one-file "$filename" 2>/dev/null; then echo "SUCCESS: $filename" >> merge-analysis.log echo "✓ $filename"else echo "CONFLICT: $filename" >> merge-analysis.log echo "✗ $filename" exit 1fiEOF
chmod +x "$merge_log"
# Run merge with logging if git merge-index "$merge_log" -a; then echo "All files merged successfully" echo "See merge-analysis.log for details" else echo "Some files need manual resolution" echo "Failed files logged in merge-analysis.log" fi
rm "$merge_log"}
analyze_merge_resultsPerformance Optimization:
Section titled “Performance Optimization:”# Optimize merge operations for large repositoriesoptimize_merge_performance() { # Disable auto-GC during merge git config gc.auto 0
# Set merge limits git config merge.renameLimit 999999 git config diff.renameLimit 999999
# Use parallel processing where possible # Note: merge-one-file is typically called per-file by merge-index}
# Batch processing for efficiencyprocess_merge_batch() { local batch_size="${1:-10}"
# Get list of conflicted files conflicted_files=$(git ls-files -u | awk '{print $4}' | sort | uniq)
echo "$conflicted_files" | \ xargs -n "$batch_size" | \ while read batch; do echo "Processing batch: $batch" for file in $batch; do git merge-one-file "$file" & done wait # Wait for batch to complete done}
optimize_merge_performanceprocess_merge_batch 5Integration with Git Workflows:
Section titled “Integration with Git Workflows:”Automated Merge Resolution:
Section titled “Automated Merge Resolution:”#!/bin/bash# Automated merge resolution with fallback
auto_resolve_merge() { local strategy="${1:-standard}"
case "$strategy" in "ours") # Always take our version git ls-files -u | awk '{print $4}' | sort | uniq | \ while read file; do git checkout --ours "$file" git add "$file" done ;; "theirs") # Always take their version git ls-files -u | awk '{print $4}' | sort | uniq | \ while read file; do git checkout --theirs "$file" git add "$file" done ;; "standard") # Use merge-one-file with custom logic git merge-index git-merge-one-file -a ;; "intelligent") # Use intelligent resolution based on file type git merge-index intelligent-merge-resolver -a ;; esac
# Check if any conflicts remain if git ls-files -u | grep -q .; then echo "Conflicts remain - manual resolution needed" return 1 else echo "All conflicts resolved" return 0 fi}
# Usageauto_resolve_merge "standard"CI/CD Pipeline Integration:
Section titled “CI/CD Pipeline Integration:”# Merge conflict handling in CIhandle_ci_merge_conflicts() { # Check if merge conflicts exist if git ls-files -u | grep -q .; then echo "Merge conflicts detected in CI"
# Attempt automatic resolution if auto_resolve_merge "intelligent"; then echo "Conflicts resolved automatically" git commit -m "Auto-resolve merge conflicts" else echo "Manual conflict resolution required"
# Report conflicts for manual intervention echo "Conflicted files:" git ls-files -u | awk '{print $4}' | sort | uniq
# Fail the build exit 1 fi else echo "No merge conflicts detected" fi}
# Use in CI pipelinehandle_ci_merge_conflictsDevelopment Environment Setup:
Section titled “Development Environment Setup:”# Configure merge-one-file for developmentsetup_merge_environment() { # Configure merge tool for interactive resolution git config merge.tool vscode git config mergetool.vscode.cmd "code --wait \$MERGED"
# Set up automatic merge for certain file types echo "*.json merge=json-merge" >> .gitattributes git config merge.json-merge.driver "jq -s '.[0] * .[1]' %O > %A"
# Configure rerere for learning from resolutions git config rerere.enabled true git config rerere.autoupdate true
echo "Merge environment configured" echo "Use 'git mergetool' for interactive conflict resolution" echo "Use 'git rerere status' to see learned resolutions"}
setup_merge_environmentTroubleshooting Common Issues:
Section titled “Troubleshooting Common Issues:”Merge-one-file Failures:
Section titled “Merge-one-file Failures:”# Debug merge-one-file operationGIT_TRACE=1 git merge-one-file conflicted-file.txt
# Check file permissionsls -la conflicted-file.txt
# Verify file contentgit show :1:conflicted-file.txt > base.txtgit show :2:conflicted-file.txt > ours.txtgit show :3:conflicted-file.txt > theirs.txt
# Manual merge testgit merge-file ours.txt base.txt theirs.txtIndex State Issues:
Section titled “Index State Issues:”# Check index stategit statusgit ls-files --stage conflicted-file.txt
# Reset problematic index entriesgit checkout --theirs conflicted-file.txtgit checkout --ours conflicted-file.txt
# Verify repository integritygit fsck --unreachable | head -10Permission Problems:
Section titled “Permission Problems:”# Fix file permissionschmod 644 conflicted-file.txt
# Handle read-only fileschmod u+w conflicted-file.txt
# Check filesystem issuesdf -h .ls -la .git/Large File Issues:
Section titled “Large File Issues:”# Check file sizesdu -h conflicted-file.txt
# Handle memory limitsulimit -v $((1024*1024*1024)) # 1GB limit
# Split large files if neededsplit -l 10000 large-file.txt part-git merge-one-file part-aa # Process first partcat part-a* > merged-large-file.txtReal-World Usage Examples:
Section titled “Real-World Usage Examples:”Enterprise Merge Automation:
Section titled “Enterprise Merge Automation:”#!/bin/bash# Enterprise-grade merge conflict resolution
enterprise_merge_resolution() { local ticket_id="$1" local merge_strategy="${2:-standard}"
echo "=== Enterprise Merge Resolution ===" echo "Ticket: $ticket_id" echo "Strategy: $merge_strategy" echo "Timestamp: $(date)"
# Pre-merge validation conflicted_count=$(git ls-files -u | wc -l) if [ "$conflicted_count" -eq 0 ]; then echo "No conflicts detected" return 0 fi
echo "Found $conflicted_count conflicted entries"
# Create backup backup_branch="backup-before-merge-$(date +%Y%m%d-%H%M%S)" git branch "$backup_branch"
# Resolution strategy selection case "$merge_strategy" in "ours") echo "Using 'ours' strategy" git ls-files -u | awk '{print $4}' | sort | uniq | \ while read file; do git checkout --ours "$file" git add "$file" done ;; "theirs") echo "Using 'theirs' strategy" git ls-files -u | awk '{print $4}' | sort | uniq | \ while read file; do git checkout --theirs "$file" git add "$file" done ;; "intelligent") echo "Using intelligent resolution" git merge-index intelligent-resolver -a ;; "standard") echo "Using standard merge-one-file" git merge-index git-merge-one-file -a ;; esac
# Verify resolution remaining_conflicts=$(git ls-files -u | wc -l) if [ "$remaining_conflicts" -eq 0 ]; then echo "✓ All conflicts resolved"
# Create merge record git notes add -m "Merge resolution: $ticket_id" HEAD git notes append -m "Strategy: $merge_strategy" HEAD git notes append -m "Resolved by: $(git config user.name)" HEAD git notes append -m "Backup branch: $backup_branch" HEAD
return 0 else echo "✗ $remaining_conflicts conflicts remain" echo "Manual resolution needed" echo "Backup available at: $backup_branch" return 1 fi}
# Usageenterprise_merge_resolution "PROJ-1234" "intelligent"Collaborative Development Workflow:
Section titled “Collaborative Development Workflow:”# Team merge conflict handlingteam_merge_workflow() { local feature_branch="$1" local target_branch="${2:-main}"
echo "=== Team Merge Workflow ===" echo "Feature: $feature_branch" echo "Target: $target_branch"
# Pre-merge analysis git checkout "$target_branch" git pull origin "$target_branch"
# Check for conflicts if git merge-tree "$target_branch" "$feature_branch" >/dev/null 2>&1; then echo "✓ Clean merge possible" git merge --no-ff "$feature_branch" else echo "⚠ Conflicts detected - using merge-one-file"
# Start merge git merge "$feature_branch"
# Resolve conflicts with merge-one-file if git merge-index git-merge-one-file -a; then echo "✓ All conflicts resolved automatically" git commit -m "Merge $feature_branch with auto-resolution" else echo "✗ Manual conflict resolution needed" echo "Use 'git mergetool' for interactive resolution" return 1 fi fi
# Push merged changes git push origin "$target_branch"}
# Usageteam_merge_workflow "feature/user-authentication" "develop"Quality Assurance Integration:
Section titled “Quality Assurance Integration:”# QA merge validationqa_merge_validation() { local merge_commit="$1"
echo "=== QA Merge Validation ==="
# Check if merge has conflicts if git ls-files -u | grep -q .; then echo "✗ Merge has unresolved conflicts" return 1 fi
# Validate merge quality echo "Validating merge quality..."
# Check for merge-one-file usage patterns git show "$merge_commit" --stat | grep -q "merge-one-file" && \ echo "✓ Used proper merge resolution tools"
# Check for conflict markers if git show "$merge_commit" | grep -q "^<<<<<<<\|^=======\|^>>>>>>>"; then echo "✗ Found conflict markers in merge" return 1 fi
# Validate file integrity git show "$merge_commit" --name-only | \ while read file; do if [ -f "$file" ]; then # Basic file validation file "$file" | grep -q "text" && echo "✓ $file is valid text" fi done
echo "✓ Merge validation passed" return 0}
# Use in QA processqa_merge_validation "HEAD"What’s the relationship between merge-one-file and git merge?
Section titled “What’s the relationship between merge-one-file and git merge?”git merge uses merge-index internally, which calls merge-one-file for individual file merges. merge-one-file is the low-level file merge utility.
How do I create custom merge logic for merge-one-file?
Section titled “How do I create custom merge logic for merge-one-file?”Write scripts that handle the three-way merge arguments passed by merge-index. Return 0 for success, non-zero for conflicts requiring manual resolution.
Can merge-one-file handle binary files?
Section titled “Can merge-one-file handle binary files?”Yes, but merge logic must handle binary content appropriately. Standard merge-one-file is text-oriented and may not work well with binary files.
What’s the difference between merge-one-file and git merge-file?
Section titled “What’s the difference between merge-one-file and git merge-file?”merge-one-file is called by merge-index for individual files during Git merges; git merge-file is a standalone three-way merge utility for manual file merging.
How do I debug merge-one-file operations?
Section titled “How do I debug merge-one-file operations?”Use GIT_TRACE=1 for detailed execution tracing. Test merge operations manually before using in merge-index workflows.
Can merge-one-file work with custom merge drivers?
Section titled “Can merge-one-file work with custom merge drivers?”Yes, merge-one-file can be replaced with custom merge programs in merge-index calls. Configure custom merge drivers in .gitattributes.
What’s the performance impact of merge-one-file?
Section titled “What’s the performance impact of merge-one-file?”Very fast for individual files. Performance scales with number of conflicted files and file sizes. Memory usage depends on file content.
How do I handle merge-one-file timeouts?
Section titled “How do I handle merge-one-file timeouts?”Use timeout command: timeout 300 git merge-index git-merge-one-file -a. Individual file merges may need per-file timeout handling.
Can merge-one-file work in bare repositories?
Section titled “Can merge-one-file work in bare repositories?”Yes, operates on Git objects regardless of working directory. Useful for server-side merge operations and CI/CD pipelines.
What’s the exit code behavior of merge-one-file?
Section titled “What’s the exit code behavior of merge-one-file?”Returns 0 for successful merge, non-zero when conflicts require manual resolution. merge-index uses this to determine merge success.
How do I integrate merge-one-file with external tools?
Section titled “How do I integrate merge-one-file with external tools?”Write wrapper scripts that call external merge tools and return appropriate exit codes. Use in merge-index for custom merge workflows.
Can merge-one-file handle Unicode and international characters?
Section titled “Can merge-one-file handle Unicode and international characters?”Yes, supports UTF-8 and other encodings. Ensure consistent encoding between all three file versions being merged.
What’s the relationship between merge-one-file and git rerere?
Section titled “What’s the relationship between merge-one-file and git rerere?”git rerere records and reuses conflict resolutions. merge-one-file can benefit from rerere when the same conflicts occur repeatedly.
How do I handle merge-one-file in automated environments?
Section titled “How do I handle merge-one-file in automated environments?”Test merge operations thoroughly, use appropriate exit code handling, implement fallback strategies for failed merges.
Can merge-one-file work with Git LFS files?
Section titled “Can merge-one-file work with Git LFS files?”Yes, works normally with LFS pointer files. LFS handles the large file content separately from the Git merge process.
Applications of the git merge-one-file command
Section titled “Applications of the git merge-one-file command”- Standard Merge Resolution: Serve as the default helper program for git merge-index during Git merge operations
- Custom Merge Logic: Enable specialized merge strategies for different file types and content
- Automated Conflict Resolution: Implement intelligent conflict resolution in CI/CD pipelines
- Enterprise Merge Workflows: Support complex merge requirements in corporate development environments
- Merge Quality Assurance: Validate merge operations and ensure consistent merge behavior
- Collaborative Development: Facilitate team merge processes with standardized conflict resolution