17

Git tracks files, not directories, and we cannot currently add empty directories (The standard trick does not add an empty directory, but instead a file within, see also the FAQ).

Nevertheless, a git repo may contain an empty directory. So what happens I clone a repo or checkout a branch containing an empty directory? Is it created in the work tree?

1 Answer 1

21

To setup a test repository with an empty directory, you can use the following script (run it in a temporary directoy).

When checking out, however, the empty directory is ignored. Here too, Git only works with files, not directories, even if the latter are present.

Github and tig display empty directories, gitk does not.

#!/bin/bash
set -e

echo ---- initialize the repo...
#rm -rf .git
git init

echo ---- create an empty tree object...
empty_tree=$(git mktree < /dev/null)

echo ---- create a parent tree containing the empty subtree...
root_tree_ls=$(echo -e 040000 tree $empty_tree\\tvacuum)
root_tree=$(echo "$root_tree_ls" | git mktree)

echo ---- commit...
commit_msg="This commit contains an empty directory"
commit=$(git commit-tree $root_tree -m "$commit_msg")

echo ---- create a branch...
git branch master $commit

# output the ids:
echo ----------------------------------------------------
echo "empty tree:" $empty_tree
echo "root tree: " $root_tree
echo "commit:    " $commit
2
  • 1
    This indeed adds the empty tree to the repository. But cloning the repo does not create the empty directory in your working tree.
    – manu
    Dec 24, 2013 at 22:49
  • 1
    for non-believers: git archive master | tar tv :-) Nov 21, 2019 at 10:07

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.