Git Tip of the Day – check for whitespace errors in diff

When creating and applying patches, git often complains about whitespace errors. Whitespace changes are generally not desirable, because they make the diff longer and diverts your focus from (very probably) more important changes. From this reason git tries to provide warnings for whitespace changes that might have not been needed. This includes trailing whitespace, spaces before tabs in indentation and empty newlines at the end of a file.

If you execute

$ git diff

and have colored output enabled, git marks these changes with light red background.

But if you don’t have colored output enabled, or maybe your patch is too long and you don’t want to scroll through it, there’s an easier way to check for whitespace errors. Just execute

$ git diff --check

You can then receive output like this:

$ git diff --check
README:7: trailing whitespace.
+or ask in #fedora-qa on the freenode IRC network.  
README:13: space before tab in indent.
+       'autoqa' is the main binary that kicks off tests
TODO:14: new blank line at EOF.

And now you know what to fix.

If you’re working on a patch for some project and you’re about to send it, use this simple command to check for whitespace errors. It will then save a little work for everyone who’s going to read the diff (or the commit log).

Happy gitting!


Flattr this

Advertisements
Git Tip of the Day – check for whitespace errors in diff

One thought on “Git Tip of the Day – check for whitespace errors in diff

Leave a Reply (Markdown syntax supported)

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s