Git 中的白名单和子目录

发布于 2025-01-03 03:00:06 字数 172 浏览 0 评论 0原文

我仅为文本文件创建了白名单。

*
!*.txt

现在,我在子目录中有一个未跟踪的文本文件 - sub/dir/file.txt,并且该文件未显示(它被忽略)。但是,会显示根目录中的文本文件。

为什么会这样,我该如何解决?

I have created a white-list for text files only.

*
!*.txt

Now, I have an untracked text file in a sub-directory - sub/dir/file.txt, and this is NOT shown (it is ignored). Text files in the root directory are shown, however.

Why is that, and how do I fix it?

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

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

发布评论

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

评论(4

秉烛思 2025-01-10 03:00:06

如果您尝试这样做,它将失败,因为您最终会将结构中的目录列入黑名单。

为了解决这个问题,您需要将所有非目录且不是您要提交的文件类型之一的内容列入黑名单,而不是将目录列入黑名单

将执行此操作的 .gitignore 文件:

# First, ignore everything
*
# Now, whitelist anything that's a directory
!*/
# And all the file types you're interested in.
!*.one
!*.two
!*.etc

在存在 *.one.txt 文件的三级结构白名单中对此进行了测试使用位于存储库根目录中的 .gitignore 的 code>、*.two*. Three 文件 - 对我有用。您不必将 .gitignore 文件添加到结构中的所有目录中。

我用来找出答案的信息来自这个 (stackoverflow.com)。

If you try it that way, it'll fail, because you'll end up blacklisting the directories in your structure.

To solve, you want to blacklist everything that is not a directory, and is not one of the file-types you want to commit, while not blacklisting directories.

The .gitignore file that will do this:

# First, ignore everything
*
# Now, whitelist anything that's a directory
!*/
# And all the file types you're interested in.
!*.one
!*.two
!*.etc

Tested this in a three-level structure white-listing for .txt files in the presence of *.one, *.two and *.three files using a .gitignore located in the root directory of the repository - works for me. You won't have to add .gitignore files to all directories in your structure.

Information I used to figure out the answer came from, amongst other things, this (stackoverflow.com).

辞旧 2025-01-10 03:00:06

实现此目的的一种更简单的方法是:

# Ignore all files...
*.*

# ...except the ones we want
!*.txt

这是有效的,因为 gitignore 将不以 / 开头的模式应用于 .gitignore 文件下面的每个级别:

如果模式的开头或中间(或两者)有分隔符,则该模式相对于特定 .gitignore 文件本身的目录级别。否则,该模式也可能在 .gitignore 级别以下的任何级别匹配。

如果您想对目录中的文件执行此操作,事情会变得更加复杂:

# Ignore all files in all directories inside subdir...
/subdir/**/*.*

# ...except the ones we want
!/subdir/**/*.txt

这是有效的,因为 gitignore** 有特殊规则:

与完整路径名匹配的模式中的两个连续星号(“**”)可能具有特殊含义:

  • 斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“a/**/b”匹配“a/b”、“a/x/b”、“>a/x/y/b”等等。

关键是确保您不会忽略目录,因为无论其他规则如何,该目录中的每个文件都会被忽略。

A simpler way of achieving this is:

# Ignore all files...
*.*

# ...except the ones we want
!*.txt

This works because gitignore applies patterns that do not start with / to every level below the .gitignore file:

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.

If you wanted to do this to files inside a directory, things get more complex:

# Ignore all files in all directories inside subdir...
/subdir/**/*.*

# ...except the ones we want
!/subdir/**/*.txt

This works because gitignore has special rules for **:

Two consecutive asterisks ("**") in patterns matched against full pathname may have special meaning:

  • A slash followed by two consecutive asterisks then a slash matches zero or more directories. For example, "a/**/b" matches "a/b", "a/x/b", "a/x/y/b" and so on.

The key piece is to make sure you don't ignore directories, because then every file within that directory is ignored regardless of other rules.

做个少女永远怀春 2025-01-10 03:00:06

我搜索了很长时间:

  1. 假设我有一个大文件夹结构,其中递归嵌套了约 100.000 个目录。在这些文件夹中,大约有 30,000 个 .txt 类型的文件(在我的例子中:类型为 *.md)。在这些 *.md 文件旁边,可以说,有 500GB(超过一百万)个我不想跟踪的文件。

  2. 我希望 git 仅跟踪所有文件夹和子目录中的 .txt(或 *.md)文件。

正确的答案应该是:这在 Git 中是不可能的。

我做了什么:

[编辑:也不起作用 -
我尝试创建一个带有符号链接(或硬链接)的文件夹并在那里使用 git,但 git 不遵循符号链接并覆盖硬链接。哎呀!]

I searched for a long time:

  1. Assume I have a large folder structure with ~100.000 directories recursively nested. In those folders, there're about 30.000 files of type .txt (in my case: type *.md). Next to these *.md files, there're, lets say, 500GB of (a million+) files that I don't want to track.

  2. I want git to track only .txt (or *.md) files in all folders and subdirs.

The correct answer should be: this is not possible in Git.

What I did instead:

[edit: did also not work -
I tried to create a folder with symlinks (or hardlinks) and use git there, but git doesn't follow symlinks and overwrites hardlinks. Doh!]

朦胧时间 2025-01-10 03:00:06

如果您想忽略存储库中除 .txt 文件之外的所有内容,.gitignore 应该类似于:

# Ignore everything
*
# Whitelist every directories, so we can recurse into them
!*/
# Whitelist .gitignore and .txt
!.gitignore
!*.txt

但是如果您想忽略仅一个文件夹您的存储库并将任何目录或子目录中的 .txt 文件列入白名单:

# Recursively ignore every files in my_folder
/my_folder/**
# Recursively whitelist every directories in my_folder
!/my_folder/**/
# Whitelist .txt
!*.txt

If you want to ignore everything in the repository except .txt files , .gitignore should be like:

# Ignore everything
*
# Whitelist every directories, so we can recurse into them
!*/
# Whitelist .gitignore and .txt
!.gitignore
!*.txt

But if you want to ignore just one folder of your repository and whitelist .txt files in any directory or subdirectory:

# Recursively ignore every files in my_folder
/my_folder/**
# Recursively whitelist every directories in my_folder
!/my_folder/**/
# Whitelist .txt
!*.txt

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