Git: What a Commit Really Stores

Outline

Transcript

0:00 A Git commit identifies a saved project state, so we can inspect what was recorded. Its record doesn't store the diff we just read. We'll follow one staged edit to its file bytes, then see how packed storage preserves that state. Git stores those pieces of data as objects. A tree maps staged filenames to their stored contents. We traced them using Git's own documentation and pinned source code; the links are in the description. Take this worker-setting edit from one to two. We don't want to check the wrong configuration.

0:31 How do we find the setting it saved? Start with the tree named in the commit. It points us toward the new settings and the unchanged notes. Does that saved state need another copy of the notes? It can reuse notes content that's already stored. We'll follow the references and see which pieces changed. Now open that stored commit. Its tree field identifies the root of the staged project state. And the parent field points to the preceding commit. There's metadata and a message too, but the actual file contents live elsewhere.

1:03 That message could say anything. I don't trust it to tell me which value was saved. Right. The tree comes from the index, the staged entries. Its identifiers lead us to the contents that were actually saved. Okay, then open that tree and look up our two files. Next, follow the root tree to our notes and settings files. Each entry connects a filename to another object, with a file mode alongside it. That mode tells Git about the kind of entry; our example uses ordinary files. The content objects are called blobs, which is just Git's name for the stored file bytes.

1:38 For instance, when you open our notes blob, you get “Ship a small change.” The filename isn't tucked into that content. It's in the tree entry pointing here. Okay, so I can follow the commit through its tree to the file's bytes. For example, the command on screen reads our saved worker setting as two. Now those separate content objects show what changes when we edit the settings. Our first snapshot has four objects: two file blobs, a root tree, and the commit. An object's ID is computed from its type, byte length and contents.

2:10 Our settings bytes changed from one worker to two, so their ID changed. The notes keep the same ID. The updated root tree now links to the changed settings and the existing notes blob. Then we write a commit for that tree, with its predecessor recorded as the parent. Which takes this tiny database from four objects to seven. A new settings blob, a new tree and a new commit. We didn't add a second notes blob. My instinct is to count three new objects per edit. What happens to that count when the file is in a subdirectory?

2:43 Imagine editing a file inside several directories. Their affected trees change the count; three belongs to this two-file example. True. I was about to turn one example into a rule. Our old settings remain available, which lets us compare the saved versions. With both snapshots saved, we can produce the patch we're used to reading. The screen shows the old worker setting removed and the new one added. Git found the two saved contents and compared them. Reading either version doesn't require replaying the earlier commits' patches.

3:15 Then a snapshot describes the saved state. Physical storage can still use delta instructions, which rebuild the content using another object as their base. Before we inspect packed storage, let's give this commit another branch name and see what gets copied. So another name can refer to this existing commit. We created the review branch and counted seven objects both before and after. Can we read that setting through the new branch name too? Yes, two workers through review. Its reference leads back to that same commit.

3:45 So we gave that commit another name. The file objects stayed put. Back to packing, can we follow one version being rebuilt from its base? Now let's change the storage behind those snapshots. We made several similar versions of a larger text file, changing one record each time, then collected the objects into packed storage. The file was about 52,000 bytes in each version. Storing every version independently would repeat a lot of content, even though Git already compresses each separate object file.

4:16 A pack groups objects in one file. It can store a version through a base plus delta instructions. Read the base, apply those instructions, and reconstruct the other object's bytes. Does the older version have to be the base, with instructions carrying it forward through the edits? Our selected pair goes the other way. It uses the later version as the base. Watch the highlighted record: copy the prefix, insert the digit six in place of seven, then copy the rest. That reconstructs the earlier file.

4:44 Hold on. That small delta still needs the base to supply everything it copies. So the parent arrows don't dictate the compression arrows? Exactly. Every object in our test still reconstructed to the same bytes after packing, and Git's integrity check passed. We can now read either saved version through its original ID even though its physical representation has changed. Finally, read back the small settings edit through those original IDs. Even after packing, reading the old settings blob returns one worker, and reading the new one returns two.

5:17 Both snapshots still point to that one notes object. A diff compares their saved states for us. A packed delta reconstructs an object's bytes from a base on disk. Nice. The storage changed, and we can still ask for those exact bytes. I'd check that saved value before trusting what's open in my editor. Thanks for listening to Learning Podcasts.