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


当前回答

我总是建立一个功能来检查我想要的文件夹结构,并在项目中为我构建它。

function check_page_custom_folder_structure () {
    if (!is_dir(TEMPLATEPATH."/page-customs"))
        mkdir(TEMPLATEPATH."/page-customs");    
    if (!is_dir(TEMPLATEPATH."/page-customs/css"))
        mkdir(TEMPLATEPATH."/page-customs/css");
    if (!is_dir(TEMPLATEPATH."/page-customs/js"))
        mkdir(TEMPLATEPATH."/page-customs/js");
}

这是在PHP,但我确信大多数语言支持相同的功能,因为文件夹的创建是由应用程序照顾的,文件夹将永远在那里。

其他回答

您可以通过 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

如上所述,无法添加空的目录,但这里是一个单一的列,将空的.gitignore 文件添加到所有目录中。

ruby -e'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each {♰f♰ FileUtils.touch(File.join(f, ".gitignore")) 如果 File.directory?(f) }'

我把它放在一个Rakefile中,以便轻松地访问。

touch .placeholder

在Linux上,它创建了一个名为.placeholder 的空白文件. 值得一提的是,这个名称是 agnostic 到 git,并在系统中的其他地方使用这种方法,例如 /etc/cron.d/.placeholder. 第二,正如另一个用户指出的那样,.git 预定协议可以为 Git 自己用于配置目的的文件和目录进行备份。

否则,正如另一个答案中所指出的那样,目录可能包含一个描述的 README.md 文件。

无论如何,这都需要文件的存在不会导致您的应用程序破裂。

输入: 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承诺是令人惊讶的困难。

您可以! 查看 Git FAQ。

目前,Git指数的设计仅允许文件列出,并且没有人有足够的能力进行更改,以便空的目录对这种情况有足够的关注,以便解决它。