Sub topic 6
Trees: directories, names, and modes
This chapter assumes you know what a blob is and how git cat-file works from earlier chapters. It also assumes you can use a Unix-style shell.
What a tree stores
A blob holds file contents and has no name. A tree object adds the names. It is a directory listing, and each entry has a mode, a type, an object ID, and a name. The entry points to either a blob (a file) or another tree (a subdirectory).
Because trees can contain trees, one root tree describes a whole project snapshot. A commit points to that root tree.
Inspecting a tree
Create a small repository with a file in a subdirectory. Git needs user.name and user.email configured before it can commit.
git init trees-demo && cd trees-demo
mkdir src
echo 'hello' > greeting.txt
echo 'echo hi' > src/run.sh
chmod +x src/run.sh
git add .
git commit -m 'add files'
git cat-file -p 'HEAD^{tree}'
git cat-file -p 'HEAD:src'
git ls-tree -r HEADThe first cat-file prints the root tree. It has two lines, one per entry, in this form:
100644 blob <id> greeting.txt
040000 tree <id> srcThe second command reads the src tree and shows 100755 blob <id> run.sh. The -r option of ls-tree walks down into subtrees and lists every file with its full path.
Modes
The mode is a number that tells Git what kind of entry it is. Git records only a few values.
| Mode | Meaning |
|---|---|
| 100644 | Regular file |
| 100755 | Executable file |
| 120000 | Symbolic link |
| 040000 | Subdirectory (a tree) |
| 160000 | Submodule (a commit in another repository) |
Git does not store full Unix permissions, owners, or timestamps. The only permission it tracks is whether the executable bit is set. This is why chmod +x shows up in a diff as a mode change from 100644 to 100755, even when the content is the same.
Names live in the tree
A rename changes the tree and leaves the blob alone. If you rename greeting.txt, the new tree has the same blob ID under a new name. Two files with identical content in different places also share one blob, because content addressing gives them the same ID.
Trees deduplicate in the same way. If a directory is identical in two commits, both root trees point to the same subtree ID. Git stores it once and can skip comparing it.
Two consequences follow:
- Git cannot store an empty directory, because a tree entry exists only for something with content. A common workaround is a placeholder file such as
.gitkeep. - Entries are sorted by name, so the same set of files always produces the same tree ID.
Common errors
- Reading
040000as a typo.ls-treeprints it with a leading zero for tree entries, and that is correct. - Expecting a file's name in
git cat-file -p <blob>. The blob has no name, so look in the tree that references it. - Expecting
git addto record an empty folder. It records nothing for it.
Exercise
You rename src/run.sh to src/start.sh without changing its content. Which objects are new in the next commit, and which are reused?
Show answer
The blob for the script is reused, because its content and therefore its ID are unchanged. The src tree is new, because one of its entry names changed. The root tree is new, because it points to the new src tree ID. The commit is also new. The greeting.txt blob is reused.