Pesky Whitespace and Git

It’s been a while since I’ve written anything directly related to programming on my blog.  I thought it might be time to get back into the swing of things and start putting some more of the things that I run across during my days.

Recently I’ve expanded my knowledge of git and thought that I might share my newfound knowledge with y’all.


Scott Hanselman wrote about the difference of line endings a while back in his blog post titled, You’re just another carriage return line feed in the wall.  In this post his main complaint was the line ending difference creating a wall of pink followed by a wall of green.  The effect was that it completely obfuscated the actual differences in the file by making it appear as if you’ve deleted the entire file and then recreated it.

Wall of Pink

This image was produced by Scott Hanselman and is from the blog post listed above.

As it turns out, the amount of whitespace in your lines can produce the same effect.  I’ve noticed that Visual Studio and ReSharper are very glad to add lots of extra whitespace to help make your code more readable.  To help you the developer eliminate this when performing a comparisons, git has created a command line option to eliminate this from the diff calculations.

  • –ignore-space-at-eol
    This option will ignore the changes in the whitespace at the end of the line.  This is a very useful option because it is very specific with what you want to eliminate.
  • –ignore-all-space
    Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none.  This brush is a bit too broad for my tastes. Not that I’ve never used it, but I think that sometimes it can hide things from view that I actually want to see.
  • –ignore-blank-lines
    This will ignore all changes where the line is blank.
  • –ignore-space-change
    Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent. Again it’s a bit of a broad brush, but it can be very useful.

Eric, that’s great if your a command line guru, but what about us poor schleps that look at our diffs in gitk?

Not a problem!  Gitk is kind enough to provide us with a check box that will help remove some of those unwanted lines in the diff.

gitk diff checkbox

Granted, it does not give you the granular control that you maintain in the bash shell, but it can reduce the number of lines you have to look at.

The bottom line is; I think that the problem of whitespace comparisons will continue to plaque the developer for some time to come, but with a little knowledge and the tools at hand, you can simplify your life.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s