Git Reference ยท Objects, Refs and Internals
Object IDs and SHA-256
How Git computes object IDs from a type header and content, what abbreviated IDs are, and how --object-format=sha256 creates repositories with 64-character IDs.
An object ID is the hash of the object's bytes preceded by a small header: the type, a space, the content length in decimal, and a NUL byte. For a blob that is blob 3\0hi\n for the three bytes hi and a newline. Git hashes that string and uses the result, written in hexadecimal, as the object's name and, split in two, as its path under .git/objects.
Historically the hash is SHA-1 (40 hex digits). Since Git 2.29 a repository can be created with SHA-256 (64 hex digits) instead. The choice is made once, at git init or git clone, and recorded in the repository's configuration as extensions.objectformat.
git hash-object file.txt # ID the file would get, without storing it git init --object-format=sha256 repo # new repository using SHA-256 git rev-parse --show-object-format # which algorithm this repository uses
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
How IDs behave
Same content, same ID, everywhere. Two people who commit the identical file get the identical blob ID, which is how Git avoids storing duplicates and how pushes know what the other side already has. Commit IDs also fold in the parents, the author and the timestamps, so they differ unless every one of those matches.
Abbreviations. Any unambiguous prefix of at least four hexadecimal digits names an object. git log --abbrev=12, core.abbrev and git rev-parse --short=N control how much is shown. Git picks a length that stays unambiguous for the repository's size, so very large repositories show longer defaults. If a prefix matches more than one object, Git reports short object ID ... is ambiguous and lists candidates.
Header matters. Because the type and size are hashed in, a blob and a commit with the same bytes have different IDs, and git hash-object -t decides which one you compute.
Collisions. SHA-1 is no longer collision-resistant in theory; since 2017 Git uses a hardened SHA-1 implementation that detects known attack patterns and refuses to produce a colliding pair. SHA-256 removes the concern.
Syntax
git init --object-format=<sha1|sha256> [<directory>] git clone --object-format=<sha1|sha256> <url> git rev-parse --show-object-format[=<storage|input|output>]
| Part | Description |
|---|---|
--object-format |
Hash algorithm for a new repository. clone normally follows the remote and rejects a mismatch. |
extensions.objectformat |
Configuration key written for SHA-256 repositories. It forces core.repositoryformatversion = 1, so older Git versions refuse to open the repository instead of corrupting it. |
--show-object-format |
Prints sha1 or sha256; the storage, input and output forms matter for the experimental interoperability mode. |
Usage notes
- Repositories cannot mix algorithms. A SHA-256 repository cannot fetch from or push to a SHA-1 one (unless the interoperability work is enabled), because every ID in a commit would change.
- Hosting support is limited. Many hosting services and tools only understand SHA-1 today. Use SHA-256 for local, controlled environments or experiments.
- Scripts that hard-code 40 (regular expressions like
[0-9a-f]{40}) break with SHA-256. Usegit rev-parse --verifyto check names, andgit hash-objectorgit rev-parserather than assuming a length. - Null ID: all zeros, 40 or 64 long, appears in hooks and ref updates to mean "does not exist".
- Converting an existing history to SHA-256 means rewriting it (for example with
git fast-exportandgit fast-import --object-format), which changes every ID. - Short IDs in prose or tickets remain valid for a while but can become ambiguous as a repository grows; use at least 10 digits for anything long-lived.