如何在 Git 中忽略文件扩展名并忽略大小写?

50
我想告诉git忽略例如jpg文件,而不考虑扩展名是如何编写的。 例如: *.jpg 应该被忽略, 包括.jpg,.JPG.和.Jpg等。 那么,在不完全忽略大小写的情况下,这种操作是否可行呢?

2
你想让 Git 在不区分大小写的文件系统上“工作”,还是仅用于忽略目的? - CB Bailey
2个回答

76

Git ignore可以识别glob模式,因此您可以使用此功能。

*.[jJ][pP][gG]

20

你可以告诉git在大多数情况下忽略大小写,包括在你的.gitignore文件中。你所需要做的只是:

git config core.ignorecase true

从实际角度考虑,可能会涉及到权衡取舍。根据 git-config(1) 手册指南:

   core.ignorecase
       If true, this option enables various workarounds to enable git to
       work better on filesystems that are not case sensitive, like FAT.
       For example, if a directory listing finds "makefile" when git
       expects "Makefile", git will assume it is really the same file, and
       continue to remember it as "Makefile".

       The default is false, except git-clone(1) or git-init(1) will probe
       and set core.ignorecase true if appropriate when the repository is
       created.

因此,如果您使用大小写敏感的文件系统,则可能会遇到问题。您的结果可能会有所不同。


7
我不建议这样做。首先,正如你所说,这不仅会使.gitconfig大小写不敏感,而且会影响Git的所有部分。其次,配置文件是针对每个用户或每个克隆的东西,因此它不会应用于与您共享.gitconfig的其他共同开发人员。 - Matthieu Moy
2
我同意@MatthieuMoy的观点。你绝对、肯定会后悔的,尤其是当你用数个小时去调试为什么你的文件丢失了或者为什么所有东西都神秘地崩溃之后。如果这个功能开启着,请将它关闭。 - 0942v8653
我正在使用Win10,它似乎是不区分大小写的。我无法记住在我的存储库中设置core.ignorecase。我该如何检查这个设置? - SL5net
正如@MatthieuMoy所指出的那样,这通常是不安全的,但仅适用于具有不区分大小写文件系统(例如Windows)的情况。 - elonderin
NTFS是区分大小写的,而Windows则不是。这是一个微妙的差别。此选项适用于FAT16,它既不区分大小写也不保留大小写。在我看来,这是错误的方法。 - CervEd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接