Last Updated: February 25, 2016
·
6.212K
· mdahlstrand

Git diff says all lines changed?

Have you ever changed a couple of lines in a text file, done a "git diff" and been confused with why git's telling you that all lines in the file changed?

I have, and today I decided to understand and fix the problem, once and for all. So I opened up the two versions of the file in 0xED - a simple HEX editor - and took a look at what actually differed between them.

Here's a screenshot of what I saw:
Picture

I guess you spotted it instantly; the old file has an additional three leading bytes, EF BB BF, the good old BOM character. It's this character that confuses git and tricks it into thinking the full file changed, and not just the lines that actually changed.

How to fix it? Just add the BOM character back in to the updated file and you're all done, git will now diff correctly! To insert the BOM character in 0xED, just put your cursor by the first character, type "EFBBBF" and hit save!

Of course, better would be to agree with the other developers on the project not to use the BOM character at all.

TL;DR: Add the BOM character back in and git will recognise changes correctly.

2 Responses
Add your response

seems like a hacky way to fix it.. if i have a project with a lot of files this would take forever

over 1 year ago ·

@aaron126 This wouldn't be something you'd do for all your files, but sometimes strange things cab happen to a file, and then it's good to know that you can edit it on a much "rawer" level than your normal editor can.

over 1 year ago ·