Sinopsis
git cat-file (-t | -s | -p | -e) <object>
git cat-file --batch
git cat-file --batch-check
Descripción
El comando git cat-file es plumbing para inspeccionar objetos Git por SHA: blobs, trees, commits, tags. Muestra el tipo (-t), tamaño (-s) o contenido (-p) de un objeto.
Esencial para debugging y exploración del modelo de objetos. git cat-file -p HEAD muestra el objeto commit en formato legible.
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
Most developers never need git cat-file directly — porcelain commands like git show, git log, and git diff wrap it transparently. Reach for it when writing scripts that walk Git objects, debugging repository corruption, or learning how Git stores data internally.
Opciones comunes
| Opción | Descripción |
|---|---|
-t | Print object type. |
-s | Print object size in bytes. |
-p | Pretty-print contents in a type-aware way. |
-e | Exit zero if the object exists. |
--batch | Stream <sha> <type> <size> CRLF <content> for each input. |
--batch-check | Like batch, but only header lines. |
--filters | Apply smudge filters when printing blobs. |
Ejemplos
git cat-file -t HEAD
# commit
git cat-file -p HEAD
# Show commit message and tree/parent SHAs
git cat-file -p HEAD:src/main.c
# Print the file as it was at HEAD
echo HEAD | git cat-file --batch-check
# Get type/size/SHA in machine-readable form
Errores comunes
Passing a partial SHA that's ambiguous returns an error — disambiguate with more characters. Forgetting -p and using -t doesn't print contents. --batch mode requires careful protocol handling; respect the size header to read the right number of bytes.
Comandos relacionados
git show, git hash-object, git ls-tree, git rev-parse