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:
| Object | Holds | Made by |
|---|---|---|
| blob | One file’s contents. No name, no date: just the bytes. | git add |
| tree | One folder: a list of names, each pointing to a blob (a file) or another tree (a subfolder). | git commit |
| commit | The top tree, the parent commit or commits, the author, the committer, the times and the message. | git commit |
| tag | An 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
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.mdLook 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.
- Print the commit HEAD points to with
git cat-file -p HEAD - Print its tree with
git cat-file -p HEAD^{tree} - Copy a blob’s hash from that list and print it with
git cat-file -p - Check the kind of an object with
git cat-file -t - 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"
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
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.
- Pro Git: Git Objects: blobs, trees and commits, built by hand
- Git documentation: git-cat-file: -p, -t and -s
- Git documentation: git-hash-object: computing an object's name
- Git documentation: gitrevisions: rev^{tree} and rev:path
- Pro Git: Packfiles: where Git does store differences, as compression