【问题标题】:Git: repo contains an empty directory - what happens?Git:repo 包含一个空目录 - 会发生什么?
【发布时间】:2012-07-21 00:00:50
【问题描述】:

Git tracks files, not directories,我们 cannot currently add empty directoriesstandard trick 不会添加一个空目录,而是添加一个文件,另见FAQ)。

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

【问题讨论】:

    标签: git directory


    【解决方案1】:

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

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

    【讨论】:

    • 这确实将空树添加到存储库中。但是克隆 repo 不会在您的工作树中创建空目录。
    • 对于非信徒:git archive master | tar tv :-)
    猜你喜欢
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    相关资源
    最近更新 更多