Sub topic 7
Trees: directories, names and modes
This chapter assumes you have read the earlier chapters on blobs, git hash-object and git cat-file. You should be able to write a blob and read it back by its hash. A blob holds file contents and nothing else, so it has no file name and no permissions. A tree adds those.
What a tree stores
A tree object is one directory listing. Each entry has four fields:
| Field | Meaning | Example |
|---|---|---|
| mode | File type and permission class | `100644` |
| type | Kind of object the entry points to | `blob` |
| hash | Object ID of the target | `ce013625...` |
| name | One path component, not a full path | `hello.txt` |
Names in a tree are single path components. The tree for docs/notes.txt does not contain the string docs/notes.txt. The root tree has an entry named docs that points to a second tree, and that second tree has an entry named notes.txt.
A tree also does not know its own name. The parent entry supplies it. This is why two directories with identical contents share one tree object, whatever they are called.
Modes
The mode tells Git how to treat the target when it writes a working directory.
| Mode | Meaning | Target type |
|---|---|---|
| `100644` | Regular file | blob |
| `100755` | Executable file | blob |
| `120000` | Symbolic link (the blob holds the link target path) | blob |
| `040000` | Directory | tree |
| `160000` | Gitlink, used by submodules | commit |
Git records only the executable bit. A tree stores no owner, group, timestamps or other permission bits. The directory mode is printed as 040000 by git ls-tree, but the raw stored form is 40000 with no leading zero.
A gitlink entry points to a commit in another repository. That commit does not exist in this object database, so the entry is an exception to the rule that every hash in a tree resolves locally.
Building a tree directly
You can create a tree without touching the index by using git mktree. It reads lines in git ls-tree format and writes a tree object. Start with a fresh repository and two blobs.
git init tree-demo
cd tree-demo
a=$(echo 'hello' | git hash-object -w --stdin)
b=$(printf '#!/bin/sh\necho hi\n' | git hash-object -w --stdin)
tree=$(printf '100644 blob %s\thello.txt\n100755 blob %s\trun.sh\n' "$a" "$b" | git mktree)
echo "$tree"
git cat-file -t "$tree"
git cat-file -s "$tree"
git cat-file -p "$tree"The type is tree. On a SHA-1 repository the size is 71 bytes. Each entry is the mode as text, a space, the name, a NUL byte and the raw 20-byte hash. For hello.txt that is 6 + 1 + 9 + 1 + 20 = 37 bytes, and for run.sh it is 34 bytes.
Because tree entries hold hashes in binary, git cat-file -p formats them for you. To see the stored bytes, ask for the raw object:
git cat-file tree "$tree" | od -c | headYou will see 100644 hello.txt, a NUL, and then bytes that are not printable text. That is the raw hash.
Nested trees
A directory is a tree entry whose target is another tree. Build docs/notes.txt from the bottom up. Write the subtree first, because the parent needs its hash.
n=$(echo 'notes' | git hash-object -w --stdin)
sub=$(printf '100644 blob %s\tnotes.txt\n' "$n" | git mktree)
root=$(printf '100644 blob %s\thello.txt\n040000 tree %s\tdocs\n100755 blob %s\trun.sh\n' "$a" "$sub" "$b" | git mktree)
git ls-tree "$root"The output lists docs, hello.txt and run.sh in that order, each with its mode, type and hash. The result is one root tree, one subtree and three blobs.
Inspecting trees
git ls-tree is the main reader. These options matter most.
git ls-tree "$root"
git ls-tree -r "$root"
git ls-tree -r -t "$root"
git ls-tree -l "$root"
git ls-tree --name-only "$root"
git ls-tree "$root" docs/-rrecurses into subtrees and lists only the leaf entries. Subtree entries are hidden.-r -trecurses and also prints the subtree entries.-ladds the blob size. Tree entries show-in the size column.--name-onlyprints only names.- A path argument limits the output.
docs/with a trailing slash lists what is insidedocs.
You can also name a path inside a tree with the tree:path syntax, which resolves each component for you:
git cat-file -p "$root:docs/notes.txt"
git rev-parse "$root:docs"The second command prints the hash of the docs subtree, which matches $sub.
Sorting and why it matters
Entries in a tree are sorted by name, compared as raw bytes. Directories compare as if their name ended with /. This detail changes the order in one common case. The byte . (0x2E) sorts before / (0x2F), and 0 (0x30) sorts after it. So a directory foo lands between foo.txt and foo0.
Sorting is not a cosmetic choice. Git hashes the exact bytes of the tree, so two tools that order entries differently would produce different hashes for the same directory. The order must be canonical. A tree with out-of-order or duplicate names is malformed, and git fsck reports it.
Sharing and identity
A tree's hash depends only on its entries. This has three consequences.
- Two files with the same content share one blob, even under different names. A tree may list the same blob hash twice.
- An unchanged directory keeps the same tree hash from one commit to the next. A new commit reuses the old subtree objects for everything it did not touch.
- Changing one file changes every tree on the path from that file up to the root. A change to
docs/notes.txtcreates a new blob, a newdocstree and a new root tree. Thehello.txtandrun.shentries in the new root still point to the old blobs.
Git does not store renames. If you move hello.txt to greeting.txt, the new tree has a different name next to the same blob hash. Tools such as git log --follow and git diff -M infer a rename by comparing trees afterward.
Edge cases
- Empty directories cannot be stored. A tree entry for a directory must point to a tree, and Git has no reason to write a tree with no files. The usual workaround is a placeholder file such as
.gitkeep. - The empty tree exists. A tree with zero entries is valid.
git hash-object -t tree /dev/nullprints its hash,4b825dc642cb6eb9a060e54bf8d69288fbee4904in a SHA-1 repository. Tools use it as a base to diff against. - Unusual names. Names are bytes and may contain spaces or newlines.
git ls-treequotes unusual names by default. Add-zto get NUL-terminated records that are safe for scripts. - Old modes. Some old repositories contain
100664entries. Current Git does not write them, butgit fsckmay warn about them. - Case. Names are case-sensitive in the tree, even on a case-insensitive file system.
READMEandreadmeare two different entries.
Checking your understanding
Exercise
A directory contains a subdirectory foo, a file foo.txt and a file foo0. In what order do the three entries appear in the tree object?
Show answer
foo.txt, then foo, then foo0. Directories sort as if their name ended with /, so foo compares as foo/. The byte . (0x2E) sorts before / (0x2F), and 0 (0x30) sorts after it.
Exercise
You edit only docs/notes.txt in the tree built in this chapter and snapshot the result. Which objects are new, and which entries in the new root tree keep their old hashes?
Show answer
A new blob for the edited file, a new docs tree that points at it, and a new root tree that points at the new docs tree. The hello.txt and run.sh entries in the new root keep their old blob hashes, because those blobs did not change.
Exercise
What command prints the hash of the docs subtree from the root tree in $root, without listing the whole tree?
Show answer
git rev-parse "$root:docs". The tree:path syntax resolves the path component by component and prints the object ID it ends at.