Progress:
TIER 3 · MODULE 20· Expert

git write-tree

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

FlagWhat it does
--missing-okAllow 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

🧪 Examples

Write the index as a tree
git write-tree
Build a commit purely with plumbing
tree=$(git write-tree); commit=$(echo 'msg' | git commit-tree $tree -p HEAD); git update-ref HEAD $commit
Tree SHA for one subdirectory
git write-tree --prefix=docs/
Tolerate missing objects (partial clone)
git write-tree --missing-ok

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

Hit each option, then Check answers. Score is recorded; Next is always open.