Git忽略BOM(防止git diff显示字节顺序标记更改) [英] Git ignore BOM (prevent git diff from showing byte order mark changes)

查看:203
本文介绍了Git忽略BOM(防止git diff显示字节顺序标记更改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望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/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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆