Skip to content
rajin

08Building a commit from scratch

core25–30 minHandbook pp. 21–22 Not started

Warm-up · from “Commit objects and immutability”

What does an initial commit lack?

No notes: write what you remember, then check. Recalling before reading is what makes it stick.

The problem

Can Git create a valid commit without a single working file and without running git add or git commit? Build it from the storage layer upward.

After this lesson you can

  • Write a blob.

  • Construct a tree and commit using plumbing.

  • Explain why the working tree may differ afterward.

Key terms

Full glossary

Say what each term means, then open it to check.

  • Commit · from lesson 7

    Object identifying a root tree, parent(s), metadata and message.

  • Tree · from lesson 6

    Directory snapshot mapping names, modes and object IDs.

  • Blob · from lesson 5

    Object storing file contents without a filename.

  • Ref · from lesson 3

    Name identifying an object; branch refs usually identify commits.

  • Index · preview, covered in lesson 9

    Prepared path-to-object entries, plus metadata and conflict stages.

  • HEAD · preview, covered in lesson 14

    Current symbolic branch reference or detached commit identity.

Visual mental model

  1. Blob
  2. Index entry
  3. Tree
  4. Commit
  5. Ref

Build the mental model

Porcelain normally hashes staged contents, writes trees, creates a commit and advances a ref. Plumbing separates these operations. hash-object -w writes an object, update-index associates a path with an object, write-tree serializes the Index as trees, commit-tree creates a commit, and update-ref changes a reference safely. The new commit does not automatically move HEAD or any branch until a ref is updated.

Computed in your browser · exactContent → structure → history

Computed in your browser: a commit object is just text that names a tree and its parents.

Under the hood

This exercise uses an empty initialized repository whose HEAD points to main. It creates app.txt in the Index without writing the working file. After updating main, git status reports a working-tree deletion: the Index and commit contain app.txt, but the disk does not. git restore app.txt then copies the staged contents into the working tree. commit-tree needs a configured identity.

Common mistake · important nuance

Do not manually overwrite ref files in real repositories: use update-ref, which supports locking and expected old values. In linked worktrees, metadata paths differ.

Move between parts freely. Completion counts only what you verify and answer.