Por Anónimo (no verificado) , 29 Abril 2026

Sinopsis

git hash-object [-t <type>] [-w] [--stdin] [<file>...]

Descripción

El comando git hash-object calcula el SHA de los contenidos de un archivo como si fuera un objeto Git, opcionalmente almacenándolo en la base de datos de objetos (con -w). Es plumbing para verificación e indexado de bajo nivel.

Útil para entender cómo Git direcciona contenido y para construir herramientas que crean objetos manualmente.

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 need this directly. It is most useful for scripts that synthesize content into the object database (importers, migration tools), for debugging by checking what hash a piece of content would have, or for educational exploration of Git's content-addressable model.

Opciones comunes

OpciónDescripción
-t <type>Object type (default blob).
-wWrite the object to the database.
--stdinRead content from standard input.
--stdin-pathsRead filenames from stdin.
--no-filtersSkip smudge/clean filters.
--literallyDon't validate content type.

Ejemplos

git hash-object README.md
# Print the SHA Git would assign

echo "hello" | git hash-object --stdin
# Hash arbitrary content

git hash-object -w newfile.txt
# Add the file's content as a blob in the object DB

find . -name '*.png' | git hash-object --stdin-paths
# Batch-compute hashes for many files

Errores comunes

Using -w creates a dangling blob if you don't reference it from a tree. git gc will eventually prune unreachable objects. Hashing through filters (CRLF normalization) yields different SHAs than raw bytes — pass --no-filters to compare with raw hashes from other tools.

Comandos relacionados

git add, git cat-file, git update-index, git write-tree