如何在 gitignore 中编写规则来忽略除某个目录中的目录之外的所有文件?
假设我有一个像这样的目录结构,
uploads/
--dir1/
----one.txt
----two.txt
----dir2/
------one.txt
------two.txt
我希望跟踪上传的任何目录和子目录,但不跟踪其中的任何文件(虚拟文件除外),因为 git 不跟踪目录。
我认为这样的事情会起作用,但事实并非如此。
*
!.gitignore
So lets say I have a directory structure like so
uploads/
--dir1/
----one.txt
----two.txt
----dir2/
------one.txt
------two.txt
I would like any directory and sub directories of uploads to be tracked but not any files in it with the exception of a dummy file because git doesn't track directories.
I would think something like this would work but it doesn't.
*
!.gitignore
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当你告诉它忽略
*
时,Git 不会递归到任何子目录(即使你有一个“unignore”模式可能与目录之一内部的内容相匹配)。不过,有一些 gitignore 模式语言可能会有所帮助。尾部斜杠强制该模式仅适用于目录。因此,您应该能够使用它:
在下面的演示中,我使用上面的
.gitignore
并在每个 < 旁边有一个附加文件(名为do-not-keep
)代码>.keep 文件。您可以看到它适用于多层子目录,并且不显示其他文件。您必须通过某种独立的方法安排每个目录拥有自己的.keep
(或其他内容)。这是使用 Git 1.7.3.1 完成的,但我希望它也适用于其他版本。
演示设置:
When you tell it to ignore
*
, Git will not recurse into any subdirectories (even though you have a “unignore” pattern that might match something inside one of the directories).There is a bit of the gitignore pattern language that might help though. A trailing slash forces the pattern to only apply to directories. So, you should be able to use this:
In the demonstration below I use the above
.gitignore
and have an additional file (nameddo-not-keep
) alongside each.keep
file. You can see that it works for multiple levels of subdirectories and does not show the other files. You will have to arrange for each directory to have its own.keep
(or whatever) via some independent method.This was done with Git 1.7.3.1, but I expect that it will work for other versions as well.
The demonstration setup: