是否有可能使git跟踪空文件夹?

时间:2013-09-03 12:10:48

标签: git

我从这个post中读到了解释为了让git跟踪文件夹,必须至少有一个空文件:

Currently the design of the Git index (staging area) only permits files 
to be listed and nobody competent enough to make the change to allow 
empty directories has cared enough about this situation to remedy it.

Directories are added automatically when adding files inside them. 
That is, directories never have to be added to the repository, 
and are not tracked on their own. You can say "git add <dir>" and it 
will add the files in there.

If you really need a directory to exist in checkouts you should
create a file in it.

我遇到了一些问题,因为git没有跟踪空文件夹,我列举了一些:

问题1

我的git存储库中有一个apache/log目录。

当我将它推送到服务器并运行网站时,它没有工作。它在我的本地系统中运行良好。经过大量研究后我发现git push没有推送空文件夹/log。服务检查文件夹日志以创建日志文件。由于文件夹log不存在,因此无法自动创建文件夹并放置这些文件。这就是错误的原因。

问题2

我有很多branch。我的特定分支中只有一个说frontend有文件夹caps

有时目录my_git_repo/caps/html/home/中的某个文件夹将为空。

分支master没有文件夹caps

如果我git checkout master,它将有空文件夹,即caps/html/home/tmp 所以我想手动删除文件夹caps,有时会造成混淆。

通常我会在文件夹中放入一个空文件(README)来解决这些问题。

所以我想让git跟踪所有空文件夹。可以通过编辑.git/config或其他东西吗?

任何帮助都会得到满足。:))

2 个答案:

答案 0 :(得分:5)

有一个非常空的文件夹,答案是否定的。 由于Git在技术上如何处理数据,这是不可能的。

但是,如果您可以接受一些妥协:只需创建一些文件,例如.gitignore或您想要查看的目录中的任何内容,您就完成了。自述文件可以是一个解决方案,但我个人不会使用该名称,因为这会让人们认为这些文件中的内容存在混淆。

有关非常相似的问题,请参阅How can I add an empty directory to a Git repository?

答案 1 :(得分:3)

就我个人而言,我通常通过在每个目录中都有一个名为dir.info的文件来解决这个问题,该文件中有一些文本说明目录是什么。

但是,如果您需要在结帐后创建特定的空目录,并且这些目录可能特定于特定分支,我建议git post-checkout hooks定制 - 您可以在顶级目录中有一个列表需要和不需要的空目录以及大约5行python,你可以根据需要创建它们并删除那些不是。

我可能会在第一个字符中使用-+,具体取决于应删除或添加哪个字符。

类似于(添加了错误处理)

import os

dirlist = open("Dirlist.Name", "rt")
for line in dirlist:
    flag = line[0]
    name = line[1:].strip()
    if flag == '+':
        os.makedirs(name)
    elif flag == '-':
        os.removedirs(name)
    else:
        print "Ignored:", line
相关问题