Application developmentDive 02 of 02

What Git stores when you commit

Follow one edited file through the index, blob, trees and commit. Inspect the actual bytes, predict which object IDs change on a rename and reconstruct an object from a real pack delta.

Levels
8
Reading
34 min
Depth
Idea to System
Published
HEADship = no2600a482ae27
Indexship = yes9bea5dfe9f07
Working treeship = maybe58894bf47c7a

Level 01/08Idea

Three versions of one file

Your release note says ship = no. You change it to ship = yes, run git add, then type ship = maybe before committing. All three answers now exist in the same local repository. Which one goes into the commit?

Our scratch repository tracks notes/status.txt. Its first line is the release decision. A fixed checklist follows it so Git has substantial identical data to reuse when the first line changes. Each step below changes the decision, the path or the commit message while keeping everything else in the example under control.

A plain git commit takes the staged version. In our experiment, the commit includes ship = yes and the working file still contains ship = maybe. The repository’s three views are distinct. HEAD resolves to the last commit on the current branch. The index records the entries proposed for the next tree. The working tree holds the files you edit.

Stage one answer and leave another in the working tree
# In the scratch repository
git switch --detach 7a228b1843286ff7cb3fad369f8b2ea9f76667d0
sed -i '1s/ship = no/ship = yes/' notes/status.txt
git add -- notes/status.txt
sed -i '1s/ship = yes/ship = maybe/' notes/status.txt
git status --short -- notes/status.txt
git diff --cached -- notes/status.txt
git diff -- notes/status.txt
git status --short
MM notes/status.txt
git diff --cached
diff --git a/notes/status.txt b/notes/status.txt
index 2600a48..9bea5df 100644
--- a/notes/status.txt
+++ b/notes/status.txt
@@ -1,4 +1,4 @@
-ship = no
+ship = yes
 check 001: verified in environment 0; revision 0; artifact archived
 check 002: verified in environment 1; revision 137; artifact archived
 check 003: verified in environment 2; revision 274; artifact archived
git diff
diff --git a/notes/status.txt b/notes/status.txt
index 9bea5df..58894bf 100644
--- a/notes/status.txt
+++ b/notes/status.txt
@@ -1,4 +1,4 @@
-ship = yes
+ship = maybe
 check 001: verified in environment 0; revision 0; artifact archived
 check 002: verified in environment 1; revision 137; artifact archived
 check 003: verified in environment 2; revision 274; artifact archived

The short status shows MM. For a normal tracked path, the first column reports the staged difference from HEAD and the second reports the unstaged difference from the index. That explains why the two diffs both show an edit to the same line and disagree about its previous value. Each diff compares a different pair of inputs.

The distinction is easiest to see with a plain commit. Options such as -a stage changes to tracked paths as part of the command, and pathspecs select a different workflow. We use no such option in the fixture. Status columns · Commit inputs

Check yourselfWhich version goes into a plain git commit at this point?

Level 02/08Interface

What the index actually keeps

Git needs more than a list of changed files. It needs a complete description of the tracked paths to put in the next snapshot. The index keeps an object ID and file mode for each ordinary path entry. Entries for unaffected files still belong to the snapshot. Git can combine unchanged and newly staged object IDs without copying full working-tree files into the index.

In this fixture, git add stores the selected file content as an object and writes the ID into the index entry. Read the actual index file from that moment. Its first twelve bytes are the signature DIRC, a four-byte format version and a four-byte entry count, both big-endian. The version-2 entry for notes/status.txt contains the blob ID from the staged version. The summary below decodes the stored fields.

Captured .git/index header and version-2 entry
44 49 52 43 00 00 00 02 00 00 00 01
DIRC | version 2 | 1 entry

Mode:        100644
Object ID:   9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3
Flags:       0x0010
Path:        notes/status.txt
SHA-1:       c099c7f19b06f049339945640f06e5098cd993c3

An ordinary version-2 entry also holds cached file metadata such as modification time and size. Git uses those fields to avoid unnecessary content reads when it compares the working tree with the index. The stat fields can differ even when the staged object is identical. That is one reason the raw index checksum can change between runs of the same experiment while the commit IDs stay reproducible.

Index paths are sorted. Stage zero is the ordinary resolved entry. During an unresolved merge, a path can occupy higher stages representing different inputs, typically the common ancestor and the two sides. The index can also have extensions and newer versions can encode paths differently. The example is deliberately one file and one version-2 entry, so its bytes are easy to locate. Index file format · git ls-files

The staged entry as Git prints it
git ls-files --stage
# 100644 9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3 0  notes/status.txt

The index is the proposed tree expressed as paths, modes and object IDs, with extra data to speed comparisons and handle merges.

Level 03/08Runtime

Put a header on the bytes

The staged object is a blob. In our SHA-1 repository, Git computes its ID from the ASCII header blob <size>, a NUL byte and the payload. The size is a byte count. When the payload changes by a single newline or encodes the same displayed text in different bytes, the bytes going into the hash can change too.

Try the small example below in a SHA-1 scratch repository. The first command gives Git the payload. The second builds the complete hash input itself. Both produce the same ID because the thirteen payload bytes and the framing bytes match. No file name enters the blob hash.

Two ways to compute the ID of the same blob
printf 'test content\n' | git hash-object --stdin
# d670460b4b4aece5915caf5c68d12f560a9fe3e4

printf 'blob 13\000test content\n' | sha1sum
# d670460b4b4aece5915caf5c68d12f560a9fe3e4  -

For a loose object, Git zlib-compresses that framed byte stream and uses the first two hex characters of the ID as a directory fan-out. The staged blob in the fixture lives at .git/objects/9b/ea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3. Its filename identifies the uncompressed logical object, independently of how well its representation compresses.

Inspect the four objects below. The leftmost hex columns begin with the type, byte count and zero terminator. After that, the payload’s shape depends on the object type. The capture inflates each loose file and compares it byte-for-byte with the header and payload reconstructed from git cat-file.

Object ID
9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3
Payload
8,396 bytes
Loose file
947 bytes
Blob payload · first four lines
ship = yes
check 001: verified in environment 0; revision 0; artifact archived
check 002: verified in environment 1; revision 137; artifact archived
check 003: verified in environment 2; revision 274; artifact archived
Inflated bytes · first 176 of 8406
0000  62 6c 6f 62 20 38 33 39 36 00 73 68 69 70 20 3d  blob 8396.ship =
0010  20 79 65 73 0a 63 68 65 63 6b 20 30 30 31 3a 20   yes.check 001: 
0020  76 65 72 69 66 69 65 64 20 69 6e 20 65 6e 76 69  verified in envi
0030  72 6f 6e 6d 65 6e 74 20 30 3b 20 72 65 76 69 73  ronment 0; revis
0040  69 6f 6e 20 30 3b 20 61 72 74 69 66 61 63 74 20  ion 0; artifact 
0050  61 72 63 68 69 76 65 64 0a 63 68 65 63 6b 20 30  archived.check 0
0060  30 32 3a 20 76 65 72 69 66 69 65 64 20 69 6e 20  02: verified in 
0070  65 6e 76 69 72 6f 6e 6d 65 6e 74 20 31 3b 20 72  environment 1; r
0080  65 76 69 73 69 6f 6e 20 31 33 37 3b 20 61 72 74  evision 137; art
0090  69 66 61 63 74 20 61 72 63 68 69 76 65 64 0a 63  ifact archived.c
00a0  68 65 63 6b 20 30 30 33 3a 20 76 65 72 69 66 69  heck 003: verifi
Object ID
a60ed2b4e68c859279fa7aa7504dcc839e67ae21
Payload
38 bytes
Loose file
55 bytes
git cat-file -p
100644 blob 9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3	status.txt
Inflated bytes · object header and payload
0000  74 72 65 65 20 33 38 00 31 30 30 36 34 34 20 73  tree 38.100644 s
0010  74 61 74 75 73 2e 74 78 74 00 9b ea 5d fe 9f 07  tatus.txt...]...
0020  2e ad 3c 4f df 6c 28 b7 c9 cd 9b 1e 87 c3        ..<O.l(.......
Object ID
59e4b7846a0bb43797822f482c0c13870dfbd2be
Payload
32 bytes
Loose file
47 bytes
git cat-file -p
040000 tree a60ed2b4e68c859279fa7aa7504dcc839e67ae21	notes
Inflated bytes · object header and payload
0000  74 72 65 65 20 33 32 00 34 30 30 30 30 20 6e 6f  tree 32.40000 no
0010  74 65 73 00 a6 0e d2 b4 e6 8c 85 92 79 fa 7a a7  tes.........y.z.
0020  50 4d cc 83 9e 67 ae 21                          PM...g.!
Object ID
d499b2086bb5153af496b73fa17fe714dec14205
Payload
236 bytes
Loose file
163 bytes
git cat-file -p
tree 59e4b7846a0bb43797822f482c0c13870dfbd2be
parent 7a228b1843286ff7cb3fad369f8b2ea9f76667d0
author Release Bot <release@example.test> 1795089600 +0000
committer Release Bot <release@example.test> 1795089600 +0000

Approve the release
Inflated bytes · first 176 of 247
0000  63 6f 6d 6d 69 74 20 32 33 36 00 74 72 65 65 20  commit 236.tree 
0010  35 39 65 34 62 37 38 34 36 61 30 62 62 34 33 37  59e4b7846a0bb437
0020  39 37 38 32 32 66 34 38 32 63 30 63 31 33 38 37  97822f482c0c1387
0030  30 64 66 62 64 32 62 65 0a 70 61 72 65 6e 74 20  0dfbd2be.parent 
0040  37 61 32 32 38 62 31 38 34 33 32 38 36 66 66 37  7a228b1843286ff7
0050  63 62 33 66 61 64 33 36 39 66 38 62 32 65 61 39  cb3fad369f8b2ea9
0060  66 37 36 36 36 37 64 30 0a 61 75 74 68 6f 72 20  f76667d0.author 
0070  52 65 6c 65 61 73 65 20 42 6f 74 20 3c 72 65 6c  Release Bot <rel
0080  65 61 73 65 40 65 78 61 6d 70 6c 65 2e 74 65 73  ease@example.tes
0090  74 3e 20 31 37 39 35 30 38 39 36 30 30 20 2b 30  t> 1795089600 +0
00a0  30 30 30 0a 63 6f 6d 6d 69 74 74 65 72 20 52 65  000.committer Re
Raw loose-object captures from Git 2.53.0 in a SHA-1 repository. The ASCII view shows binary bytes as dots. Long objects are excerpted.

Git stores the canonical bytes it stages. Attributes can configure a clean filter or line-ending conversion, so those bytes need not be identical to a working-tree file’s on-disk representation. This fixture has no attributes and explicitly uses SHA-1. A SHA-256 repository uses a different hash algorithm and wider object IDs. Its tree and commit payloads also contain those wider IDs where they reference other objects. Git object framing · Hash formats

Level 04/08Runtime

Where a filename lives

Where does notes/status.txt live, then? In the directory structure. Git’s root tree has an entry named notes pointing to another tree. That child tree has an entry named status.txt pointing to the blob. Git reconstructs the full path by walking entries, one component at a time.

Each tree entry has a mode in ASCII octal, a space, a name terminated by NUL, then an object ID in raw bytes. In the fixture, the blob entry starts with 100644 status.txt. The rendered output from git ls-tree inserts a type label and prints the ID as hexadecimal. Those are conveniences of the command. In the loose bytes, the ID after the NUL is twenty binary bytes.

Follow the staged blob through the edit commit’s trees
git ls-tree 59e4b7846a0bb43797822f482c0c13870dfbd2be
# 040000 tree a60ed2b4e68c859279fa7aa7504dcc839e67ae21  notes
git ls-tree a60ed2b4e68c859279fa7aa7504dcc839e67ae21
# 100644 blob 9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3  status.txt

Git writes a tree from the staged path entries. For this edit it writes a new notes tree because the blob pointer changes, then a new root tree because the notes pointer changes. An unchanged subtree can be referenced by its existing tree ID. It needs no new tree object for this commit.

A tree also tracks modes. The same blob can be used at two paths or under different relevant modes. Changing the executable bit from 100644 to 100755 changes the tree entry without changing the blob. A gitlink uses mode 160000 and points to a submodule commit ID. The ordinary index records tracked files. A working directory with no tracked descendants is absent from a snapshot built from that index. Tree display · Writing a tree from the index

Level 05/08Runtime

The branch moves and the objects stay

A commit object records a root tree ID, zero or more parent commit IDs, author and committer headers, then a blank line and the message. The first commit has no parent. This edit has one. A merge can have more than one. The stored headers, time zones, message and any additional headers all participate in the commit’s ID.

In the object browser, select Commit and find the parent line. It points to the baseline commit. The tree line points to the edited snapshot. The commit tells Git both what to read and how history connects. To display a diff, Git compares trees. The commit payload contains references and metadata, so it does not need to store a ready-made list of changed lines.

The complete payload of the edit commit
tree 59e4b7846a0bb43797822f482c0c13870dfbd2be
parent 7a228b1843286ff7cb3fad369f8b2ea9f76667d0
author Release Bot <release@example.test> 1795089600 +0000
committer Release Bot <release@example.test> 1795089600 +0000

Approve the release

References are a separate layer. In the scratch repository’s captured state, refs/heads/main holds the edit commit ID. HEAD points symbolically to refs/heads/rename-only because the capture switched branches to make the rename. An ordinary commit on a branch updates that branch’s ref to the new object ID. Checking out another branch changes where HEAD points.

The naming and storage of refs can vary. This example uses loose files. Other repos can store refs in packed-refs or use reftable. Detached HEAD directly names a commit. Reflogs record local ref updates where enabled and are separate from commit history. A commit can remain present in the object database after a ref moves. Its continued retention depends on reachability, reflogs and maintenance policy. Repository layout · Commit object construction · Ref history

Refs and objects from the scratch repository
HEAD                   ref: refs/heads/rename-only
refs/heads/main        d499b2086bb5153af496b73fa17fe714dec14205
main^{tree}            59e4b7846a0bb43797822f482c0c13870dfbd2be
main^                   7a228b1843286ff7cb3fad369f8b2ea9f76667d0

A new commit creates an object and advances a reference. Its parent is a stored link back to history.

Level 06/08Interface

Predict which hashes change

Start from the original ship = no snapshot. Predict the smallest object that must change in each experiment. Then check which IDs propagate up to the root.

For the message case, the capture constructs a second root commit with the same tree, same author and committer, and same timestamps. Only the message changes. This isolates the message from parent changes that would also occur if we simply appended another commit.

Stage ship = yes and commit. Resulting path notes/status.txt.

ObjectBaseline IDResult IDChange
File blob2600a482a9bea5dfe9Different
notes tree9509be911a60ed2b4eDifferent
Root treeb570b88c259e4b7846Different
Commit7a228b184d499b2086Different

Move status.txt to release.txt without changing its bytes. Resulting path notes/release.txt.

ObjectBaseline IDResult IDChange
File blob2600a482a2600a482aSame
notes tree9509be91121be5e626Different
Root treeb570b88c2f31ed6e5bDifferent
Commit7a228b184751cda96bDifferent

Construct the same root snapshot with a different message. Resulting path notes/status.txt.

ObjectBaseline IDResult IDChange
File blob2600a482a2600a482aSame
notes tree9509be9119509be911Same
Root treeb570b88c2b570b88c2Same
Commit7a228b18438f0b5244Different
Each result is compared with the same baseline. The table uses shortened real IDs so the changes are easy to scan.

The edited contents change the blob, which changes both trees, and the edit commit records the new root. The rename keeps the original blob because the content is identical. Its path component changes in the notes tree, which changes the root. The message-only variant changes only the commit object.

git diff --name-status -M reports R100 for the captured rename, meaning it paired those paths with a 100% similarity score. A commit stores the two snapshots through its tree and parent. Rename detection is a comparison between paths in those snapshots. With edits mixed into a move, the similarity score and threshold can affect the result. Diff rename detection

Check yourselfIf the path changes and its bytes stay the same, which object can keep exactly the same ID?

Level 07/08System

Rebuild a blob from the pack

Loose objects are useful for writing one new object at a time. Keeping every object in its own file wastes filesystem entries and misses a compression opportunity. Git can store many objects together in a pack. Some entries store a whole payload. Others store instructions for reconstructing a payload from a similar base. The logical ID stays tied to the reconstructed object.

The fixture uses git pack-objects to write a standalone pack without deleting its loose originals. This pack has 12 entries. Our target is the original ship = no blob. Git chose the later ship = yes blob as its base. That is the staged blob we followed into the edit commit, and it is stored whole in this pack. The pack puts the base before its offset delta even though the base’s commit came later in the history.

Pack header
50 41 43 4b 00 00 00 02 00 00 00 0c
Whole blob
bc 8c 04
Offset delta
e2 01 86 62
Inflated instructions
cc 41 cb 41 09 73 68 69 70 20 3d 20 6e 6f b1 0a c2 20
Rebuilt object
INSERT 9 bytes: "ship = no"
COPY   8386 bytes from base offset 10

Signature PACK, format version 2, 12 entries. These twelve bytes precede the first object.

1 / 5
Step through real bytes from the generated .pack. The capture inflates the base, applies the delta and checks the reconstructed object ID against Git.

From an offset and two instructions to a blob

The entry begins at byte 1499. Its first byte e2 carries a continuation bit, type 6 and the low four bits of size. The next byte 01 adds sixteen, giving an inflated delta length of 18 bytes. The next two bytes 86 62 encode an offset distance of 994 bytes. Subtracting that distance locates the base at byte 505. An OFS_DELTA uses this backward distance. A REF_DELTA instead names its base by object ID.

Inflating the data after that header gives two size prefixes and the instructions. The prefixes say the base has 8,396 bytes and the result has 8,395. Instruction 09 inserts the nine bytes of ship = no. Instruction b1 0a c2 20 copies 8,386 bytes from byte ten of the base. That starts at its newline and preserves the checklist. The reconstructed blob payload can be framed and hashed the same way as the original loose blob.

Captured git verify-pack -v output
751cda96b51a36123abf7fbe53b3e3cd91da5981 commit 238 159 12
d499b2086bb5153af496b73fa17fe714dec14205 commit 74 86 171 1 751cda96b51a36123abf7fbe53b3e3cd91da5981
38f0b524473e3ed159e98a58411b706cd534f1aa commit 206 139 257
7a228b1843286ff7cb3fad369f8b2ea9f76667d0 commit 8 19 396 1 38f0b524473e3ed159e98a58411b706cd534f1aa
59e4b7846a0bb43797822f482c0c13870dfbd2be tree   32 41 415
a60ed2b4e68c859279fa7aa7504dcc839e67ae21 tree   38 49 456
9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3 blob   8396 904 505
b570b88c2cb0f4e8f8c7bea2168aa7a6f647c536 tree   32 41 1409
9509be9115cf1f01b6495140467c6b082aeb9c83 tree   38 49 1450
2600a482ae278854cddbfb7ff76365bd5206f1a2 blob   18 30 1499 1 9bea5dfe9f072ead3c4fdf6c28b7c9cd9b1e87c3
f31ed6e5bce9ab595c4413d53383167f8a33aaf0 tree   32 42 1529
21be5e6269aa24f33b78e799f6a139ade1d9a56f tree   39 50 1571
non delta: 9 objects
chain length = 1: 3 objects
pack/capture-a10e1da05e20d700fbde5ad585e7c5dbe9b6649d.pack: ok

The blob’s delta row above reports 18 inflated bytes of instructions and 30 bytes occupied by the packed entry including the header. The 8,395-byte object comes out only after applying the delta. One chain can contain multiple deltas, so Git must resolve the complete chain to read its target.

The companion pack index, conventionally ending in .idx, locates objects by ID. Its version-2 layout has a fan-out table, sorted IDs, CRCs and offsets. The fan-out narrows the search using the first ID byte. The matching offset locates the object entry in the associated pack. This pack index is separate from the staging index .git/index. Pack and index formats · Reading verify-pack

Level 08/08Interface

Read the evidence in a scratch repository

The captures use git version 2.53.0 and an explicitly initialized SHA-1 repository. Start from the Undefined Stack checkout. The script creates a new scratch directory, uses fixed commit metadata and ignores user-level Git configuration for its fixture. It keeps the scratch repository available for inspection, prints its path on stderr and emits the JSON captures on stdout.

Generate and inspect the scratch repository
node src/dives/git-internals/capture.mjs > /tmp/git-dive-capture.json
# Use the scratch repository path printed on stderr for commands below.
cd /path/to/the/printed/scratch/repository
git log --all --oneline --graph
git ls-tree 7a228b1843286ff7cb3fad369f8b2ea9f76667d0
git cat-file -p d499b2086bb5153af496b73fa17fe714dec14205
git show 7a228b1843286ff7cb3fad369f8b2ea9f76667d0:notes/status.txt | head -n 3
Verify the packed target from the captured run
# Run inside the printed scratch repository
git verify-pack -v pack/capture-*.idx
git --git-dir=packed-only.git cat-file -t 2600a482ae278854cddbfb7ff76365bd5206f1a2
git --git-dir=packed-only.git cat-file -s 2600a482ae278854cddbfb7ff76365bd5206f1a2
git --git-dir=packed-only.git cat-file blob 2600a482ae278854cddbfb7ff76365bd5206f1a2 | git hash-object --stdin

The script copies just the generated pack and its index into a second scratch repository, packed-only.git. Its cat-file must find the object in the pack. Independently, the capture reads the same pack’s bytes, inflates its base and delta, and verifies that the reconstructed bytes match Git’s result. Object hashes should match on compatible Git versions for these fixed inputs. A different packer version or compression configuration can choose a different base or byte offset while preserving the same object IDs.

When a command surprises you, follow the object that owns the changed information. Edits change a blob. Names and modes belong to trees. History and messages belong to commits. A pack changes the representation used to store those objects, while a ref determines which history your branch starts from.

Read the capture script
capture.mjs
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { copyFileSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { inflateSync } from "node:zlib";
import { applyDelta, objectBytes, objectId, readPackEntry } from "./pack.mjs";

const repo = mkdtempSync(join(tmpdir(), "undefinedstack-git-dive-"));
const env = {
  ...Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith("GIT_"))),
  LC_ALL: "C",
  GIT_CONFIG_GLOBAL: "/dev/null",
  GIT_CONFIG_SYSTEM: "/dev/null",
  GIT_AUTHOR_NAME: "Release Bot",
  GIT_AUTHOR_EMAIL: "release@example.test",
  GIT_COMMITTER_NAME: "Release Bot",
  GIT_COMMITTER_EMAIL: "release@example.test",
  GIT_AUTHOR_DATE: "2026-11-19T12:00:00Z",
  GIT_COMMITTER_DATE: "2026-11-19T12:00:00Z",
};
const binary = (...args) => execFileSync("git", ["-C", repo, "--no-pager", ...args], {
  env, timeout: 30_000,
});
const git = (...args) => binary(...args).toString("utf8").trimEnd();
const quiet = (...args) => binary(...args, "-q");

const path = "notes/status.txt";
const renamedPath = "notes/release.txt";
const body = (decision) => [
  `ship = ${decision}`,
  ...Array.from({ length: 120 }, (_, i) => {
    const number = String(i + 1).padStart(3, "0");
    return `check ${number}: verified in environment ${i % 7}; revision ${(i * 137) % 997}; artifact archived`;
  }),
  "",
].join("\n");
const writeDecision = (decision) => writeFileSync(join(repo, path), body(decision));

quiet("init", "--template=", "--object-format=sha1", "-b", "main");
git("config", "index.version", "2");
mkdirSync(join(repo, "notes"));
writeDecision("no");
git("add", "--", path);
quiet("commit", "-m", "Record the release decision");

function snapshot(ref, name) {
  return {
    path: name,
    blob: git("rev-parse", `${ref}:${name}`),
    directory: git("rev-parse", `${ref}:notes`),
    tree: git("rev-parse", `${ref}^{tree}`),
    commit: git("rev-parse", ref),
  };
}

const baseline = snapshot("HEAD", path);
writeDecision("yes");
git("add", "--", path);
writeDecision("maybe");
const line = (value) => value.split("\n")[0];
const index = readFileSync(join(repo, ".git", "index"));
const staged = {
  headLine: line(git("show", `HEAD:${path}`)),
  indexLine: line(git("show", `:${path}`)),
  workLine: line(readFileSync(join(repo, path), "utf8")),
  headOid: baseline.blob,
  indexOid: git("rev-parse", `:${path}`),
  workOid: git("hash-object", path),
  status: git("status", "--short"),
  cachedDiff: git("diff", "--cached", "--", path),
  workingDiff: git("diff", "--", path),
  index: {
    headerHex: index.subarray(0, 12).toString("hex"),
    version: index.readUInt32BE(4),
    count: index.readUInt32BE(8),
    mode: index.readUInt32BE(36).toString(8),
    oid: index.subarray(52, 72).toString("hex"),
    flags: index.readUInt16BE(72),
    path: index.subarray(74, index.indexOf(0, 74)).toString("utf8"),
    checksum: index.subarray(-20).toString("hex"),
    calculated: createHash("sha1").update(index.subarray(0, -20)).digest("hex"),
  },
};
assert.equal(staged.indexOid, staged.index.oid);
git("commit", "-qm", "Approve the release");
const edit = snapshot("HEAD", path);
assert.equal(edit.blob, staged.indexOid);

writeDecision("yes");
quiet("switch", "-c", "rename-only", baseline.commit);
git("mv", "--", path, renamedPath);
git("commit", "-qm", "Move the release note");
const rename = snapshot("HEAD", renamedPath);
const renameDiff = git("diff", "--name-status", "-M", baseline.commit, rename.commit);

const messageCommit = git("commit-tree", baseline.tree, "-m", "Record the release decision, take two");
git("update-ref", "refs/heads/message-only", messageCommit);
const message = snapshot(messageCommit, path);

function object(type, id) {
  const data = binary("cat-file", type, id);
  assert.equal(objectId(type, data), id);
  const framed = objectBytes(type, data);
  const loose = readFileSync(join(repo, ".git", "objects", id.slice(0, 2), id.slice(2)));
  assert.deepEqual(inflateSync(loose), framed);
  return {
    type,
    id,
    length: data.length,
    text: type === "blob" ? data.toString("utf8").split("\n").slice(0, 4).join("\n") : git("cat-file", "-p", id),
    rawHex: framed.subarray(0, 176).toString("hex"),
    omitted: Math.max(0, framed.length - 176),
    rawBytes: framed.length,
    compressedBytes: loose.length,
    loosePath: `.git/objects/${id.slice(0, 2)}/${id.slice(2)}`,
  };
}

const objects = [
  { label: "Blob", ...object("blob", edit.blob) },
  { label: "Directory tree", ...object("tree", edit.directory) },
  { label: "Root tree", ...object("tree", edit.tree) },
  { label: "Commit", ...object("commit", edit.commit) },
];

mkdirSync(join(repo, "pack"));
const baseName = join(repo, "pack", "capture");
const packId = git("pack-objects", "--all", "--delta-base-offset", "--window=10", "--depth=10", "--threads=1", baseName);
const verifyPack = git("verify-pack", "-v", `${baseName}-${packId}.idx`);
const packed = readFileSync(`${baseName}-${packId}.pack`);
const packIndex = readFileSync(`${baseName}-${packId}.idx`);
const entries = verifyPack.split("\n").map((row) => row.match(/^[0-9a-f]{40} /) ? row.split(/\s+/) : null).filter(Boolean);
const deltaRow = entries.find((row) => row[1] === "blob" && row.length === 7);
assert.ok(deltaRow, "Expected a real delta between the two checklist blobs");
const baseRow = entries.find((row) => row[0] === deltaRow[6]);
assert.ok(baseRow && baseRow.length === 5, "Expected a single-hop offset delta");
const delta = readPackEntry(packed, Number(deltaRow[4]));
const base = readPackEntry(packed, Number(baseRow[4]));
assert.equal(delta.baseOffset, Number(baseRow[4]));
const rebuilt = applyDelta(base.data, delta.data);
assert.deepEqual(rebuilt.bytes, binary("cat-file", "blob", deltaRow[0]));
assert.equal(objectId("blob", rebuilt.bytes), deltaRow[0]);
assert.equal(packed.readUInt32BE(8), entries.length);
assert.equal(createHash("sha1").update(packed.subarray(0, -20)).digest("hex"), packId);

quiet("init", "--bare", "--template=", "--object-format=sha1", "packed-only.git");
for (const extension of ["pack", "idx"]) {
  const name = `capture-${packId}.${extension}`;
  copyFileSync(join(repo, "pack", name), join(repo, "packed-only.git", "objects", "pack", name));
}
const packedOnly = binary("--git-dir=packed-only.git", "cat-file", "blob", deltaRow[0]);
assert.deepEqual(packedOnly, rebuilt.bytes);

const output = {
  version: git("--version"),
  objectFormat: git("rev-parse", "--show-object-format"),
  branch: "main",
  baseline, edit, rename, message, staged, renameDiff, objects,
  pack: {
    id: packId,
    version: packed.readUInt32BE(4),
    headerHex: packed.subarray(0, 12).toString("hex"),
    count: packed.readUInt32BE(8),
    length: packed.length,
    indexLength: packIndex.length,
    indexVersion: packIndex.readUInt32BE(4),
    verify: verifyPack.replaceAll(repo + "/", ""),
    packOnlyObjectId: objectId("blob", packedOnly),
    wholeHex: packed.toString("hex"),
    base: { id: baseRow[0], offset: Number(baseRow[4]), headerHex: base.headerHex, bytes: base.data.length, compressedBytes: base.compressedBytes, line: line(base.data.toString("utf8")) },
    delta: { id: deltaRow[0], offset: Number(deltaRow[4]), headerHex: delta.headerHex, bytes: delta.data.length, compressedBytes: delta.compressedBytes, dataHex: delta.data.toString("hex"), line: line(rebuilt.bytes.toString("utf8")), resultLength: rebuilt.resultLength, operations: rebuilt.operations },
  },
  refs: {
    head: readFileSync(join(repo, ".git", "HEAD"), "utf8").trimEnd(),
    main: readFileSync(join(repo, ".git", "refs", "heads", "main"), "utf8").trimEnd(),
    current: git("symbolic-ref", "--short", "HEAD"),
  },
};

console.error(`Scratch repository: ${repo}`);
console.log(JSON.stringify(output, null, 2));

End of Git internals · 8 levels, Idea to System

Where to next

Browse all 39 divesAll topics