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

1

13

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.

Wellesz answered 1/12, 2014 at 7:58 Comment(3)
Would git diff -w (--ignore-all-space) work better?Intervale
@VonC: It does not do the trick unfortunately.Wellesz
Ok. Then I don't know. Git doesn't seem to touch or ignore BOM (as seen in https://mcmap.net/q/908389/-checkin-changes-to-utf8-bom-using-git)Intervale
B
7

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:

Or remove the BOM once for all

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

Burnisher answered 11/9, 2017 at 8:44 Comment(1)
I see that you might be using a UTF-16 BOM, then '1s/^\xFE\xFF//' might be the sed pattern you want... But in case of UTF-16, the BOM might be actually required and you are converting whole file to UTF-8; just beware of that.Burnisher

© 2022 - 2024 — McMap. All rights reserved.