Sub topic 14
SHA-1, SHA-256, and object format versions
This chapter assumes you have read the earlier chapters on content-addressable storage and the object header format. You should be able to compute a blob ID by hand with sha1sum.
The hash is a repository setting
Every object ID in Git is the hash of <type> <size>\0<content>. The hash function is not fixed. A repository chooses one when it is created, and that choice is called its object format.
| Object format | Hash function | ID length | Binary ID length |
|---|---|---|---|
| `sha1` (default) | SHA-1 | 40 hex characters | 20 bytes |
| `sha256` | SHA-256 | 64 hex characters | 32 bytes |
The header and the content are the same in both formats. Only the hash function applied to them changes, so the same file gets a different ID in each kind of repository.
Why Git is moving away from SHA-1
SHA-1 is no longer collision-resistant. Researchers have produced two different inputs with the same SHA-1 hash. If an attacker could craft two blobs with one ID, they could try to swap content without changing any ID that points to it.
Git has defended against this since version 2.13. It uses a hardened SHA-1 implementation (SHA-1DC) that detects the known collision patterns and refuses to process them. That is a mitigation, not a new design. SHA-256 removes the dependency on a weakened function.
Creating and checking a SHA-256 repository
Pass --object-format when you create the repository. It cannot be changed afterwards.
git init --object-format=sha256 demo256
cd demo256
git rev-parse --show-object-format
git config extensions.objectFormat
echo 'hello' | git hash-object --stdin
printf 'blob 6\0hello\n' | sha256sumThe last two commands print the same 64-character ID. This is the hand computation from the earlier chapter, with sha256sum in place of sha1sum. In a default repository, the same content gives ce013625030ba8dba906f756967f9e9ca394464a.
git init records the choice in .git/config:
[core]
repositoryformatversion = 1
[extensions]
objectFormat = sha256repositoryformatversion = 1 is the important part. Git versions that don't know the extensions section refuse to open the repository instead of misreading its objects.
What changes inside objects
The format affects every place an ID is stored.
- A tree entry stores the child ID as raw bytes, not hex text. Each entry is therefore 12 bytes longer in a SHA-256 repository.
- Commit
treeandparentlines and tagobjectlines contain the longer hex ID. - Abbreviated IDs from
git log --onelineuse the same default number of characters. They are a shorter share of a longer ID. - Loose object paths still use the first two hex characters as the directory name. The file name is the remaining 62 characters instead of 38.
The commands you have learned (cat-file, mktree, commit-tree, update-ref) work the same way in both formats.
Practical limits
Converting an existing repository means rewriting its history into a new repository, and every commit ID changes. Do this only when you have a specific reason, such as a security policy that prohibits SHA-1.
Exercise
You run git init --object-format=sha256 demo256 and create a blob from the text hello plus a newline. What exact input would you pass to sha256sum to compute the blob ID by hand, and how many hex characters will the ID have?
Show answer
Pass printf 'blob 6\0hello\n' | sha256sum. The header blob 6, a NUL byte, and the 6 bytes of content are hashed together. The ID has 64 hex characters. The input matches the SHA-1 case. Only the hash function differs.
Exercise
Why does Git write repositoryformatversion = 1 and extensions.objectFormat = sha256 into the config of a SHA-256 repository?
Show answer
Older Git versions that don't understand the extensions section will refuse to open the repository. This prevents them from misreading SHA-256 objects as SHA-1 ones.