git 忽略包含 <pattern to ignore> 的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32335459/
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
git ignore filenames which contain <pattern to ignore>
提问by Brian J Hoskins
I am trying to tell git to ignore files which have "_autosave" somewhere in the filename. An example of such a file is:
我试图告诉 git 忽略文件名中某处带有“_autosave”的文件。此类文件的一个示例是:
hats/TFCB_ATV_TOP_HAT/_autosave-TFCB_ATV_HAT.kicad_pcb
hats/TFCB_ATV_TOP_HAT/_autosave-TFCB_ATV_HAT.kicad_pcb
In a regular expression, I would simply use the pattern ^.*_autosave.*
在正则表达式中,我会简单地使用模式 ^.*_autosave.*
Of course, git doesn't use regular expressions to evaluate its .gitignore file. I did some reading and had the impression that *_autosave*
would work, but it doesn't.
当然,git 不使用正则表达式来评估其 .gitignore 文件。我做了一些阅读,并有这样的印象*_autosave*
,但事实并非如此。
What is the correct syntax to tell git to ignore these files?
告诉 git 忽略这些文件的正确语法是什么?
回答by joran
Add this line to your .gitignore
将此行添加到您的 .gitignore
*_autosave*
According to git help gitignore
根据 git help gitignore
patterns match relative to the location of the .gitignore file
模式匹配相对于 .gitignore 文件的位置
Patternz like *_autosave*
match files or directories containing "_autosave" somewhere in the name.
Patternz 喜欢*_autosave*
匹配文件或名称中包含“_autosave”的目录。
Two consecutive asterisks ("**") in patterns mathed against full pathname may have special meaning
A leading "**" followed by a slash means match in all directories.
对完整路径名进行数学运算的模式中的两个连续星号 ("**") 可能具有特殊含义
前导“**”后跟斜杠表示在所有目录中匹配。
But "**/" seams redundant in some enviroments.
但是“**/”在某些环境中显得多余。
EDIT:
编辑:
My machine at work (using git 1.7.1) does not have support for dubbel asterisk, but with *_autosave*
it excludes the files.
我在工作的机器(使用 git 1.7.1)不支持 dubbel asterisk,但*_autosave*
它排除了文件。
here is a simple test scrtipt (for Linux)
这是一个简单的测试脚本(适用于 Linux)
DIR="$(mktemp -d)"
git init $DIR/project1
cd $DIR/project1
cat > .gitignore <<EOF
**/*_autosave*
*_autosave*
EOF
mkdir dir1
touch README foo_autosave dir1/bar_autosave
git status
rm -rf $DIR/project1