git gitignore 目录中的所有扩展文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10712555/
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
gitignore all files of extension in directory
提问by Harry
Is there a way to ignore all files of a type in a directory?
有没有办法忽略目录中某个类型的所有文件?
**
is apparently meaningless to git, so this doesn't work:
**
显然对 git 毫无意义,所以这不起作用:
/public/static/**/*.js
The idea is to match arbitrary nested folders.
这个想法是匹配任意嵌套的文件夹。
采纳答案by ptyx
Never tried it, but git help ignore
suggests that if you put a .gitignore
with *.js
in /public/static
, it will do what you want.
从来没有尝试过,但git help ignore
建议如果你把.gitignore
with *.js
in /public/static
,它会做你想做的。
Note: make sure to also check out Joeys' answer below: if you want to ignore files in a specific subdirectory, then a local .gitignore is the right solution (locality is good). However if you need the same pattern to apply to your whole repo, then the ** solution is better.
注意:请务必查看下方 Joeys 的回答:如果您想忽略特定子目录中的文件,那么本地 .gitignore 是正确的解决方案(本地性很好)。但是,如果您需要将相同的模式应用于整个存储库,那么 ** 解决方案会更好。
回答by joeyhoer
It would appear that the **
syntax is supported by git
as of version 1.8.2.1
according to the documentation.
根据文档,该**
语法似乎受git
as of version支持。1.8.2.1
Two consecutive asterisks ("
**
") in patterns matched against full pathname may have special meaning:
A leading "
**
" followed by a slash means match in all directories. For example, "**/foo
" matches file or directory "foo
" anywhere, the same as pattern "foo
". "**/foo/bar
" matches file or directory "bar
" anywhere that is directly under directory "foo
".A trailing "
/**
" matches everything inside. For example, "abc/**
" matches all files inside directory "abc
", relative to the location of the.gitignore
file, with infinite depth.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.Other consecutive asterisks are considered invalid.
**
与完整路径名匹配的模式中的两个连续星号 (" ") 可能具有特殊含义:
前导 "
**
" 后跟斜杠表示在所有目录中匹配。例如,“**/foo
”匹配文件或目录“foo
”的任何位置,与模式“foo
”相同。"**/foo/bar
" 匹配文件或目录 "bar
" 直接位于目录 "foo
"下的任何位置。尾随的“
/**
”匹配里面的所有内容。例如,“abc/**
”匹配目录“abc
”内的所有文件,相对于.gitignore
文件的位置,具有无限深度。斜杠后跟两个连续的星号,然后斜杠匹配零个或多个目录。例如,“
a/**/b
”匹配“a/b
”、“a/x/b
”、“a/x/y/b
”等。其他连续的星号被认为是无效的。
回答by Adam Sharp
UPDATE: Take a look at @Joey's answer: Git now supports the **
syntax in patterns. Both approaches should work fine.
更新:看看@Joey 的回答:Git 现在支持**
模式中的语法。这两种方法都应该可以正常工作。
The gitignore(5) man pagestates:
该gitignore(5)手册页中指出:
Patterns read from a .gitignore file in the same directory as the path, or in any parent directory, with patterns in the higher level files (up to the toplevel of the work tree) being overridden by those in lower level files down to the directory containing the file.
从与路径相同的目录或任何父目录中的 .gitignore 文件读取模式,高级文件中的模式(直到工作树的顶层)被低级文件中的模式覆盖到目录包含文件。
What this means is that the patterns in a .gitignore
file in any given directory of your repo will affect that directoryand all subdirectories.
这意味着在您的.gitignore
存储库的任何给定目录中的文件中的模式将影响该目录和所有子目录。
The pattern you provided
您提供的图案
/public/static/**/*.js
isn't quite right, firstly because (as you correctly noted) the **
syntax is not used by Git. Also, the leading /
anchors that pattern to the start of the pathname. (So, /public/static/*.js
will match /public/static/foo.js
but not/public/static/foo/bar.js
.) Removing the leading EDIT: Just removing the leading slash won't work either — because the pattern still contains a slash, it is treated by Git as a plain, non-recursive shell glob (thanks @Joey Hoerfor pointing this out)./
won't work either, matching paths like public/static/foo.js
and foo/public/static/bar.js
.
不太正确,首先是因为(正如您正确指出的那样)**
Git 不使用该语法。此外,该/
模式的前导锚定到路径名的开头。(因此,/public/static/*.js
将匹配/public/static/foo.js
但不匹配/public/static/foo/bar.js
。)删除前导编辑:仅仅删除前导斜杠也不起作用——因为模式仍然包含一个斜杠,它被 Git 视为一个普通的、非递归的 shell glob(感谢@Joey Hoer指出这一点)。/
也不起作用,匹配public/static/foo.js
和foo/public/static/bar.js
.
As @ptyx suggested, what you need to do is create the file <repo>/public/static/.gitignore
and include just this pattern:
正如@ptyx 所建议的,您需要做的是创建文件<repo>/public/static/.gitignore
并仅包含以下模式:
*.js
There is no leading /
, so it will match at any part of the path, and that pattern will only ever be applied to files in the /public/static
directory and its subdirectories.
没有前导/
,因此它将匹配路径的任何部分,并且该模式将仅应用于/public/static
目录及其子目录中的文件。
回答by Thiago Ramos
To ignore untracked files just go to .git/info/exclude. Exclude is a file with a list of ignored extensions or files.
要忽略未跟踪的文件,只需转到 .git/info/exclude。排除是一个包含被忽略的扩展名或文件列表的文件。
回答by Garini
I believe the simplest solution would be to use find
. I do not like to have multiple .gitignore
hanging around in sub-directories and I prefer to manage a unique, top-level .gitignore
. To do so you could simply append the found files to your .gitignore
. Supposing that /public/static/
is your project/git home I would use something like:
我相信最简单的解决方案是使用find
. 我不喜欢.gitignore
在子目录中有多个闲逛,我更喜欢管理一个独特的顶级.gitignore
. 为此,您只需将找到的文件附加到您的.gitignore
. 假设那/public/static/
是你的项目/git home,我会使用类似的东西:
find . -type f -name *.js | cut -c 3- >> .gitignore
I found that cutting out the ./
at the beginning is often necessary for git to understand which files to avoid. Therefore the cut -c 3-
.
我发现./
在开始时删除git 通常需要了解要避免哪些文件。因此cut -c 3-
.