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


当前回答

您可以! 查看 Git FAQ。

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

其他回答

我也面临着空的目录的问题. 使用存储文件的问题是,你需要创建它们,并删除它们,如果它们不再需要(因为后来添加了子目录或文件。

这就是为什么我决定写一个开源工具,可以自动管理创建/删除此类存储文件。 它是为.NET 平台写的,并在 Mono (.NET for Linux) 和 Windows 中运行。

只需看看: http://code.google.com/p/markemptydirs

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

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

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

要将 Jamie Flournoy 的解决方案扩展到一个目录树上,您可以将此.gitignore 文件放在顶级目录中,并在 Git 应该跟踪的每个子目录中点击.keepdir。

# Ignore files but not directories. * matches both files and directories
# but */ matches only directories. Both match at every directory level
# at or below this one.
*
!*/

# Git doesn't track empty directories, so track .keepdir files, which also
# tracks the containing directory.
!.keepdir

# Keep this file and the explanation of how this works
!.gitignore
!Readme.md

安迪·莱斯特是对的,但如果你的目录只需要是空的,而不是空的,你可以把一个空的.gitignore 文件在那里作为一个工作室。

另一方面,这是一个实施问题,而不是一个基本的Git存储设计问题,正如在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