Git:仓库包含一个空目录-会发生什么?

17

Git 跟踪文件而非目录,因此我们目前无法添加空目录标准技巧不会添加空目录,而是在其中添加一个文件,还请参阅FAQ)。

尽管如此,git repo 可能包含空目录。那么,如果我克隆一个包含空目录的 repo 或者检出一个分支,会发生什么?它会在工作树中创建吗?

1个回答

21

要创建一个带有空目录的测试仓库,您可以使用以下脚本(在临时目录中运行)。

但是,在检出时,空目录将被忽略。这里,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

1
这确实将空树添加到存储库中。但是克隆存储库不会在您的工作树中创建空目录。 - manu
1
对于那些不信的人:git archive master | tar tv :-) - Michał Šrajer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接