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


当前回答

你不能,不幸的是,你永远不会能做到这一点,这是Linus Torvald自己做的决定,他知道什么对我们有好处。

这里有一个地方,我读过一次。

我找到了 Re: 空的目录,但也许还有另一个。

你必须和工人一起生活......不幸的是。

其他回答

我也面临着空的目录的问题. 使用存储文件的问题是,你需要创建它们,并删除它们,如果它们不再需要(因为后来添加了子目录或文件。

这就是为什么我决定写一个开源工具,可以自动管理创建/删除此类存储文件。 它是为.NET 平台写的,并在 Mono (.NET for Linux) 和 Windows 中运行。

只需看看: http://code.google.com/p/markemptydirs

这样做的一个简单的方式是通过添加一个.gitkeep 文件到您希望(目前)保持空白的目录。

查看此 SOF 答案以获取更多信息 - 这也解释了为什么一些人发现添加.gitignore 文件的竞争协议(如在许多答案中所述)令人困惑。

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

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

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

*
*/
!.gitignore

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

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

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

如上所述,无法添加空的目录,但这里是一个单一的列,将空的.gitignore 文件添加到所有目录中。

ruby -e'require "fileutils" ; Dir.glob(["target_directory","target_directory/**"]).each {♰f♰ FileUtils.touch(File.join(f, ".gitignore")) 如果 File.directory?(f) }'

我把它放在一个Rakefile中,以便轻松地访问。