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


当前回答

我喜欢Artur79和mjs的答案,所以我已经使用了两者的组合,并为我们的项目创造了标准。

find . -type d -empty -exec touch {}/.gitkeep \;

但是,只有少数我们的开发人员在Mac或Linux上工作,很多工作在Windows上,我找不到一个相当的简单的单线来完成相同的。

因此,由于我们大多数开发人员已经安装了Ant,我想到的第一件事是将一个Ant构建文件组合起来,以便独立于平台完成这一点。

pip3 install gitkeep2

它将允许你创建和删除.gitkeep 文件重复,它也将允许你添加消息给他们,让你的同事了解为什么这些目录是重要的。

$ gitkeep --help
Usage: gitkeep [OPTIONS] PATH

  Add a .gitkeep file to a directory in order to push them into a Git repo
  even if they're empty.

  Read more about why this is necessary at: https://git.wiki.kernel.org/inde
  x.php/Git_FAQ#Can_I_add_empty_directories.3F

Options:
  -r, --recursive     Add or remove the .gitkeep files recursively for all
                      sub-directories in the specified path.
  -l, --let-go        Remove the .gitkeep files from the specified path.
  -e, --empty         Create empty .gitkeep files. This will ignore any
                      message provided
  -m, --message TEXT  A message to be included in the .gitkeep file, ideally
                      used to explain why it's important to push the specified
                      directory to source control even if it's empty.
  -v, --verbose       Print out everything.
  --help              Show this message and exit.

其他回答

您可以通过 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存储库,有些不。

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    ../trine/device_android/

它不在这个新目录中列出单独的文件,然后我认为也许我只能添加这个目录,然后处理单独的文件,所以我谷歌“只添加目录”。

在我的情况下,我发现我只能在新目录中添加一个文件,我确信我想将其添加到Git。

git add new_folder/some_file

此后,“Git状态”将显示单独文件的状态。

没有办法 Git 跟踪目录,所以唯一的解决方案是在您想要 Git 跟踪的目录中添加一个位置持有者文件。

文件可以被命名并包含你想要的任何东西,但大多数人使用一个空白的文件称为.gitkeep (尽管有些人更喜欢VCS诊断的.keep)。

预定. 标记它作为一个隐藏的文件。

另一个想法是添加一个 README 文件,解释该目录将用于什么。

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

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

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

此分類上一篇

要将 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