我如何将一个空的目录(不包含文件)添加到一个Git存储库中?


当前回答

输入: https://gitlab.com/empty-repo/empty.git

一个 Git 存储库与正确的一个承诺:

commit e84d7b81f0033399e325b8037ed2b801a5c994e0
Author: Nobody <none>
Date: Thu Jan 1 00:00:00 1970 +0000

使用

要添加一个空的目录给你 GIT repo:

git submodule add https://gitlab.com/empty-repo/empty.git path/to/dir

将所有现有空白目录转换为子模块:

find . -type d -empty -delete -exec git submodule add -f https://gitlab.com/empty-repo/empty.git \{\} \;

Git 在创建子模块参考时将存储最新的 commit hash,所以你不必担心我(或GitLab)使用此注射恶意文件。 不幸的是,我没有找到任何方法来强制该 commit ID 在查询期间使用,所以你将不得不手动检查该参考 commit ID 是e84d7b81f0033399e325b8037ed2b801a5c994e0 使用 git 子模块状态添加后

附件:重新创建此承诺

您应该能够重新创建这个准确的承诺使用(在一个空的目录):

# Initialize new GIT repository
git init

# Set author data (don't set it as part of the `git commit` command or your default data will be stored as “commit author”)
git config --local user.name "Nobody"
git config --local user.email "none"

# Set both the commit and the author date to the start of the Unix epoch (this cannot be done using `git commit` directly)
export GIT_AUTHOR_DATE="Thu Jan 1 00:00:00 1970 +0000"
export GIT_COMMITTER_DATE="Thu Jan 1 00:00:00 1970 +0000"

# Add root commit
git commit --allow-empty --allow-empty-message --no-edit

创建可重复的Git承诺是令人惊讶的困难。

其他回答

您可以通过 create_readme.php 保存此代码并从您的 Git 项目的根目录运行 PHP 代码。

php create_readme.php

它将添加 README 文件到所有是空的目录,因此这些目录将被添加到指数。

<?php
    $path = realpath('.');
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path),       RecursiveIteratorIterator::SELF_FIRST);
    foreach($objects as $name => $object){
        if ( is_dir($name) && ! is_empty_folder($name) ){
            echo "$name\n" ;
            exec("touch ".$name."/"."README");
        }
    }

    function is_empty_folder($folder) {
        $files = opendir($folder);
        while ($file = readdir($files)) {
            if ($file != '.' && $file != '..')
                return true; // Not empty
            }
        }
?>

然后做

git commit -m "message"
git push

您可以随时将 README 文件放入目录中,并解释为什么您想要此,否则空白,目录在存储库中。

没有办法 Git 跟踪目录,所以唯一的解决方案是在您想要 Git 跟踪的目录中添加一个位置持有者文件。

文件可以被命名并包含你想要的任何东西,但大多数人使用一个空白的文件称为.gitkeep (尽管有些人更喜欢VCS诊断的.keep)。

预定. 标记它作为一个隐藏的文件。

另一个想法是添加一个 README 文件,解释该目录将用于什么。

添加另一个选项给女士。

假设你想添加一个目录到 git,这对于所有与 git 相关的目的,应该保持空白,从来没有跟踪其内容,一个.gitignore 如这里多次提议,会做这个技巧。

形式,如上所述,是:

*
!.gitignore

$ echo "*" > .gitignore && echo '!.gitignore' >> .gitignore && git add .gitignore

#!/bin/bash

dir=''

if [ "$1" != "" ]; then
    dir="$1/"
fi

echo "*" > $dir.gitignore && \
echo '!.gitignore' >> $dir.gitignore && \
git add $dir.gitignore

使用此,您可以从您想要添加的目录中运行它,或者将目录引用为第一个和唯一的参数:

$ ignore_dir ./some/directory

另一个选项(回复 @GreenAsJade的评论),如果你想跟踪一个空的文件夹,可能在未来包含跟踪的文件,但将是空的现在,你可以从.gitignore 文件中删除 *,并检查它。

!.gitignore

我建议在文件中保持一个行是,它给了.gitignore 目的。 否则,某些下行可能会认为要删除它. 如果你在线上放一个评论,这可能有帮助。

警告:这个推文真的不像它出现的那样工作,对不起不舒服。

下面的原创文章:

这个解决方案是短暂的,似乎工作得很好(参见EDIT!),但它不那么容易记住......

空的树 SHA-1 可以通过创建一个新的空的 Git 存储库,CD 进入它,并发出 git 写作树,从而输出空的树 SHA-1.

编辑:

我一直在使用这个解决方案,因为我发现它. 它似乎工作正如创建一个子模块一样,除非没有模块被定义在任何地方. 这导致发行 git 子模块 init 更新时的错误. 问题是, git 更新指数将 040000 树部分重新编写到 160000 承诺。

但是,如果您已经(并且不会)在您的存储库中使用任何 Git 子模块,而“空白”文件夹将保持空白,或者如果您希望 Git 了解其存在并忽略其内容,您可以使用此推文。