SHA Hashes in Git
Every object in Git is identified by a SHA hash — a 40-character hexadecimal string that serves as both the name and the integrity check of that object. Understanding how these hashes work explains why Git history is immutable, how Git detects corruption, and why changing any byte in any commit changes every subsequent commit hash.
What SHA-1 Is
SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that takes any input and produces a fixed 160-bit (20-byte, 40 hex character) output. The same input always produces the same output. Different inputs (almost certainly) produce different outputs. Crucially, you cannot reverse the process — given a hash, you cannot reconstruct the input without trying every possibility.
SHA-1 demo: same input, same hash
# Same content always produces the same hash echo "hello world" | git hash-object --stdin # 8c7e5a667f1b771847ad9a65b8d2fd67e80cc76d echo "hello world" | git hash-object --stdin # 8c7e5a667f1b771847ad9a65b8d2fd67e80cc76d (identical) # One character change → completely different hash echo "hello World" | git hash-object --stdin # 93ba5c3b3db4e0aa11f2c6e3c9f1e5d7a8b2c4d6 (totally different)
How Git Computes the Hash
Git does not hash just the raw content. It prepends a header with the object type and size, then hashes the combination. The format is exactly:
Hash input format
<type> <size in bytes>