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


当前回答

我总是建立一个功能来检查我想要的文件夹结构,并在项目中为我构建它。

function check_page_custom_folder_structure () {
    if (!is_dir(TEMPLATEPATH."/page-customs"))
        mkdir(TEMPLATEPATH."/page-customs");    
    if (!is_dir(TEMPLATEPATH."/page-customs/css"))
        mkdir(TEMPLATEPATH."/page-customs/css");
    if (!is_dir(TEMPLATEPATH."/page-customs/js"))
        mkdir(TEMPLATEPATH."/page-customs/js");
}

这是在PHP,但我确信大多数语言支持相同的功能,因为文件夹的创建是由应用程序照顾的,文件夹将永远在那里。

其他回答

这个解决方案为我工作了。

1. 将.gitignore 文件添加到您的空白目录中:

*
*/
!.gitignore

* 忽略文件夹中的所有文件 */ 忽略子指南!.gitignore 包含.gitignore 文件

然后删除您的缓存,步行您的文件,承诺并按下:

git rm -r --cached .
git add . // or git stage .
git commit -m ".gitignore fix"
git push

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

另一方面,这是一个实施问题,而不是一个基本的Git存储设计问题,正如在Git邮件列表上多次提到的那样,这一点没有实施的原因是没有人照顾足以提交一个插件,而不是它不能或不应该完成。

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

touch .gitkeep

这是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.

我正在寻找这个问题,因为:我创建一个新的目录,它包含许多文件。在这些文件中,一些我想添加到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状态”将显示单独文件的状态。