Skip to content
Tutorials Center
My learning

Topics

Git Reference ยท Plumbing Commands

git write-tree command

Reference for git write-tree: turn the current index into tree objects, print the root tree ID, use --prefix for subtrees and --missing-ok to skip checks.

git write-tree takes the current index and writes it to the object database as a set of tree objects: one for each directory, with the root tree last. It prints the ID of the root tree. This is exactly what git commit does just before it creates the commit object: the new commit's tree is whatever write-tree returns.

The command does not touch the working tree and does not move any ref; the printed tree simply exists afterwards. Combine it with git commit-tree to create commits by hand, or with git diff-tree to compare snapshots.

Terminal
git write-tree
git write-tree --prefix=lib/

Try it

Bash Snapshot the index as a 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 Subtrees with --prefix and unmerged errors

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 write-tree [--missing-ok] [--prefix=<prefix>/]
Name Description
--missing-ok Normally every object named in the index must exist in the database. This option skips that check, so a tree can be written for objects you do not have (useful in partial clones and tooling).
--prefix=<prefix>/ Write only the part of the index that lies under this directory, and print the ID of that subtree. The prefix must end with a slash.

Returns: the 40-character ID (64 in SHA-256 repositories) of the root tree on standard output, exit status 0.

Errors and edge cases:

  • An unmerged index (during a conflicted merge) prints <path>: unmerged (<id>) once for each conflict stage, followed by fatal: git-write-tree: error building trees. Resolve and git add the paths first.
  • A missing blob or tree produces error: invalid object 100644 <id> for '<path>' unless --missing-ok is given.
  • An empty index yields the well-known empty tree, 4b825dc642cb6eb9a060e54bf8d69288fbee4904.

Usage notes

  • Idempotent. The same index gives the same tree and no new objects the second time. The result depends only on paths, modes and blob IDs, not on file timestamps.
  • Directories exist only through files. Git trees hold files and subtrees; an empty directory has no index entry and therefore no tree.
  • The cache-tree extension. The index stores tree IDs for unchanged directories, so write-tree reuses them and only builds trees for what changed. This is why git commit is fast in big repositories.
  • Sorted entries. Entries in a tree are sorted by name in byte order, with directories compared as if their names ended in /. Ordering mistakes in hand-written trees are why git mktree exists.
  • Typical plumbing recipe. git hash-object -w, git update-index --add --cacheinfo, git write-tree, git commit-tree, git update-ref. See Git objects for the walkthrough.
  • Temporary indexes. Set GIT_INDEX_FILE to build trees from a scratch index without disturbing the real one. See GIT_INDEX_FILE.
  • Not a commit. A tree has no author, message or parent. Nothing refers to it until a commit or tag does, and unreferenced trees are removed by git gc.