How Git works · lesson 2 of 6
Commands and ideas in this lesson
ASHA-1the hash Git uses by default today
BSHA-256the stronger hash Git is moving to
Cgit rev-parseprint an object's full name

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.

Git sandbox
  1. Print HEAD’s full name with git rev-parse HEAD
  2. Change only the last commit’s message with git commit --amend -m "…"
  3. Print HEAD’s name again, and compare
  4. 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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 and SHA-256 in Git
SHA-1SHA-256
Default for new repositoriesYes, todayPlanned for Git 3.0
Name length40 hex digits64 hex digits
Make onegit initgit init --object-format=sha256
Hosting sitesAllNot yet supported by GitHub; partial elsewhere
Working with the other kindNot 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.

Terminal
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

Spot the trapAna

Ana writes in Kusina’s README: “The printed edition is commit a5e3.”

What happens if…

Check what you learned

Ana changes one letter in a recipe. How much of its blob’s hash changes?
You reword a commit from ten commits ago. Which commits get new hashes?
Is SHA-256 the default for new Git repositories?

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.

All sources for Simple Git