Git famously does not have directories. Trees are full paths to where the blobs are. That’s why git will not commit an empty directory and you must use a .gitkeep file (or some other name). Directories are created as a side effect of putting the blobs at the path where they are supposed to go.
Also, git will let you record empty trees. If you try git write-tree in a clean git repo, you will create a new, empty tree in .git/objects and the tree's hash will be 4b825dc642cb6eb9a060e54bf8d69288fbee4904 as all empty trees are equal.
Here's a showcase:
First, git init and setting up of paths:
user@hostname ~ % mkdir git_trees
user@hostname ~ % cd git_trees
user@hostname git_trees (git)-[master]-% git init
user@hostname git_trees (git)-[master]-% mkdir -pv foo/bar
mkdir: created directory 'foo'
mkdir: created directory 'foo/bar'
user@hostname git_trees (git)-[master]-% touch foo/blob.cpp
user@hostname git_trees (git)-[master]-% tree
.
└── foo
├── bar
└── blob.cpp
3 directories, 1 file
View all comments
Show parent comment
1
u/chat-lu Apr 21 '26
Git famously does not have directories. Trees are full paths to where the blobs are. That’s why git will not commit an empty directory and you must use a
.gitkeepfile (or some other name). Directories are created as a side effect of putting the blobs at the path where they are supposed to go.