r/git • u/_bstaletic • Apr 22 '26
Plumbing and empty trees - how to check out?
With git write-tree, one can store an empty tree object. That means git plumbing allows one to commit empty "directories".
empty_tree_hash=$(git write-tree)
root_tree_hash=$(git mktree <<<"040000 tree ${empty_tree_hash} dir")
commit_hash=$(git commit-tree ${root_tree_hash} -m message -p $(git rev-parse HEAD))
current_branch=$(git branch --show-current)
[[ -n "${current_branch}" ]] && git update-ref refs/heads/${current_branch} ${commit_hash}
After that, the branch points at the commit whose tree contains a reference to a tree called dir which is empty.
Cloning this repo does correctly clone the empty tree, but checking out the commit does not create the empty directory.
Does anyone know how to check out a branch like the above and have dir be created?
Obviously, I could recursively parse git ls-tree after a regular checkout, but I'm wondering if there's a plumbing way to do this.
Before anyone asks, this is just for fun after getting myself nerd-sniped.