Git ignore BOM (prevent git diff from showing byte order mark changes)

霸气de小男生 提交于 2019-11-27 07:41:06

问题


I want git diff to not show BOM changes.
Such changes typically show up as <feff> in the diff:

-<feff>/*^M
+/*^M

How can I make git diff to behave this way?
Preferably with a command-line parameter.

git --ignore-all-space (aka git -w) does not do the trick. I am on Mac OS X if that matters.


回答1:


Use Git Attributes filter

One possible solution would be to create a BOM filter such as:

#!/bin/bash
sed '1s/^\xEF\xBB\xBF//' "$1"

store it somewhere in your path (as i.e. removebom) and make it executable.

Then you need to configure the filter in Git by running:

$ git config diff.removebom.textconv removebom

and adding an attribute for files you are interested in (i.e. cpp) to your repository:

*.cpp diff=removebom

as a .gitattributes file.

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

Or remove the BOM once for all

... because, hopefully, the BOM may be removed once and for all in your project :)



来源:https://stackoverflow.com/questions/27223985/git-ignore-bom-prevent-git-diff-from-showing-byte-order-mark-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!