Git:repo 包含一个空目录 - 会发生什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11600871/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git: repo contains an empty directory - what happens?
提问by tiwo
Git tracks files, not directories, and we cannot currently add
empty directories(The standard trickdoes not add an empty directory, but instead a file within, see also the FAQ).
Git跟踪文件,而不是目录,我们目前不能add
清空目录(标准技巧不会添加空目录,而是添加一个文件,另见常见问题解答)。
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?
然而,一个 gitrepo
可能包含一个空目录。那么我克隆一个 repo 或 checkout 一个包含空目录的分支会发生什么?它是在工作树中创建的吗?
回答by tiwo
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.
但是,在签出时,会忽略空目录。在这里,Git 也仅适用于文件,而不适用于目录,即使后者存在。
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