Look inside a snapshot

Lesson 8 of 25 · Git: records, branches, and collaboration

Git represents history using objects, each identified by a hash derived from its type, size, and content. Many repositories use SHA-1; Git also supports SHA-256 object format, so identifier length is not universal. Hashes are useful identifiers, not descriptions or a guarantee against deliberate collisions.

Three object types explain the example:

commit ──> root tree ──> guide.md blob (file content)
   │              ├────> .gitignore blob
   │              └────> keep.tmp blob
   └────> parent commit ──> earlier tree

A blob holds file content; its name is recorded in a tree, along with a mode and object reference. A tree can refer to another tree for a directory. A commit points to a root tree and its parent or parents, and includes author, committer, timestamps, and message. Author and committer need not be the same person. Two commits can describe identical file content yet have different IDs because their metadata or parents differ.

Inspect the actual objects, rather than editing storage files:

git cat-file -t HEAD
git cat-file -p HEAD
git cat-file -p 'HEAD^{tree}'
git cat-file -p HEAD:guide.md

-t prints the type, -p a readable representation. The first result is commit; the tree listing includes a blob entry for guide.md; the last command prints the guide as it was recorded in HEAD. HEAD:guide.md refers to the snapshot, not an unstaged edit on disk. Git records selected file modes, not every filesystem attribute; empty directories have no tracked content. Symlinks and submodules have special representations.

Next: Understand reuse.