Git 中的白名单和子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9162919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Whitelisting and subdirectories in Git
提问by Nate
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.
现在,我在子目录 - 中有一个未跟踪的文本文件sub/dir/file.txt
,并且没有显示(它被忽略)。但是,会显示根目录中的文本文件。
Why is that, and how do I fix it?
为什么会这样,我该如何解决?
回答by simont
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:
.gitignore
将执行此操作的文件:
# 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.
.txt
在存在 的文件的三级结构白名单中对此进行了测试*.one
,*.two
并且*.three
使用.gitignore
位于存储库根目录中的文件 - 对我有用。您不必将.gitignore
文件添加到结构中的所有目录。
Information I used to figure out the answer came from, amongst other things, this(stackoverflow.com).
我用来找出答案的信息来自于此(stackoverflow.com)。
回答by Alex
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.
假设我有一个大型文件夹结构,其中包含递归嵌套的 ~100.000 个目录。在这些文件夹中,大约有 30.000 个类型的文件
.txt
(在我的情况下: type*.md
)。在这些*.md
文件旁边,可以说,有 500GB(一百万+)个我不想跟踪的文件。我希望 git 仅跟踪
.txt
(或*.md
)所有文件夹和子目录中的文件。
The correct answer should be: this is not possible in Git.
正确答案应该是:这在 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!]
[编辑:也没有工作 - 我试图创建一个带有符号链接(或硬链接)的文件夹并在那里使用 git,但 git 不遵循符号链接并覆盖硬链接。呸!]
回答by Sharadh
A simpler way of achieving this is:
实现这一目标的更简单方法是:
# Blacklist 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:
这是有效的,因为gitignore
将不以开头的模式应用于文件/
下的每个级别.gitignore
:
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.
如果模式的开头或中间(或两者)有分隔符,则模式是相对于特定 .gitignore 文件本身的目录级别的。否则模式也可能在低于 .gitignore 级别的任何级别匹配。
If you wanted to do this to files inside a directory, things get more complex:
如果您想对目录中的文件执行此操作,事情会变得更加复杂:
# Blacklist all files in all directories inside subdir...
/subdir/**/*.*
# ...except the ones we want
!/subdir/**/*.txt
This works because gitignore
has special rules for **
:
这是有效的,因为gitignore
有以下特殊规则**
:
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.
**
与完整路径名匹配的模式中的两个连续星号 (" ") 可能具有特殊含义:
- 斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“
a/**/b
”匹配“a/b
”、“a/x/b
”、“a/x/y/b
”等。
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.
关键是确保您不要忽略目录,因为无论其他规则如何,该目录中的每个文件都会被忽略。