Hashes
Last updated
A commit's name, like a5e3d67, isn't a number Git counts up. It's a hash: a fingerprint worked out from the commit's contents. In this lesson you'll watch a hash change, see why changing any old commit renames every commit after it, and learn why Git is moving from SHA-1 to SHA-256.
By the end of this lesson you can
- Explain what a hash is and why it identifies content
- Explain why changing a commit changes every later commit's hash
- Say why SHA-1 is being replaced, and what Git does about it
- Use short hashes safely
What a hash is
A hash function turns any amount of data into a short, fixed-size number, called a hash or digest. A good one has three properties Git relies on:
- Same input, same hash, every time, on every computer.
- Any change gives a completely different hash. Not a slightly different one: about half of its bits flip, so nearly every digit changes.
- You can't find two inputs with the same hash in practice, or work backwards from a hash to the input.
Git uses SHA-1, which makes a 160-bit hash, written as 40 hexadecimal digits (0–9 and a–f). Git hashes each object's kind and size followed by its contents (lesson 8.1).
Change one letter
This works out the name Git would give a file containing just the line you type: the same thing git hash-object does. Change tsp to tbsp.
Its name in Git today (SHA-1, 40 hex digits)
Started as 249d6f072c60050d1af2e30f28725938142389e1
In a SHA-256 repository (64 hex digits)
This is the line you started with. Change one letter.
SHA-1 now: 249d6f072c60050d1af2e30f28725938142389e1. SHA-256 now: .
There's no pattern to follow from one hash to the next. That's what makes a hash a good fingerprint: two files with the same hash have the same contents.
Why every later hash changes
A commit contains its parent's hash (lesson 8.1). So its own hash depends on its parent's, which depends on its parent's, all the way back. Change one old commit, even only its message, and it gets a new hash; so does its child, and every commit after it.
That's what makes rewriting history (Stage 6) visible to everyone: the commits after the change are all new commits, as far as any other clone can tell. It's also why one commit hash vouches for the whole history behind it.
- Print HEAD’s full name with
git rev-parse HEAD - Change only the last commit’s message with
git commit --amend -m "…" - Print HEAD’s name again, and compare
- Find the old commit in
git reflog: it’s still there, under its old name
You are in ~/kusina, which has ten commits from two weeks of work.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
The files didn't change, so the tree's hash didn't either. Only the commit changed, so only the commit got a new name.
SHA-1, hardened SHA-1 and SHA-256
SHA-1 was designed in the 1990s, and it has weakened since:
- In 2017, researchers made the first known collision: two different PDF files with the same SHA-1 hash. It took a huge amount of computing, but it showed that SHA-1's third property no longer holds.
- Git responded with hardened SHA-1: since Git 2.13, Git uses a version of SHA-1 that detects the trick those attacks use, and refuses objects made with it. Hashes of ordinary files are unchanged.
- The long-term fix is SHA-256, a newer hash with 256 bits, written as 64 hex digits. NIST, which publishes the US standard, has said SHA-1 should be out of use everywhere by 2030.
Where Git stands, as checked for this lesson:
| SHA-1 | SHA-256 | |
|---|---|---|
| Default for new repositories | Yes, today | Planned for Git 3.0 |
| Name length | 40 hex digits | 64 hex digits |
| Make one | git init | git init --object-format=sha256 |
| Hosting sites | All | Not yet supported by GitHub; partial elsewhere |
| Working with the other kind | Not yet: a SHA-1 repository and a SHA-256 one can’t push to each other |
Git 3.0 hasn't been released. Git's BreakingChanges page lists SHA-256 for new repositories as a planned 3.0 default, with the hosting sites' support as the main thing it's waiting for. Existing SHA-1 repositories won't change. For now, use SHA-1 unless you have a reason not to, and check that your host supports SHA-256 before trying it.
git init --object-format=sha256 test-repo Initialized empty Git repository in /home/ana/test-repo/.git/ cd test-repo git rev-parse --show-object-format sha256
Spot the trap: short hashes in documents
Ana writes in Kusina’s README: “The printed edition is commit a5e3.”
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: hash-function-transition: why Git is moving to SHA-256, and SHA-1 collision detection
- Git documentation: BreakingChanges: the changes planned for Git 3.0, including SHA-256 by default
- Git documentation: git-init, --object-format: making a SHA-256 repository
- Git documentation: git-rev-parse: full and short names
- NIST: Transitioning away from SHA-1 for all applications: the standards body retiring SHA-1 by 2030