是否有一种方法可以忽略目录中同一类型的所有文件?

**显然对git毫无意义,所以这行不通:

/public/static/**/*.js

其思想是匹配任意嵌套文件夹。


当前回答

正如其他答案已经说过的,像**/*.exe这样的模式将忽略存储库中任何位置的所有.exe文件。

然而,许多人(像我一样)在排除故障时必然会遇到这个问题…为什么这行不通。

所以,命令git状态或你的IDE可能会告诉你xyz.exe即将提交,你可能会撞到墙上试图弄清楚为什么会这样,因为你确实有**/*.exe在你的。gitignore文件,既然这个问答确认这实际上是正确的魔法咒语,这应该是防止这种情况。但事实并非如此。

您处于这种荒谬情况的实际原因是您(或者更有可能是您的IDE自动为您提供的)已经暂放了该文件。

解决办法是先取消它,然后它就会被忽略。

<rant> The "staging area" is an entirely useless feature of git, which is a major source of pain without any benefit worth speaking of; however, it is central in how git works, so you have to always be aware of it. Different IDEs take different approaches towards it: Visual Studio forces you to deal with staging, and that's one of the reasons why working with git in Visual Studio is a major pain, and a very poor user experience. IntelliJ IDEA handles staging behind the scenes for you, (as it should,) so you do not have to be bothered by it, or even be aware of its existence, and that's one of the reasons why working with git in IntelliJ IDEA is a pleasure. Unfortunately, nothing works perfectly, so inevitably, every once in a while there will be pain, even with IntelliJ IDEA. </rant>

其他回答

从来没有尝试过,但是git help ignore建议如果你把.gitignore和*.js放在/public/static中,它会做你想做的事情。

注意:一定要看看下面Joeys的回答:如果你想忽略特定子目录中的文件,那么本地的.gitignore是正确的解决方案(局部性很好)。但是,如果您需要将相同的模式应用于整个回购,那么**解决方案会更好。

正如其他答案已经说过的,像**/*.exe这样的模式将忽略存储库中任何位置的所有.exe文件。

然而,许多人(像我一样)在排除故障时必然会遇到这个问题…为什么这行不通。

所以,命令git状态或你的IDE可能会告诉你xyz.exe即将提交,你可能会撞到墙上试图弄清楚为什么会这样,因为你确实有**/*.exe在你的。gitignore文件,既然这个问答确认这实际上是正确的魔法咒语,这应该是防止这种情况。但事实并非如此。

您处于这种荒谬情况的实际原因是您(或者更有可能是您的IDE自动为您提供的)已经暂放了该文件。

解决办法是先取消它,然后它就会被忽略。

<rant> The "staging area" is an entirely useless feature of git, which is a major source of pain without any benefit worth speaking of; however, it is central in how git works, so you have to always be aware of it. Different IDEs take different approaches towards it: Visual Studio forces you to deal with staging, and that's one of the reasons why working with git in Visual Studio is a major pain, and a very poor user experience. IntelliJ IDEA handles staging behind the scenes for you, (as it should,) so you do not have to be bothered by it, or even be aware of its existence, and that's one of the reasons why working with git in IntelliJ IDEA is a pleasure. Unfortunately, nothing works perfectly, so inevitably, every once in a while there will be pain, even with IntelliJ IDEA. </rant>

根据文档,从1.8.2.1版本开始,git似乎支持**语法。

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.

我试着在我的vscode中打开。gitignore文件,windows 10。在这里,您可以看到一些先前添加的忽略文件(如果有的话)。

要创建一个新规则来忽略一个扩展名为(.js)的文件,可以像这样追加文件的扩展名:

*.js

这将忽略git存储库中的所有.js文件。

要从特定目录中排除特定类型的文件,你可以添加这个:

**/foo/*.js

这将忽略/foo/目录下的所有.js文件。

有关详细的学习,您可以访问:about git-ignore

我认为最简单的解决方案是使用find。我不喜欢有多个。gitignore挂在子目录,我更喜欢管理一个唯一的,顶级的。gitignore。为此,您只需将找到的文件附加到.gitignore。假设/public/static/是你的项目/git home,我将使用如下代码:

find . -type f -name *.js | cut -c 3- >> .gitignore

我发现,为了让git了解应该避免哪些文件,在开头去掉./通常是必要的。因此cut -c 3-。