Git 中的白名单和子目录
我仅为文本文件创建了白名单。
*
!*.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您尝试这样做,它将失败,因为您最终会将结构中的目录列入黑名单。
为了解决这个问题,您需要将所有非目录且不是您要提交的文件类型之一的内容列入黑名单,而不是将目录列入黑名单。
将执行此操作的
.gitignore
文件:在存在
*.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: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).
实现此目的的一种更简单的方法是:
这是有效的,因为
gitignore
将不以/
开头的模式应用于.gitignore
文件下面的每个级别:如果您想对目录中的文件执行此操作,事情会变得更加复杂:
这是有效的,因为
gitignore
对**
有特殊规则:关键是确保您不会忽略目录,因为无论其他规则如何,该目录中的每个文件都会被忽略。
A simpler way of achieving this is:
This works because
gitignore
applies patterns that do not start with/
to every level below the.gitignore
file:If you wanted to do this to files inside a directory, things get more complex:
This works because
gitignore
has special rules for**
: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.
我搜索了很长时间:
假设我有一个大文件夹结构,其中递归嵌套了约 100.000 个目录。在这些文件夹中,大约有 30,000 个
.txt
类型的文件(在我的例子中:类型为*.md
)。在这些*.md
文件旁边,可以说,有 500GB(超过一百万)个我不想跟踪的文件。我希望 git 仅跟踪所有文件夹和子目录中的
.txt
(或*.md
)文件。正确的答案应该是:这在 Git 中是不可能的。
我做了什么:
[编辑:也不起作用 -
我尝试创建一个带有符号链接(或硬链接)的文件夹并在那里使用 git,但 git 不遵循符号链接并覆盖硬链接。哎呀!]
I searched for a long time:
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.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!]
如果您想忽略存储库中除
.txt
文件之外的所有内容,.gitignore
应该类似于:但是如果您想忽略仅一个文件夹您的存储库并将任何目录或子目录中的
.txt
文件列入白名单:If you want to ignore everything in the repository except
.txt
files ,.gitignore
should be like:But if you want to ignore just one folder of your repository and whitelist
.txt
files in any directory or subdirectory: