Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Practical 0 · A commit with no git commit

Goal: a repository called byhand whose two commits you built yourself, blob by blob, without git add or git commit, with each id predicted before git printed it; then a corrupted object you find with git fsck and repair.

Time: ~35 minutes. Where: your own computer, in ~/git-internals-intro/, in a terminal. Needs git 2.28 or newer and python3 (for ./lab). Read lesson 1 and lesson 2 first.

Task 1 · Get the lab

mkdir -p ~/git-internals-intro
cd ~/git-internals-intro
curl -fo lab http://localhost:8800/git-internals-intro/tools/lab
chmod +x lab
./lab setup p0
What setup did

It checked that git is 2.28 or newer (git init -b needs it) and knows your name and email (every commit needs them), and nothing else: everything in this practical you type yourself. ./lab check p0 only runs read-only git commands in your repository; every command it ran is in lab.log.

Task 2 · An empty repository

Make a repository called byhand in ~/git-internals-intro, with main as its first branch, and go into it.

Predict first: how many files are in .git/objects right now? Does .git/refs/heads/main exist?

Solution
git init -b main byhand
cd byhand
find .git/objects -type f
ls .git/refs/heads

None, and no. .git/HEAD already says ref: refs/heads/main, but there’s no main file yet: HEAD names a branch that doesn’t exist until its first commit.

Task 3 · Blobs, without git add

Store two pieces of content as blobs, with no file in your folder:

  • hello followed by a newline (the file greeting.txt will have this content)
  • # notes followed by a newline (later, docs/notes.md)

Predict first: the id of the first blob. You’ve seen it.

Hint

git hash-object prints an id; -w also writes the object; --stdin reads the content from a pipe.

Solution
echo 'hello' | git hash-object -w --stdin
printf '# notes\n' | git hash-object -w --stdin
ce013625030ba8dba906f756967f9e9ca394464a
7c88e8a591766f27635a5bdab01832a59b04cf2e

The first is lesson 1’s id exactly: a blob’s id depends only on its bytes, so it’s the same in every repository on every machine. Check that the two objects exist with find .git/objects -type f.

Task 4 · A tree and a commit

Make a commit whose snapshot is one file, greeting.txt, an ordinary file (mode 100644) holding the first blob. Message: Built by hand.

CommandWhat it does
git update-index --add --cacheinfo <mode>,<id>,<path>puts an entry in the index, the list git builds the next tree from (what git add fills)
git ls-files --stageprints the index
git write-treewrites a tree object from the index, prints its id
git commit-tree <tree> -m <message>writes a commit object for that tree, prints its id; moves no branch

Predict first: the tree’s id. (Lesson 1’s first commit had exactly this snapshot.)

Solution
git update-index --add --cacheinfo 100644,ce013625030ba8dba906f756967f9e9ca394464a,greeting.txt
git ls-files --stage
git write-tree
100644 ce013625030ba8dba906f756967f9e9ca394464a 0	greeting.txt
57e9529754dc514a3ec10db2ff882018fbe1fcbf
git commit-tree 57e9529754dc514a3ec10db2ff882018fbe1fcbf -m 'Built by hand'

The tree is 57e9529, the same as lesson 1’s: a tree’s content is its entries, and the entries are the same. The 0 in ls-files --stage is the stage number, which is only something else during a merge conflict.

The commit’s id is not lesson 1’s, and not your neighbour’s: it contains your name, email and the current time. Copy it from what you see; it’s <commit> below.

git log --oneline main now fails with unknown revision: the commit exists, but no branch names it.

Task 5 · Make it a branch

Point main at your commit, without git commit, git reset or editing files under .git by hand.

Hint

git update-ref <ref> <id> writes a ref safely: it locks the file and adds a line to the reflog.

Solution
git update-ref refs/heads/main <commit>
git log --oneline
git status --short

git log shows your commit. git status --short says:

 D greeting.txt

Predict first: why “deleted”? Which of the three places, the store, the index and your folder, lacks greeting.txt?

What you see

Your folder. The commit and the index both have greeting.txt, but you never made the file: the blob went straight into the store. Bring the folder in line with the commit:

git restore greeting.txt
cat greeting.txt
hello

Task 6 · A second commit, with a folder

Make a second commit on main whose snapshot adds docs/notes.md holding the second blob. Message: Add notes. Its parent is the first commit. Then make your folder match it.

Hint

Add the entry to the index with its full path; write-tree makes the docs tree for you. commit-tree takes the parent with -p, and -p main works. Then update-ref again.

Solution
git update-index --add --cacheinfo 100644,7c88e8a591766f27635a5bdab01832a59b04cf2e,docs/notes.md
git write-tree
git cat-file -p 618fcd781e1ab99dacdb0cc0cb55df0bcc6a1760
git commit-tree 618fcd781e1ab99dacdb0cc0cb55df0bcc6a1760 -p main -m 'Add notes'
git update-ref refs/heads/main <the id commit-tree printed>
git restore .
git log --oneline

The root tree now has a tree entry for docs:

040000 tree e958723d91d4d067ace5d4b9538d767a2bc25e6a	docs
100644 blob ce013625030ba8dba906f756967f9e9ca394464a	greeting.txt

040000 is the mode of a directory. Now look at the branch’s reflog:

git reflog main

Two lines, your two commit ids, and nothing after the colon:

22b548d main@{0}: 
1d8b58e main@{1}: 

update-ref wasn’t given a reason (-m sets one); git commit would have written commit: Add notes. That’s how the checker knows you didn’t use it.

Task 7 · Break it: a corrupted object

Objects are files; files get damaged (a bad disk, a sync tool, someone “cleaning up” .git). Damage the greeting.txt blob on purpose, keeping a copy first. Objects are read-only, so chmod first:

cp .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a ../blob-backup
chmod u+w .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
printf 'junk' > .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
git cat-file -p ce01362
error: inflate: data stream error (incorrect header check)
error: unable to unpack ce013625030ba8dba906f756967f9e9ca394464a header
fatal: Not a valid object name ce01362

inflate is zlib’s decompression: the file isn’t zlib data any more. Find everything that’s wrong the way you would in a real repository:

  1. git fsck --full checks every object and every link between them. It names the file and what it expected:

    ...
    error: ce013625030ba8dba906f756967f9e9ca394464a: object corrupt or missing: .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
    missing blob ce013625030ba8dba906f756967f9e9ca394464a
    
  2. Which files does it break? git ls-tree -r main and git ls-tree -r main~1 list every path with its blob id: greeting.txt in both commits is that blob.

  3. Repair: the right bytes are anywhere with the same id: your backup, a teammate’s clone, the remote. In a clone the object is usually packed, so there’s no file to copy: remove the damaged file first (git won’t overwrite an object file that exists, even a broken one), then git -C <their repo> cat-file blob ce01362 | git hash-object -w --stdin writes it back. fsck checks that the bytes hash to their id. Put your backup back and check again:

    cp ../blob-backup .git/objects/ce/013625030ba8dba906f756967f9e9ca394464a
    git fsck --full; echo "exit $?"
    

Now delete it (rm that file) and run git fsck --full once more: only missing blob ce01362..., exit code 2 instead of 3. Put the backup back again.

Check your work

cd ~/git-internals-intro
./lab check p0

All 11 checks should pass.

Go deeper: what git commit does

What you did in tasks 4 to 6 is most of git commit: write-tree from the index, commit-tree with HEAD’s commit as parent, then update-ref of the branch HEAD names, with a reflog reason (commit: Add notes). git add is hash-object -w plus update-index.

The rest of git commit is hooks, the editor for the message, the summary it prints, and a check that may start git gc on its own when the repository has grown (git-gc): lesson 3 covers what that removes. The Git Book walks through the same plumbing in Git Objects.

Next: lesson 3 · Getting lost work back, and when it really is gone.