我如何将一个空的目录(不包含文件)添加到一个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

其他回答

The Ruby on Rails 登录文件夹创建方式:

mkdir log && touch log/.gitkeep && git add log/.gitkeep

现在日志目录将被列入树上,在部署时非常有用,所以你不需要写一个日常来创建日志目录。

登记文件可以通过发行,

echo log/dev.log >> .gitignore

但你可能知道这一点。

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

基本上,一个源代码管理系统的目的是管理源代码,空的目录不是源代码,Git也经常被描述为内容跟踪器,再次,空的目录不是内容(相反的,实际上),所以它们不被跟踪。

只需在您想要跟踪的空白目录中添加一个空白(没有内容).gitignore 文件。

例如,如果你想跟踪一个空的目录, /项目/内容/帖子,然后创建一个新的空的文件, /项目/内容/帖子/.gitignore

注意:.gitkeep 不是官方 Git 的部分:

此分類上一篇

Jamie Flournoy的解决方案工作得很好. 这里有一个稍微改进的版本,以保持.htaccess :

# Ignore everything in this directory
*
# Except this file
!.gitignore
!.htaccess

使用此解决方案,您可以插入一个空的文件夹,例如 /log、 /tmp 或 /cache 并且文件夹将保持空白。