LinuxCommandLibrary

git-merge-tree

Combine and analyze content differences between trees

TLDR

Show the result of a merge between two branches

$ git merge-tree [branch1] [branch2]
copy

Perform a merge and write the resulting tree
$ git merge-tree --write-tree [branch1] [branch2]
copy

SYNOPSIS

git merge-tree [--write-tree] <base-tree> [-X<strategy-option>=<value>...] <head> <remote> [--] [<pathspec>...]

PARAMETERS

--write-tree
    Output the ID of the resulting tree object to stdout instead of per-path contents.

-X<option>=<value>, --strategy-option=<option>=<value>
    Pass the specified merge strategy option and value (e.g., -Xpatience).

DESCRIPTION

git merge-tree is a low-level plumbing command in Git that performs a three-way merge between a base tree, head tree, and remote tree for specified paths. Unlike higher-level commands like git merge, it does not modify the working directory, index, or repository state. Instead, it outputs the merge results directly to stdout.

For each pathspec, if the merge is clean, it prints the path followed by a NUL byte and the merged blob content. In case of conflicts, it outputs a detailed conflict format with markers:
  path
  <<<<<<< HEAD
  head content
  ||||||| base
  base content
  =======
  remote content
  >>>>>>> remote

This makes it ideal for scripting merge resolution, inspecting potential conflicts, or integrating into custom tools. It supports merge strategies via options, allowing customization like ignoring whitespace. Recent versions added --write-tree to output the resulting tree object ID, enabling efficient merge tree creation without full checkout.

Primarily used internally by Git porcelain commands, it's valuable for advanced users needing precise control over tree merges.

CAVEATS

Plumbing command: output format not guaranteed stable across Git versions. Does not recurse into submodules. Requires tree-ish arguments (commits, trees, tags). Paths with conflicts include full markup; clean merges use NUL-separated binary output.

OUTPUT FORMAT

Clean merge: <path>NULmerged-content
Conflict: <path>
<<<<<<< head-label
head content
||||||| base-label
base content
=======
remote content
>>>>>>> remote-label

HISTORY

Added in Git 1.7.4 (April 2011) as plumbing for tree-level merges. The --write-tree option introduced in Git 2.46 (July 2024) to support efficient scripted merging without index manipulation.

SEE ALSO

Copied to clipboard