Git忽略BOM(防止git diff显示字节顺序标记更改) [英] Git ignore BOM (prevent git diff from showing byte order mark changes)
问题描述
我希望git diff
不显示 BOM 更改.
此类更改通常在差异中显示为<feff>
I want git diff
to not show BOM changes.
Such changes typically show up as <feff>
in the diff:
-<feff>/*^M
+/*^M
如何使git diff
表现为这种方式?
最好带有命令行参数.
How can I make git diff
to behave this way?
Preferably with a command-line parameter.
git --ignore-all-space
(又名git -w
)不能解决问题.
如果重要的话,我使用的是Mac OS X.
git --ignore-all-space
(aka git -w
) does not do the trick.
I am on Mac OS X if that matters.
推荐答案
使用Git属性过滤器
一种可能的解决方案是创建BOM表过滤器,例如:
One possible solution would be to create a BOM filter such as:
#!/bin/bash
sed '1s/^\xEF\xBB\xBF//' "$1"
将其存储在路径中的某个位置(例如removebom
)并使其可执行.
store it somewhere in your path (as i.e. removebom
) and make it executable.
然后,您需要通过运行以下命令在Git中配置过滤器:
Then you need to configure the filter in Git by running:
$ git config diff.removebom.textconv removebom
并向存储库中添加您感兴趣的文件(即cpp)的属性:
and adding an attribute for files you are interested in (i.e. cpp) to your repository:
*.cpp diff=removebom
作为.gitattributes
文件.
有关该主题的更多信息,请访问:
More on the topic can be found on:
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#_diffing_binary_files
- https://unix .stackexchange.com/questions/381230/如何从i-utf-8文件中删除该宝印
- https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#_diffing_binary_files
- https://unix.stackexchange.com/questions/381230/how-can-i-remove-the-bom-from-a-utf-8-file
或一次删除BOM表
...,因为希望您的项目中的BOM可以一劳永逸地删除:)
... because, hopefully, the BOM may be removed once and for all in your project :)
这篇关于Git忽略BOM(防止git diff显示字节顺序标记更改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!