LinuxCommandLibrary

git-merge-one-file

Merge changes from another branch into one file

TLDR

Resolve a simple merge conflict for a file

$ git merge-one-file [path/to/file]
copy

Use as a helper in merge-index for a file
$ git merge-index git-merge-one-file [path/to/file]
copy

Handle a binary file merge
$ git merge-one-file -p [path/to/file]
copy

Apply after read-tree in a scripted merge
$ git read-tree -m [branch1] [branch2] && git merge-index git-merge-one-file [path/to/file]
copy

SYNOPSIS

git-merge-one-file [-d] [-p] [-q] <ancestor> <current> <remote>

PARAMETERS

-d
    Enable verbose debugging output

-p
    Produce patch-format output with conflict markers instead of merging

-q
    Quiet mode; suppress non-error messages

DESCRIPTION

git-merge-one-file is an internal Git plumbing command designed to perform a three-way merge on a single file during merge operations. It takes three file versions as input: the common ancestor, the current branch's version, and the other branch's version. The command attempts to merge changes automatically, resolving conflicts where possible by applying diff hunks or line-based changes.

It outputs the merged result either to stdout (with -p) in a patch format showing conflicts with markers, or directly updates the target file. This tool was historically invoked by git-merge-index when using the merge-one-file strategy, particularly useful in non-recursive merges or when merging trees via git-read-tree.

Primarily used in early Git workflows for low-level file resolution, it supports basic conflict detection using diff3-style output. However, it lacks advanced features like rename detection or content-aware merging found in modern strategies. For direct use, extract the files from Git objects first (e.g., via git cat-file), then pass their paths.

While still present in Git distributions, it is considered legacy and has been largely superseded by git merge-file, which offers improved syntax and functionality.

CAVEATS

Internal plumbing command not intended for direct user invocation.
Deprecated; prefer git merge-file for new scripts.
Requires files to exist on disk; no direct blob handling.

EXIT CODES

0: clean merge success.
1: unmerged (conflicts remain).
>1: error (e.g., missing files).

INPUT FILES

Paths to temporary files holding blob contents; typically created by git-merge-index.

HISTORY

Introduced in early Git (circa 2005) as a simple merge helper by Linus Torvalds and contributors. Evolved from shell/perl scripts in contrib/. Retained for backward compatibility but phased out in favor of git merge-file (added in Git 1.7.4, 2010) with better C implementation and options.

SEE ALSO

Copied to clipboard