Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git merge-tree command

Reference for git merge-tree: compute a merge of two commits in the object database only, get the result tree ID, detect conflicts by exit code, and use it in bare repositories.

git merge-tree performs a real merge of two commits, but only inside the object database. It does not need a working tree or an index, does not change HEAD, and does not move any branch. On success it prints the ID of the tree that the merge would produce. If there are conflicts, it still writes a tree (with conflict markers inside the affected files) and tells you which paths conflicted.

This makes it ideal for server-side checks ("can this pull request merge cleanly?"), for bare repositories, and for experiments where you want to look at a merge result without disturbing your files.

Terminal
git merge-tree --write-tree main feature

Try it

Bash A clean merge and its result tree

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.

Bash Detect a conflict without merging

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

Terminal
git merge-tree --write-tree [<options>] <branch1> <branch2>
git merge-tree --write-tree [<options>] --merge-base=<commit> <branch1> <branch2>
git merge-tree <base-tree> <branch1> <branch2>        # old, trivial-merge mode
Option Description
--write-tree Do a real merge (with rename detection and recursive handling) and write the result tree. Required to select this mode.
--name-only List only the names of conflicted files instead of mode, object ID and stage.
--no-messages, --messages Suppress or force the informational section that follows the file list.
-z NUL-separate the output fields.
--allow-unrelated-histories Merge commits that share no ancestor.
--merge-base=<commit> Use this commit as the base instead of computing one. Only with exactly two branches.
-X<option> Pass a strategy option such as -Xours or -Xignore-space-change.

Returns: the first line is the ID of the resulting tree. On conflict, it is followed by one line per conflicted path (mode, object ID, stage and name: stages 1, 2 and 3 appear separately), a blank line and the messages Git would have printed ("CONFLICT (content): ...").

Exit status: 0 for a clean merge, 1 for a merge with conflicts, anything else for an error.

Usage notes

  • Check the exit status, not the output. A conflicted merge still prints a tree ID. Only status 0 means the tree is clean.
  • Stored objects. New blobs and trees are written to the object database, so they stay until git gc prunes them. No ref points at them.
  • Make a merge commit from it: git commit-tree "$tree" -p main -p feature -m "..." builds the commit; git update-ref advances a branch. This is how hosting services create merge commits in bare repositories.
  • Real merge, not a guess. Rename detection and virtual base commits for criss-cross histories are handled the same way as in git merge -s ort.
  • The old mode is deprecated. git merge-tree <base-tree> <b1> <b2> only does a trivial merge and prints a diff-like description. Prefer --write-tree; it was added in Git 2.38.
  • Compare with git merge --no-commit, which also lets you inspect a result but touches the index and working tree and must be aborted.