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


当前回答

正如其他答案中所描述的那样,Git 无法在其排序区域中代表空的目录(见 Git FAQ ) 但是,如果为您的目的,一个目录是空的,如果它只包含一个.gitignore 文件,那么您只能通过:

find . -type d -empty -exec touch {}/.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

将.gitkeep 文件添加到空的目录中,并承诺。

touch .gitkeep

这是Git跟随的标准。

假设你需要一个空的目录,称为tmp:

$ mkdir tmp
$ touch tmp/.gitignore
$ git add tmp
$ echo '*' > tmp/.gitignore
$ git commit -m 'Empty directory' tmp

换句话说,你需要将.gitignore 文件添加到指数之前,你可以告诉 Git 忽略它(以及所有其他在空的目录中)。

只需添加一个 readme 或.gitignore 文件,然后从 GitHub 网站删除它,但不是终端。

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

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

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

此分類上一篇