How Git works · lesson 1 of 6
Commands and ideas in this lesson
Ablob, tree, commit, tagGit's four kinds of object
Bgit cat-file -pprint an object
Cgit hash-objectthe name a file's contents would get
DHEAD^{tree}the tree a commit points to

Objects

Last updated

Every command so far has been moving four kinds of thing around inside .git. This lesson opens the folder and looks at them: blobs, trees, commits and tags. Once you've seen them, branches, merges and rebases stop being magic.

By the end of this lesson you can

  • Explain what blobs, trees, commits and annotated tags hold
  • Explain content addressing: an object's name is the hash of its contents
  • Read any object with git cat-file -p
  • Explain why Git stores snapshots, and where differences come in

Snapshots, not differences

Many older version control tools store a file's first version and then a list of changes. Git doesn't. Each commit records a snapshot: the whole of every file, as it was when you committed.

That sounds wasteful, but two things make it cheap:

  • A file that didn't change isn't stored again. The new snapshot points to the same stored copy as the old one.
  • Git compresses what it stores, and later packs similar objects together (step 5).

Everything Git stores is one of four kinds of object:

Git's four kinds of object
ObjectHoldsMade by
blobOne file’s contents. No name, no date: just the bytes.git add
treeOne folder: a list of names, each pointing to a blob (a file) or another tree (a subfolder).git commit
commitThe top tree, the parent commit or commits, the author, the committer, the times and the message.git commit
tagAn annotated tag (lesson 3.6): the commit it points to, the tagger, the date and a message.git tag -a

One commit, unpacked

Here is a small Kusina with three recipes, one of them in a desserts folder. Step through it, then watch what one change does.

First commit

commit 97a97c6

tree f07062f
author Ana Reyes

Add three recipes
1 of 5 A commit is a small text object: the hash of a tree, its parent (none for the first), the author and the message.

So a commit is a whole snapshot, but it only costs what changed: one blob, and a new tree for each folder on the way up to the top.

The name is the hash of the contents

Every object's name is a hash of its contents: a 40-digit number, worked out from the bytes, that changes completely if any byte changes. Lesson 8.2 looks at hashes closely. For now, three things follow from it:

  • The same contents always get the same name, on any computer. That's how two snapshots share a blob, and how a clone knows which objects it already has.
  • An object can't change. Changing it would change its name, so it would be a new object. This is why "editing" a commit (lesson 2.4, Stage 6) really makes a new one.
  • A commit's name covers its whole history. It contains its tree's hash and its parent's hash, which contains its parent's, and so on. Change anything in the past and every later name changes.

git hash-object prints the name a file's current contents would get, without storing anything:

$ git hash-object adobo.md

Look inside Kusina

git cat-file -p prints any object, given its name or anything that leads to one. HEAD^{tree} means "the tree HEAD's commit points to", and HEAD:adobo.md means "the blob for adobo.md in HEAD". git cat-file -t prints only the kind of object.

Git sandbox
  1. Print the commit HEAD points to with git cat-file -p HEAD
  2. Print its tree with git cat-file -p HEAD^{tree}
  3. Copy a blob’s hash from that list and print it with git cat-file -p
  4. Check the kind of an object with git cat-file -t
  5. Print adobo.md’s name with git hash-object adobo.md, and compare it with the tree
You are in ~/kusina, which has ten commits from two weeks of work.

The blob's hash from hash-object matches the one in the tree, because the file hasn't changed since the last commit. Edit the file, run hash-object again, and the name is completely different.

Spot the trap: "Git stores the changes"

Spot the trapJoaquin

Joaquin is explaining Git to a friend: “Each commit just stores what changed since the one before. That’s why git diff is so fast.”

What happens if…

Check what you learned

What does a blob hold?
Ana changes one line of desserts/leche-flan.md and commits. Which new objects does Git make?
Why can’t a commit be edited in place?

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