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

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

/public/static/**/*.js

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


当前回答

在其他答案之上还有一些额外的建议(如果你幸运的话,问题是由其他原因引起的,这些建议可能会起作用):

确保忽略代码中大于100mb的文件 只需重新启动git工作流。删除本地的.git文件夹和git init,然后重试推到github。

其他回答

根据文档,从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.

要忽略未跟踪的文件,只需转到.git/info/exclude。Exclude是一个包含被忽略扩展名或文件列表的文件。

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

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

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

更新:看看@Joey的回答:Git现在支持模式中的**语法。这两种方法都可以正常工作。


gitignore(5)手册页声明:

从与路径相同目录的.gitignore文件中读取的模式,或在任何父目录中读取的模式,高级文件中的模式(直到工作树的顶层)将被低级别文件中的模式覆盖,直到包含该文件的目录。

这意味着在repo的任何给定目录中的.gitignore文件中的模式将影响该目录和所有子目录。

您提供的模式

/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 / won't work either, matching paths like public/static/foo.js and foo/public/static/bar.js. 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 Hoer for pointing this out).

正如@ptyx所建议的,您需要做的是创建文件<repo>/public/static/。Gitignore,包括这个图案:

*.js

没有前导/,所以它将匹配路径的任何部分,并且该模式将只应用于/public/static目录及其子目录中的文件。

在其他答案之上还有一些额外的建议(如果你幸运的话,问题是由其他原因引起的,这些建议可能会起作用):

确保忽略代码中大于100mb的文件 只需重新启动git工作流。删除本地的.git文件夹和git init,然后重试推到github。