15

Git跟踪文件,而不是目录,我们目前不能add清空目录标准技巧不会添加空目录,而是添加一个文件,另请参阅常见问题解答)。

然而,一个 gitrepo可能包含一个空目录。那么我克隆一个 repo 或检出一个包含空目录的分支会发生什么?它是在工作树中创建的吗?

4

1 回答 1

18

要使用空目录设置测试存储库,您可以使用以下脚本(在临时目录中运行它)。

但是,在签出时,将忽略空目录。在这里,Git 也只适用于文件,而不是目录,即使后者存在。

Github 和tig显示空目录,gitk没有。

#!/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
于 2012-07-22T13:58:49.550 回答