不忽略特定深层嵌套文件集的更简单方法

发布于 2025-01-18 13:23:42 字数 1533 浏览 3 评论 0原文

我有深层嵌套的文件,我想将其包含在源代码管理中并让 git 跟踪它们。

示例是:

Projects/
    .git/
    StrategicProject/
        SpecificProject1/
            Method1/
                doc/
                   *.tex
    .gitignore

在每个不同的文件夹中,都有我不想跟踪的其他文件/文件夹。我只想跟踪 Projects/StrategicProject/SpecificProject1/Method1/doc/ 中的所有 .tex 文件

截至目前,为了完成此任务,我已经我的 .gitignore 文件:

StrategicProject/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/ # don't ignore this
StrategicProject/SpecificProject1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/ # don't ignore this
StrategicProject/SpecificProject1/Method1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/ # don't ignore this
StrategicProject/SpecificProject1/Method1/doc/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/*.tex # don't ignore this

上面的方法可以工作,但是当我在工作目录中创建新的文件夹结构时很容易出错。也相当麻烦。要跟踪嵌套文件夹中的一组 .tex 文件,需要在 .gitignore 文件中创建 8 行。一位 SO 用户创建了一个网络应用程序来缓解这一特定的困难活动。请参阅此处

虽然该应用程序目前运行良好,但它只能在网络上运行。更一般地说,是否有一个 GUI 应用程序,可以离线工作(无需使用互联网),可以查看我的文件夹结构,并允许我选中/取消选中与文件/对应的框(作为其用户界面的一部分)文件夹并根据该文件夹自动生成适当的 .gitignore 文件?


ETA:

我使用 VSCode 的默认 Git 界面。虽然这确实提供了右键单击文件并将其添加到 .gitignore 的选项,但这不是我想要的。我想忽略特定的深层嵌套文件集。我愿意尝试其他编辑来完成这项任务。

I have deeply nested files that I would like to include in source control and have git track them.

Example is:

Projects/
    .git/
    StrategicProject/
        SpecificProject1/
            Method1/
                doc/
                   *.tex
    .gitignore

In each of the various folders there are other files/folders that I would not like to track. I only want to track, say, all of the .tex files in Projects/StrategicProject/SpecificProject1/Method1/doc/

As of now, to accomplish this, I have in my .gitignore file:

StrategicProject/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/ # don't ignore this
StrategicProject/SpecificProject1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/ # don't ignore this
StrategicProject/SpecificProject1/Method1/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/ # don't ignore this
StrategicProject/SpecificProject1/Method1/doc/* # ignore everything under this, exceptions below
!StrategicProject/SpecificProject1/Method1/doc/*.tex # don't ignore this

The above works, but is prone to error when I create a new folder structure in my working directory. It is also quite cumbersome. To track one set of .tex files in a nested folder needs creation of 8 lines in the .gitignore file. A fellow-SO user created an web-app to ease this specific difficult activity. See here

While that app works fine for now, it only works over the web. More generally, is there a gui app, that works offline (without having to use the internet), that can take a look at my folder structure and allows me to check/uncheck boxes (as part of its user interface) corresponding to files/folders and based on that automatically generate the appropriate .gitignore file?


ETA:

I use VSCode's default Git interface. While that does provide option to right click on a file and add it to .gitignore, that is not what I would like. I would like to not ignore a specific deeply nested set of files. I am open to trying out other editors for this task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

愛放△進行李 2025-01-25 13:23:42

您可以使用一种有用的技巧,尽管计算成本较高。正如您所看到的,如果您忽略一个目录,Git 永远不会查看该目录的内部,从而无法取消忽略嵌套在该目录下较深处的文件。这通常可以节省 Git 大量时间:git status 必须递归地查看工作树中的所有内容,例外每次它遇到被忽略的目录时,它都不会必须查看那里的任何东西。由于lstat-工作树递归旅行中每个目录中的每个实体通常是 Git 工作树操作中最慢的部分,因此绕过其中一些调用有助于出了很多。

不过,您只需确保 !*/ 是每个 .gitignore 文件中的最后条目,就可以完全阻止 Git 绕过目录。无论 Git 处于遍历的哪个位置,最后一个条目都会说如果您遇到一个目录,请查看它的内部。现在您不再需要仔细而明确的内容,例如:

StrategicProject/*
!StrategicProject/SpecificProject1/
StrategicProject/SpecificProject1/*
!StrategicProject/SpecificProject1/Method1/
StrategicProject/SpecificProject1/Method1/*
!StrategicProject/SpecificProject1/Method1/doc/
StrategicProject/SpecificProject1/Method1/doc/*
!StrategicProject/SpecificProject1/Method1/doc/*.tex

因为您可以简单地列出应该忽略的文件; Git 总是会到处搜索。

拟议的未来增强功能(没有实现)

我认为 Git 应该根据需要自动插入异常。也就是说,我们应该能够写:

*.o
generated-docs
!generated-docs/*.in

并且 Git 应该弄清楚这个“意思”,例如:

generated-docs/*
!generated-docs/*.in

也就是说,我们已经排除了的某些内容< code>generate-docs 意味着 Git 应该查看内部 generated-docs,即使默认是跳过那些可能在那里找到。

There is one useful, albeit somewhat computationally expensive, trick you can use. As you've seen, if you ignore a directory, Git never looks inside the directory, making it impossible to un-ignore files nested deeper under the directory. This generally saves Git a lot of time: git status has to recursively look at everything in the working tree, except that every time it hits an ignored directory, it doesn't have to look at anything there. Since lstat-ing each entity in each directory in a recursive travel of the working tree is usually the slowest part of a Git working-tree operation, bypassing some of these calls helps out a lot.

Still, you can prevent Git from bypassing directories at all by simply making sure that !*/ is the last entry in each .gitignore file. No matter where Git is in its traversal, this last entry says if you encounter a directory, look inside it. Now you no longer need careful and explicit stuff like:

StrategicProject/*
!StrategicProject/SpecificProject1/
StrategicProject/SpecificProject1/*
!StrategicProject/SpecificProject1/Method1/
StrategicProject/SpecificProject1/Method1/*
!StrategicProject/SpecificProject1/Method1/doc/
StrategicProject/SpecificProject1/Method1/doc/*
!StrategicProject/SpecificProject1/Method1/doc/*.tex

as you can simply list files that should be ignored; Git will always be searching everywhere.

A proposed future enhancement (with no implementation)

I think Git should automatically insert exceptions as needed. That is, we should be able to write:

*.o
generated-docs
!generated-docs/*.in

and Git should figure out that this "means", e.g.:

generated-docs/*
!generated-docs/*.in

That is, the fact that we've excepted something within generated-docs means that Git should look inside generated-docs, even though the default is to skip over files that may be found there.

凉风有信 2025-01-25 13:23:42

对于您的示例,到目前为止最简单的是

StrategicProject/**
!StrategicProject/**/
!StrategicProject/SpecificProject1/Method1/doc/*.tex

我发音为“递归地排除StrategyProject中的所有内容,但仍然扫描所有目录以获取进一步的例外,例如这些tex文件”。


除非或直到您拥有一个非常大且复杂的源代码树,否则

!*/
build/

.gitignore 末尾放置类似的内容通常会更紧凑,以简单地关闭所有目​​录排除,例如这里的任何 build 目录,我称之为惯用语,以前看过它的人会立即理解。

For your sample by far the simplest is

StrategicProject/**
!StrategicProject/**/
!StrategicProject/SpecificProject1/Method1/doc/*.tex

which I'd pronounce "recursively exclude everything in StrategicProject but still scan all the directories for further exceptions, like these tex files".


Unless or until you've got a remarkably large and complicated source tree it's often more compact to put something like

!*/
build/

at the end of your .gitignore to simply shut off all directory exclusions except for example here any build directory, I'd call that idiomatic, something people who've seen it before immediately understand.

人间☆小暴躁 2025-01-25 13:23:42

至于创建和维护.gitignore您可能会查看IDE的插件。

例如,使用Intellij/pycharm,这可能足以使您尝试做的事情:
https://plugins.jetbrains.jetbrains.com/plugin/7495

As far as creating and maintaining the .gitignore you might look at plugins for your IDEs.

For example, with IntelliJ/PyCharm, this might be GUI enough for what you are trying to do:
https://plugins.jetbrains.com/plugin/7495--ignore

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