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


当前回答

也许添加一个空的目录似乎会是最少的抵抗的道路,因为你有脚本,希望该目录存在(也许是因为它是生成的二进制的目标)。

mkdir --parents .generated/bin ## create a folder for storing generated binaries
mv myprogram1 myprogram2 .generated/bin ## populate the directory as needed

在此例子中,您可以查看(破)标志性链接到目录,以便您可以访问它,而无需“创建”预定义(但这是可选的)。

ln -sf .generated/bin bin
git add bin

如果你想清理你的源树,你可以:

rm -rf .generated ## this should be in a "clean" script or in a makefile

如果您采取常见的检查方法,在一个几乎空的文件夹中,您有更少的复杂性,删除内容,而不删除“Gitignore”文件。

您可以忽略所有创建的文件,并将以下内容添加到您的 root.gitignore:

.generated

其他回答

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

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

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

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

此分類上一篇

touch .placeholder

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

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

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

您可以通过 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 跟踪空的目录 允许探索其他选项. 也许(希望)你有一个部署脚本. 让部署脚本创建目录后 git 克隆. 或者你有一个构建脚本. 让构建脚本创建目录后编辑. 或者也许甚至修改工具本身检查并创建目录之前使用。

我认为这是对问题的更敏感的方法. 构建脚本和部署脚本是为了准备事物运行程序。 您的工具需要一个空的目录. 因此,使用这些脚本来创建空的目录。


独自行走是危险的,请遵循这些命令。

find -type d -empty

find -name .git -prune -o -type d -empty -print

下列命令可能会帮助您,如果您继承了一个包含“空白”目录的项目。

列出包含一个名为.gitkeep 的文件的每个目录:

find -type f -name .gitkeep

find -type f -printf "%h\n" | sort | uniq -c | sort -n