是否可以让 git 跟踪空文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18592006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to make git to track empty folders?
提问by suhailvs
I read from this postwhich explains that to make git to track a folder there must atleast an empty file:
我从这篇文章中读到,它解释说要使 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.
I have faced some problems because git not tracking empty folders, i am list a few:
我遇到了一些问题,因为 git 不跟踪空文件夹,我列出了一些:
Problem1:
问题1:
I have a apache/log
directory in one of my git repository.
apache/log
我的 git 存储库之一中有一个目录。
when I pushed it to server and run the website it didn't worked. Where it worked fine in my localsystem. after a lot of research i found out that git push
did n't pushed the empty folder /log
. the serve checked folder log to create logfiles. since folder log
did not exist, it is not possible to automatically create a folder and put these files. That is why the error.
当我将它推送到服务器并运行网站时,它不起作用。它在我的本地系统中运行良好。经过大量研究,我发现git push
并没有推送空文件夹/log
。服务检查文件夹日志以创建日志文件。由于文件夹log
不存在,因此无法自动创建文件夹并放置这些文件。这就是错误的原因。
Problem2:
问题2:
I have lots of branch
. Only one of my particular branch say frontend
have folder caps
.
我有很多branch
. 只有我的一个特定分支说frontend
有 folder caps
。
Sometime one of a folder in the directory my_git_repo/caps/html/home/
will be empty.
有时目录中的文件夹之一my_git_repo/caps/html/home/
将为空。
branch master
doesn't have folder caps
分支master
没有文件夹caps
if i make git checkout master
it will have empty folder ie caps/html/home/tmp
so i want to manually delete the folder caps
sometimes it will create confusion.
如果我让它git checkout master
有空文件夹,即caps/html/home/tmp
所以我想手动删除文件夹caps
有时会造成混乱。
usually i solve these problems by putting an empty file(README
) in the folder.
通常我通过README
在文件夹中放置一个空文件()来解决这些问题。
So I would like to make git to track all empty folders. is it possible by editing .git/config
or something?
所以我想让 git 跟踪所有空文件夹。是否可以通过编辑.git/config
或其他方式?
Any help will be appriciated.:))
任何帮助都会得到帮助。:))
采纳答案by Steve Barnes
Personally I usually solve this by having a file called dir.info
in every directory that has some text stating what the directory is there for.
就我个人而言,我通常通过dir.info
在每个目录中调用一个文件来解决这个问题,该文件有一些文本说明该目录的用途。
However, if you need to create specific, empty, directories after a checkout with those directories possibly being specific to specific branches I would suggest git post-checkout hooksare made to order - you can have a list in your top level directory of the empty directories that are needed and not needed and with about 5 lines of python you can create them if required and delete those that are not.
但是,如果您需要在结帐后创建特定的空目录,而这些目录可能特定于特定分支,我建议按订单制作git post-checkout 挂钩- 您可以在您的顶级目录中列出一个空目录需要和不需要的目录以及大约 5 行 python,您可以根据需要创建它们并删除不需要的目录。
I would probably do something like having -
or +
in the first char depending on which should be removed or added.
我可能会做一些事情,比如在第一个字符中包含-
或+
,具体取决于应该删除或添加哪个。
Something like, (with the error handling added):
类似的东西,(添加了错误处理):
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
回答by Baptiste Mathus
With a really empty folder, the answer is no. It's not possible because of how Git technically handles data.
对于一个真正空的文件夹,答案是否定的。这是不可能的,因为 Git 在技术上处理数据的方式。
BUT, if you're OK to accept some compromise: just create some file, like .gitignore or whatever inside the directory you want be seen, and you're done. README file can be a solution, but I would personnally not use that name because that would create confusion with people thinking there's content inside those files.
但是,如果您可以接受一些妥协:只需创建一些文件,例如 .gitignore 或您希望看到的目录中的任何内容,就完成了。README 文件可以是一个解决方案,但我个人不会使用该名称,因为这会与认为这些文件中有内容的人混淆。
See How can I add an empty directory to a Git repository?for a very similar question.
请参阅如何将空目录添加到 Git 存储库?对于一个非常相似的问题。