Counterpart to commit-tree. Together they implement git commit.
🎯 What & why
git write-tree serialises the current index into one or more tree objects and prints the SHA of the top-level tree on stdout. It is the second half of what git commit does, paired with commit-tree.
🧠 Mental model
Index in, tree SHA out. The same index always yields the same tree SHA; write-tree is pure with respect to .git/index. It writes new tree objects into the object store but does not touch refs, HEAD, or the worktree.
🛠️ Synopsis
git write-tree [--missing-ok] [--prefix=<dir>/]
# Stdout: 40-char SHA of the resulting tree.
🎚️ Switches & options
Flag
What it does
--missing-ok
Allow the index to reference blob/tree SHAs that are not present locally (partial clones).
--prefix=<dir>/
Write only the subtree rooted at <dir>; the printed SHA is for that subtree, not the whole index.
💡 Use cases
Build a commit by hand: write-tree, then commit-tree, then update-ref.
Compute a tree SHA for the current index without committing, e.g. to compare against another tree.
Snapshot just one directory of the index using --prefix, useful in subtree workflows.
Verify that the index is well-formed; write-tree fails loudly if entries are inconsistent.