Sinopsis
git write-tree [--missing-ok] [--prefix=<dir>]
Descripción
El comando git write-tree es plumbing que crea un objeto tree a partir del estado actual del index. Es lo que git commit hace internamente para producir el tree raíz del nuevo commit.
Combinado con git commit-tree, te permite construir commits manualmente sin pasar por la porcelain.
En el uso diario, este comando se integra estrechamente con alias de shell, plugins de editor e integración continua. Los usuarios avanzados a menudo añaden alias que combinan los flags que siempre pasan. El formato de salida puede personalizarse vía configuración de Git. Cuando algo sale mal, ejecuta el comando con GIT_TRACE=1 para revelar las llamadas plumbing subyacentes.
Entender cómo este comando interactúa con el resto del modelo de datos de Git rinde dividendos. Cada comando opera sobre algún subconjunto de las piezas (objetos, index, refs, árbol de trabajo), y saber cuáles toca ayuda a predecir resultados y a recuperarse de errores.
Cuándo usar
You almost never call this directly. git commit calls write-tree internally to capture the staged state. Reach for it when synthesizing tree objects for custom workflows (generating a tree without a commit, scripting commit-tree pipelines).
Opciones comunes
| Opción | Descripción |
|---|---|
--missing-ok | Don't fail if referenced objects are missing. |
--prefix=<dir> | Write a subtree of the index. |
Ejemplos
git write-tree
# Print SHA of tree representing the current index
TREE=$(git write-tree)
COMMIT=$(echo "Snapshot" | git commit-tree "$TREE" -p HEAD)
git update-ref refs/heads/snapshot "$COMMIT"
# Manually craft a commit on a new branch
git write-tree --prefix=docs/
# Just the docs/ subtree
Errores comunes
If the index has unmerged entries, write-tree fails. Resolve conflicts first. Forgetting that write-tree doesn't create a commit — only a tree — leads to dangling objects. git help write-tree opens the full manual page with comprehensive coverage of every option and edge case; it remains the authoritative reference whenever the summary on this page leaves a question unanswered. For team workflows, capturing how your project uses this command in a CONTRIBUTING document avoids repeated onboarding questions and keeps everyone aligned on conventions.
Comandos relacionados
git read-tree, git commit-tree, git ls-tree, git update-index