如何在 gitignore 中编写规则来忽略除某个目录中的目录之外的所有文件?

发布于 2024-09-26 13:07:02 字数 256 浏览 3 评论 0原文

假设我有一个像这样的目录结构,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滥情哥ㄟ 2024-10-03 13:07:02

当你告诉它忽略 * 时,Git 不会递归到任何子目录(即使你有一个“unignore”模式可能与目录之一内部的内容相匹配)。

不过,有一些 gitignore 模式语言可能会有所帮助。尾部斜杠强制该模式仅适用于目录。因此,您应该能够使用它:

# .gitignore
*
!.keep
!*/

在下面的演示中,我使用上面的 .gitignore 并在每个 < 旁边有一个附加文件(名为 do-not-keep)代码>.keep 文件。您可以看到它适用于多层子目录,并且不显示其他文件。您必须通过某种独立的方法安排每个目录拥有自己的 .keep (或其他内容)。

% git status --short -uall
?? a/.keep
?? a/b/.keep
?? a/b/c/.keep
?? a/b/c/d/.keep
?? e/.keep
?? e/f/.keep
?? e/f/g/.keep
?? h/.keep
?? h/i/.keep
?? j/.keep

这是使用 Git 1.7.3.1 完成的,但我希望它也适用于其他版本。

演示设置:

% git init
% mkdir -p a/b/c/d e/f/g h/i j
% zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep
% tree -aI .git .                                       
.
|-- .gitignore
|-- a
|   |-- .keep
|   |-- b
|   |   |-- .keep
|   |   |-- c
|   |   |   |-- .keep
|   |   |   |-- d
|   |   |   |   |-- .keep
|   |   |   |   `-- do-not-keep
|   |   |   `-- do-not-keep
|   |   `-- do-not-keep
|   `-- do-not-keep
|-- e
|   |-- .keep
|   |-- do-not-keep
|   `-- f
|       |-- .keep
|       |-- do-not-keep
|       `-- g
|           |-- .keep
|           `-- do-not-keep
|-- h
|   |-- .keep
|   |-- do-not-keep
|   `-- i
|       |-- .keep
|       `-- do-not-keep
`-- j
    |-- .keep
    `-- do-not-keep

10 directories, 21 files

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:

# .gitignore
*
!.keep
!*/

In the demonstration below I use the above .gitignore and have an additional file (named do-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.

% git status --short -uall
?? a/.keep
?? a/b/.keep
?? a/b/c/.keep
?? a/b/c/d/.keep
?? e/.keep
?? e/f/.keep
?? e/f/g/.keep
?? h/.keep
?? h/i/.keep
?? j/.keep

This was done with Git 1.7.3.1, but I expect that it will work for other versions as well.

The demonstration setup:

% git init
% mkdir -p a/b/c/d e/f/g h/i j
% zargs -i.. -- **/*(/) -- touch ../.keep ../do-not-keep
% tree -aI .git .                                       
.
|-- .gitignore
|-- a
|   |-- .keep
|   |-- b
|   |   |-- .keep
|   |   |-- c
|   |   |   |-- .keep
|   |   |   |-- d
|   |   |   |   |-- .keep
|   |   |   |   `-- do-not-keep
|   |   |   `-- do-not-keep
|   |   `-- do-not-keep
|   `-- do-not-keep
|-- e
|   |-- .keep
|   |-- do-not-keep
|   `-- f
|       |-- .keep
|       |-- do-not-keep
|       `-- g
|           |-- .keep
|           `-- do-not-keep
|-- h
|   |-- .keep
|   |-- do-not-keep
|   `-- i
|       |-- .keep
|       `-- do-not-keep
`-- j
    |-- .keep
    `-- do-not-keep

10 directories, 21 files
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文