How to view whitespace issues?

git config --global core.whitespace OPTIONS

Git comes with a built in whitespace issue detector, which can detect 6 common whitespace issues, which are:

  1. blank-at-eol looks for spaces at the end of a line. This option is enabled by default.
  2. blank-at-eof notices blank lines at the end of a file. This option is enabled by default.
  3. space-before-tab looks for spaces before tabs at the beginning of a line. This option is enabled by default.
  4. indent-with-non-tab looks for lines that begin with spaces instead of tabs. This option is disabled by default.
  5. tab-in-indent watches for tabs in the indentation portion of a line. This option is disabled by default.
  6. cr-at-eol tells Git that carriage returns at the end of lines are OK. This option is disabled by default.

You can set the selected options with git config --global core.whitespace option1,option2 and disable an option by prepending a - in front of it, or use the default value by leaving it out of the setting string entirely.

Once the whitespace options are set, git diff will detect and color them.

Layer 1
Terminal Example
git config --global core.whitespace trailing-space,-space-before-tab,indent-with-non-tab,cr-at-eol

git diff
diff --git a/articles_controller.rb b/articles_controller.rb
index ea67e94..99f04b3 100644
--- a/articles_controller.rb
+++ b/articles_controller.rb
@@ -4,7 +4,7 @@ class ArticlesController < ApplicationController
   end

   def show
-    @article = Article.find(params[:id])
+    @article = Article.find(params[:id])  
   end

   def new

See git config to learn more about it.

Last modified on December 18, 2021.

You might also like