Progress:
TIER 3 · MODULE 10· Expert

git mktree

Build a tree by hand from blob SHAs.

🎯 What & why

Plumbing that builds a tree object from a flat listing of entries on stdin and prints the resulting tree SHA. It is the inverse of ls-tree and the way to assemble trees without touching the index.

🧠 Mental model

Hand it the lines ls-tree would have printed and it gives you back a tree SHA. Combine with hash-object and commit-tree to fabricate a commit entirely server-side, with no working tree at all.

🛠️ Synopsis

git mktree [-z] [--missing] [--batch] < entries.in
# entry format per line:
#   <mode> SP <type> SP <sha> TAB <name>
# e.g. 100644 blob a1b2c3...\tREADME.md
# prints: <tree-sha>

🎚️ Switches & options

FlagWhat it does
-zNUL-terminate input records instead of LF (safe for weird filenames).
--missingAllow referenced objects to be absent from the ODB.
--batchRead multiple trees separated by blank lines; emit one SHA per tree.
(stdin)Lines must be sorted as Git expects; otherwise the resulting tree differs.

💡 Use cases

🧪 Examples

Build a tiny tree by hand
printf '100644 blob %s\tREADME.md\n' "$(git hash-object -w README.md)" | git mktree
Round-trip an existing tree
git ls-tree HEAD | git mktree   # should reprint the same SHA as HEAD^{tree}
Batch-build many trees
cat trees.in | git mktree --batch    # blank lines separate trees
Tree -> commit -> ref, no working copy
tree=$(git ls-tree HEAD | git mktree) && c=$(echo msg | git commit-tree "$tree" -p HEAD) && git update-ref refs/heads/auto "$c"

🎓 Recommendations

🪤 Common pitfalls

🔗 Related modules

📝 Quiz

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