Sub topic 7
Trees: directories, names and modes
This chapter assumes you can run Git commands in a Unix-style shell. It also assumes you know that a blob stores file content and that git hash-object and git cat-file write and read objects. Here you will see how Git adds file names and directory structure on top of blobs.
Why blobs need trees
A blob holds only bytes. It has no file name and no permissions. A tree is the object that supplies them. Each tree represents one directory, and each entry in it has a mode, a type, a hash and a name.
Build a nested tree
Create a repository with one file at the top level and one in a subdirectory. Then stage both files and write the index out as a tree.
git init tree-demo
cd tree-demo
echo 'hello' > hello.txt
mkdir docs
echo 'notes' > docs/notes.txt
git update-index --add hello.txt docs/notes.txt
tree=$(git write-tree)
git cat-file -p "$tree"The output has this shape (the hash of docs will differ from the placeholder):
040000 tree <hash-of-docs> docs
100644 blob ce013625030ba8dba906f756967f9e9ca394464a hello.txtgit write-tree created two tree objects. One is the root tree. The other is the tree for docs, which the root tree refers to by hash.
Reading trees
git ls-tree lists the entries of a tree. The options below change how much it shows.
git ls-tree "$tree"
git ls-tree -r "$tree"
git ls-tree -r -t "$tree"
git ls-tree -l "$tree"- With no options,
ls-treelists one level only. -rrecurses into subtrees and prints only blobs.-r -trecurses and also prints the tree entries it passes through.-ladds the size of each blob.
You can also ask for a path inside a tree, for example git cat-file -p "$tree:docs". The <tree>:<path> form resolves to the object at that path.
The mode field
The mode tells Git what kind of thing an entry is.
| Mode | Type | Meaning |
|---|---|---|
| `100644` | blob | Regular file |
| `100755` | blob | Executable file |
| `120000` | blob | Symbolic link. The blob content is the link target. |
| `160000` | commit | Submodule. The entry points at a commit in another repository. |
| `040000` | tree | Directory |
Git records only whether a file is executable. It does not store owner, group, timestamps or other permission bits. That is why ls-tree prints 040000 for directories, with a leading zero.
What a tree contains
A tree is a list of entries sorted by name. Each entry holds the mode, the name and the hash of the object it points to. Nothing else is stored. A tree has no name of its own, because the parent tree supplies it. The root tree has no name at all. A commit points to it directly.
Because a tree's hash is computed from its content, two directories with identical files, names and modes produce the same tree. Git stores that tree once. If you change one file, only the blob for that file and the trees on the path from it to the root get new hashes. Every other object is reused.
Common mistakes
- Expecting
ls-treeto recurse by default. It shows one level unless you pass-r. - Passing a path with a trailing slash and getting the contents rather than the directory entry.
git ls-tree "$tree" docsshows thedocsentry.git ls-tree "$tree" docs/shows what is inside it. - Assuming a mode change is invisible. Making a file executable changes its mode, so the tree hash changes even though the blob does not.
Exercise
You commit two directories, a/ and b/, each containing a file x.txt with exactly the same content and both with mode 100644. How many blob objects and how many tree objects does Git need to store for these two directories, not counting the root tree? Why?
Show answer
One blob and one tree. Both files have identical content, so they hash to the same blob. Both directories contain the same entry (mode 100644, name x.txt, same blob hash), so they hash to the same tree. The root tree then has two entries, a and b, that point at that one shared tree.