Git Reference ยท Plumbing Commands
git merge-file command
Reference for git merge-file: merge two changed versions of a file against their common ancestor, use -p, --diff3 and --ours, and read the exit status.
git merge-file performs a three-way merge on three plain files: your version (current), the common ancestor (base) and the other version (other). By default it writes the result back into the first file. Where both sides changed the same lines it inserts conflict markers, just as git merge would.
It does not need a repository; it works on any three files. Use it to merge configuration files, to reproduce what Git does during a merge, or to resolve a conflicted index entry by hand after extracting stages with git checkout-index --stage=all.
git merge-file ours.txt base.txt theirs.txt
git merge-file -p ours.txt base.txt theirs.txt
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git merge-file [-L <current> [-L <base> [-L <other>]]] [--ours | --theirs | --union] [-p] [-q] [--marker-size=<n>] [--diff3 | --zdiff3] <current-file> <base-file> <other-file>
| Name | Description |
|---|---|
<current-file> |
Your version. Overwritten with the result unless -p is given. |
<base-file> |
The common ancestor. |
<other-file> |
The version to merge in. |
-p, --stdout |
Print the result to standard output and leave the files alone. |
-L <label> |
Label for the conflict markers; up to three, in the order current, base, other. |
--ours, --theirs, --union |
Resolve conflicts automatically: take the current side, the other side, or both. |
--diff3, --zdiff3 |
Include the base text in conflict blocks (zdiff3 trims common lines). |
--marker-size=<n> |
Length of the marker lines (default 7). |
-q, --quiet |
Do not warn about conflicts. |
--object-id |
Treat the three arguments as blob IDs and print the ID of the merged blob (stored in the object database). |
Returns: the exit status is the number of conflicts (up to 127), or 0 for a clean merge; a negative result (exit 255) means an error occurred.
Errors: an unreadable file, or a binary file, gives an error and a non-zero status. Git refuses to merge binary files.
Usage notes
- The order matters. The base is the middle argument: current, base, other. Swapping them gives a different (usually conflicting) result.
- Overwrites by default. Without
-pthe first file is replaced. Copy it first, or use-p, when you want to keep it. - Exit status as a counter.
if git merge-file -p ...; thenis true only for a conflict-free merge. A script can also capture the exact count. --oursand--theirsonly decide the conflicting hunks; non-conflicting changes from both sides are still merged. That differs fromgit merge -s ours, which ignores the other side completely.- Whitespace and line endings: the merge is line based. Files with mixed line endings may produce spurious conflicts.
- No Git repository required, though repository settings are used when you run it inside one.