有没有办法让 git 忽略根目录中不符合特定命名约定的文件?

Is there a way to have git ignore files in the root directory if they don't match a specific naming convention?

好的,所以我已经在 Google 上寻找了一些方法来执行此操作并在 Whosebug 上进行了搜索,但到目前为止还没有看到任何说明如何完成此操作的内容(或者如果它是这是不可能的)。

我需要做的是能够忽略根目录中的所有 .txt 文件,除了那些具有某种命名模式的文件。例如,以下文件将是我希望“看到”并能够提交到存储库的文件:

基本上,如果文件的名称遵循以下模式“[字母][4 个数字][(可选)字母后跟一个数字]_[M][任何数字][至少 1 个字母数字字符] .txt" 或 "ReadMe.txt" 那么我希望它不会被忽略。同样,需要忽略根目录中与此模式不匹配的任何 .txt 文件。有什么办法吗?

如果你查看文档,你可以使用这个:

An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again.

关于模式:

An asterisk "*" matches anything except a slash. The character "?" matches any one character except "/". The range notation, e.g. [a-zA-Z], can be used to match one of the characters in a range. See fnmatch(3) and the FNM_PATHNAME flag for a more detailed description.

这看起来有点疯狂,但应该可行:

*.txt
!W[0-9][0-9][0-9][0-9]_M[a-zA-Z0-9]?*.txt
!W[0-9][0-9][0-9][0-9][a-zA-Z][0-9]_M[a-zA-Z0-9]*.txt

如上所述,它忽略所有 .txt 文件,然后是 re-adds W,四位十进制数字,_M,一个字母或数字后跟可选的任何内容。对于在 _.

之前带有字母和小数位的变体也是如此