简体   繁体   English

如何忽略或永久阻止文件名中包含日期或日期+时间的文件被 Git 检测为添加/修改的文件

[英]How to Ignore or permanently block the files, which contain date or date+time in their filenames, from being detected by Git as files added/modified

As I mentioned in the Q-title, I have some files whose names contain either date string or date+time timestamp in their names which are in the project configured with Git.正如我在 Q-title 中提到的,我有一些名称包含date字符串或日期+时间时间戳的文件,这些文件位于使用 Git 配置的项目中。

File names with dates or timestamps(date+time) are like below:带有日期或时间戳(日期+时间)的文件名如下:

....
....
FileName1_18-07-2022.php
FileName2_18-07-2022_10-28-17_PM.vue
FileNameOther1_18-07-2022_10-28-28_PM.vue
....
....

And there are more than 20s of such files, which I want permanently ignored from Git detection.并且有超过 20 个这样的文件,我希望从 Git 检测中永久忽略它们。 Filename can have numbers but when they contain exact date format strings(like shown in the examples above) in their names they must not be detected by Git whenever created/modified.文件名可以有数字,但是当它们的名称中包含精确的日期格式字符串(如上面的示例中所示)时,无论何时创建/修改它们都不能被 Git 检测到。

I know there are 2 methods for this, one is .gitignore and other is in Project's .git/info/exclude and I know couple wildcards like * and ?我知道有两种方法,一种是.gitignore ,另一种是在项目的.git/info/exclude中,我知道几个通配符,比如*? , but can't figure out how to block those files that have date or date+time in their names. ,但无法弄清楚如何阻止那些名称中包含日期或日期+时间的文件。

Anyone can help figure this out ?任何人都可以帮助解决这个问题吗?

Note: "_PM" or "_AM" are optional and apparently they would be only present if Filename contains full date+time string.注意: “_PM”或“_AM”是可选的,显然它们只有在文件名包含完整的日期+时间字符串时才会出现。 And date or date+time strings will always be in exact pattern as shown in list above.并且日期或日期+时间字符串将始终采用上面列表中所示的精确模式。 No other pattern of date/date+time is there, positively.肯定没有其他日期/日期+时间模式。

Add to your .gitignore :添加到您的.gitignore

*_??-??-????[._]*

This will match files with a date followed by a dot or an underscore, and is unlikely to match anything else.这将匹配带有日期后跟点或下划线的文件,并且不太可能匹配其他任何内容。

With the help of John Kugelman's answer and some of turek's nice suggestions from comments I came up with a pattern that won't have any issue from year 1000 until year 9999 ;).在 John Kugelman 的回答和 turek 从评论中提出的一些很好的建议的帮助下,我想出了一个从 1000 年到 9999 年不会有任何问题的模式;)。 So this perfectly matches all filename patterns that I have in my project and successfully ignores those files when this pattern is added in either .gitignore or .git/info/exclude file:因此,这完全匹配我在项目中拥有的所有文件名模式,并在.gitignore.git/info/exclude文件中添加此模式时成功忽略这些文件:

*_[0123][0-9]-[01][0-9]-[1-9][0-9][0-9][0-9][._]*.php
*_[0123][0-9]-[01][0-9]-[1-9][0-9][0-9][0-9][._]*.vue

If anyone wants to block all file extensions then they can use this:如果有人想阻止所有文件扩展名,那么他们可以使用这个:

*_[0123][0-9]-[01][0-9]-[1-9][0-9][0-9][0-9][._]*.*

Thanks both John and turek for help.感谢 John 和 turek 的帮助。 Anyone having any suggestions or point-outs feel free to mention in comments below.任何有任何建议或指出的人都可以在下面的评论中提及。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM