Section 13.19

Git commit and working tree

Definition

A Git commit is an unchangeable snapshot of every file Git tracks in a project, with its author, time, message and the commit or commits it follows. Git, whose name is not an abbreviation, is a version-control system: a program that keeps the history of a set of files. A commit is named by a cryptographic hash §5.1 of its content, a short value that changes if any byte changes. The working tree is the directory of files that people edit and a build reads; it can differ from every commit.

The idea

A test report states that the measured program was built from commit 3f9c2a1. Before compiling, a developer had changed one line of the header config.h from a delay of 2 ms to 20 ms without committing the change. The measured program waits 20 ms; anyone who later checks out commit 3f9c2a1, meaning copies its files into a working tree, and builds it gets a program that waits 2 ms. The build also read a header that a script had generated and Git had been told to ignore, so no commit contains it. The report’s label names recorded content; the compiler read the working tree.

A photograph and a room are a fair picture. A commit is a photograph of the tracked files, filed under a name computed from the photograph itself, so the name cannot be attached to another picture. The working tree is the room, which people keep changing. Git can compare the room with a photograph, but only for the items it was told to track, and it never learns which items the build touched. The picture stops being accurate in one respect: a photograph can be destroyed, whereas a commit copied to other repositories cannot be withdrawn, which matters when it contains a secret.

How it works

Git keeps three places apart.

Every object is named by a hash of its content. Git computes SHA-1 (Secure Hash Algorithm §14.1 1) over a short header followed by the content; the header is the object type, a space, the content length in decimal digits and a zero byte. The result is written as 40 hexadecimal digits. A repository can instead use SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output). A tree contains the identifiers of its entries, and a commit contains the identifiers of its tree and its parents. A change to one byte of one file therefore changes its blob identifier, every tree identifier above it and the commit identifier: a commit identifier fixes every tracked file and the whole history before it. This chaining of hashes is the idea of a Merkle tree §5.43.

Branches, tags and HEAD are references, names that point at a commit. A branch moves forward with every commit made on it, and HEAD names the branch or commit currently checked out, so a branch name can identify a different commit tomorrow. Only the full commit identifier names one snapshot; a short prefix of it, such as 7 hexadecimal digits, can become ambiguous as a repository grows.

The command git status makes three comparisons.

  1. The tree of the HEAD commit against the index, reported as changes to be committed.

  2. The index against the working tree, reported as changes not staged for commit: tracked files that were modified or deleted.

  3. The working tree against the index, reported as untracked files: files that the index does not list. A file that matches a pattern in a .gitignore file is ignored and, by default, not reported at all.

Generated files and build outputs are usually listed in .gitignore, so a status with nothing to report says nothing about them.

Each file a build read is therefore in one of five classes: committed and unchanged (tracked, and its bytes hash to the blob that the commit records §8.21 for its path); modified (tracked, with different bytes); untracked (in the working tree but not recorded); ignored (untracked and matched by .gitignore, typically a generated file); or external (outside the working tree, such as the compiler, system headers, libraries and downloaded packages). Only the first class is identified by the commit. The list of files a build read comes from the compiler’s dependency output or from a trace of the files the build opened, as the source closure §13.6 entry describes. A submodule, a repository nested inside another, appears in the outer commit only as one commit identifier of the inner repository, whose working tree can differ from that commit.

Worked example

A build reads three project files: main.c; config.h; and gen.h, which a script writes and .gitignore lists. Commit CC records the first two. In CC, config.h holds the line #define DELAY_MS 2 and a newline: 7+1+8+1+1+1=197 + 1 + 8 + 1 + 1 + 1 = 19 bytes for the word #define, a space, the name, a space, the digit and the newline.

Git hashes the header blob 19 and a zero byte, 7+1=87 + 1 = 8 bytes, followed by the 19 bytes of content, 8+19=278 + 19 = 27 bytes in all. The SHA-1 result is the blob identifier

58fd1cf308d4aeebdb5d1f01a6ab25386d64ab7a

A developer changes the 2 to 20 and builds without committing. The file now has 20 bytes, the header becomes blob 20, and the identifier is

8b652b2e9b0cea2bbd60f727cc2924e520cda994

One added byte gave an unrelated identifier, so comparing identifiers detects the change.

Against CC, main.c hashes to the recorded blob, so it is committed and unchanged. config.h hashes to 8b652b2e… where CC records 58fd1cf3…, so it is modified, and git status reports it. gen.h is in no commit and is ignored, so git status is silent about it. The compiler and the system headers are external. Only 1 of the 3 project files is identified by CC. A version label of the form that git describe --dirty prints, the commit’s name followed by -dirty, compares tracked files only: it flags config.h but can never flag gen.h.

The measured program waits 20 ms and depends on gen.h. A rebuild from CC waits 2 ms and either fails for lack of gen.h or uses whatever gen.h the rebuilding machine happens to have.

Stated precisely

Let HH be the repository’s hash function, SHA-1 or SHA-256. For an object whose type word is tt (blob, tree, commit or tag) and whose content cc is nn bytes long, id=H(t‖a space‖dec(n)‖𝟶𝚡𝟶𝟶‖c),\mathrm{id} = H\bigl(t \mathbin{\Vert} \text{a space} \mathbin{\Vert} \mathrm{dec}(n) \mathbin{\Vert} \mathtt{0x00} \mathbin{\Vert} c\bigr), where dec(n)\mathrm{dec}(n) is nn written in decimal digits and a‖ba \mathbin{\Vert} b means the bytes of aa followed by the bytes of bb.

Let II be the set of files the build read and RR the set of files inside the working tree. For a commit CC, let TC(f)T_C(f) be the blob identifier that CC records for the path of file ff, undefined when CC does not track that path, and let b(f)b(f) be the blob identifier of the bytes of ff as the build read them. The statement “the build used the source of commit CC” holds exactly when TC(f) is defined and TC(f)=b(f)for every f∈I∩R.T_C(f) \text{ is defined and } T_C(f) = b(f) \quad \text{for every } f \in I \cap R. A file in II but not in RR lies outside the statement and needs its own identity, such as a digest recorded with the build.

The statement rests on three assumptions.

  1. II is complete: no file the build read is missing from it.

  2. The check and the build saw the same bytes: nothing changed a file between the comparison and the compiler’s read.

  3. HH is collision-resistant §5.5: nobody can find two different contents with the same identifier.

Objects outlive their branch

An object is reachable when a chain of identifiers leads to it from a reference: from the reference to a commit, from each commit to its parents and its tree, and from each tree to its entries. Git never edits an object. Commands said to rewrite history, such as amending (replacing) the last commit or rebasing a branch (replaying its commits onto another starting point), write new commits with new identifiers and move the branch to them; the old commits stay in the object database, unreachable from the branch.

Each repository also keeps a reflog, a local log of the commits that each reference and HEAD have pointed to. It keeps old commits reachable: by default for 90 days, or for 30 days when a commit is no longer reachable from the reference’s current value. Pushing and cloning never copy the reflog. An object that nothing reaches is deleted by the housekeeping command git gc (the letters stand for garbage collection) once it is older than a grace period §9.21, two weeks by default.

Copies spread the other way. A clone copies every object reachable from the branches and tags it takes, and a push copies new objects to another repository and moves references there. A forced push can move a server’s branch to a rewritten history, but it cannot delete the objects that clones, forks, mirrors and backups already hold, and a hosting service can keep old commits in references of its own or in caches.

Limits and common mistakes

A commit identifier covers tracked files only. Untracked, ignored and generated files, the toolchain §12.2 and downloaded dependencies lie outside it.

git status compares; it does not trace. It cannot say which files the build read, it hides ignored files unless given --ignored, and it describes only the moment it ran, so a clean status taken before the build is a time-of-check to time-of-use §9.26 (TOCTOU) gap. Building from a fresh checkout of the commit in an empty directory, with generated files produced by recorded steps, removes these gaps for every file inside the working tree.

Git can convert bytes between the stored blob and the working file, for example line endings or the output of a configured filter. The file on disk can then differ from the blob while git status reports no change, and the compiler reads the file on disk.

A signed commit carries a digital signature §5.14, which shows that a holder of the signing key made the commit. It shows neither that the code is correct nor that a particular build used it.

Git’s default hash, SHA-1, is no longer collision-resistant: a collision was published in 2017 [312]. Git detects inputs built with the published attack technique, and a repository that needs a stronger hash can use SHA-256.

Deleting a file in a new commit changes no earlier commit. A secret that was ever committed stays in the history and, once pushed, in every copy.

Questions answered

Was the measured source clean, modified, generated, or externally supplied?

The answer has to be established file by file, for every file the build read; a commit identifier alone cannot give it, because a Git commit records only the tracked files while the build reads the working tree, the files on disk. A file is clean when Git tracks it and its bytes hash to the blob (stored file version) that the commit records for its path; modified when it is tracked but its bytes differ; generated or untracked when it is in the working tree but in no commit, and if it is listed in .gitignore, as generated files usually are, git status does not even mention it; and externally supplied when it lies outside the working tree, like the compiler, system headers and downloaded libraries.

To decide, take the list of files read from the compiler’s dependency output or a file-access trace, compare each tracked file with the commit at the time of the build, and record a digest of every other file. Building from a fresh checkout in an empty directory, with generated files made by recorded steps and external tools identified by digest, makes the answer “clean” true by construction.

A key was committed and later deleted. Where can it still be found, and why does rewriting history not help once the branch was pushed?

The key is still in every commit from the one that added it up to the one that deleted it, because deleting a file makes a new commit and changes no existing one. Once the branch was pushed, the key is also on the server and in every clone, fork, mirror and backup made since, and the hosting service may keep it in references of its own or in caches. Locally, even after a rewrite, the reflog (a log of earlier branch positions) keeps the old commits for weeks.

Rewriting history does not help because Git never edits an object. A rewrite writes new commits and moves the branch, and a forced push moves the server’s branch the same way, but neither can delete the copies that others already hold, and nobody can tell who made copies. Treat the key as disclosed: revoke it and replace it everywhere, which is key revocation §5.32 followed by key rotation §5.29. A rewrite afterwards only slows further spread.

Related entries: Source closure (§13.6), Artifact provenance (§13.1), Toolchain (§12.2), Reproducible build (§13.8), Key revocation (§5.32), Key rotation (§5.29). Further reading: [135, 136, 312].