git-merge-file
Merge file with git-style conflict markers
SYNOPSIS
git merge-file [-L <name1> [-L <name2> [-L <name3>]]] [-p|--stdout] [-q|--quiet] [--diff3] [--ours|--theirs|--union] [--marker-size=n] <current-file> <base-file> <other-file>
PARAMETERS
-L
Label for files in conflict markers: first for current-file, second for base-file, third for other-file (repeatable up to 3 times).
-p, --stdout
Write merged result to stdout instead of overwriting current-file.
-q, --quiet
Suppress warnings about unresolved conflicts.
--diff3
Use longer diff3-style conflict markers including base-file content.
--ours
Resolve conflicts favoring current-file (discard other-file changes).
--theirs
Resolve conflicts favoring other-file (discard current-file changes).
--union
Resolve by taking all non-conflicting lines from both sides (no markers).
--marker-size=
Use <n> characters for conflict marker lines (default: 7).
DESCRIPTION
git-merge-file is Git's low-level utility for performing a three-way merge on text files. It combines changes from a current-file (your version), a base-file (common ancestor), and an other-file (incoming version from another branch). This mimics the per-file merge logic used by git merge and git rebase, but operates standalone for scripting or manual use.
The command detects differences, applies non-conflicting changes automatically, and inserts conflict markers (e.g., <<<<<<< HEAD) for manual resolution where hunks overlap. By default, it overwrites the current-file with the result. Use --stdout (-p) to output to console instead, ideal for previewing or piping.
Custom labels via -L improve marker readability, e.g., <<<<<<< local. Strategies like --ours (keep current), --theirs (keep other), or --union (non-conflicting lines only) override normal merging. --diff3 adds base context in conflicts for better resolution.
It's a plumbing command, perfect for tools integrating Git merges, patch workflows, or custom conflict handlers. Exits non-zero on unresolved conflicts or errors.
CAVEATS
Refuses binary files (exits with error). Modifies current-file in place by default—use --stdout to avoid. Unresolved conflicts leave markers and exit non-zero; always review output. Assumes text files with consistent line endings.
EXIT STATUS
0: success (no conflicts or resolved).
1: conflicts remain.
>1: errors (e.g., binary file, missing args).
EXAMPLE
git merge-file file.txt file.txt~base file.txt~other -L 'local' -L 'ancestor' -L 'remote'
Merges other into file.txt using base, with custom labels.
HISTORY
Introduced in Git 1.5.3 (Feb 2007) as a plumbing command for file-level merges, inspired by RCS merge. Evolved with Git's recursive merge strategies; widely used in scripts and IDEs for granular conflict handling.


