Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore trailing whitespace errors with git stash

Tags:

I have an unusual problem with git. I've pulled from a repo I haven't used in awhile, and for simplicity let's say I have two branches, develop and master. I was already in develop, and checked out master. I made no changes, I was just looking at the files. When I went to checkout develop, I could not because README.md was modified. Git diff showed the whole file was changed, with identical content, so I thought that was some sort of whitespace error.

I did git checkout -- README.md, and it still appeared modified. So then I tried git stash -p (so I could just stash that file). That gave me:

warning: 3 lines add whitespace errors.

It went on to say:

warning: squelched 55 whitespace errors
warning: 60 lines add whitespace errors.

And the file is still modified.

Is there any way to get around these errors? What could be causing it?

like image 958
Katharine Osborne Avatar asked Jan 26 '17 17:01

Katharine Osborne


1 Answers

As you mentioned above, the issue is named trailing whitespace.
Git has several ways to handle whitespace. You can read about it here:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_formatting_and_whitespace

You can try and set the configuration values:

$ git config --global core.whitespace \
                      trailing-space, \
                      -space-before-tab, \
                      indent-with-non-tab, \
                      tab-in-indent, \
                      cr-at-eol

When git pops up content from a stash, it's adding it back to the working directory using the apply command, so you can disable the apply warning with this configuration

git config --global apply.whitespace nowarn
or 
git config --global core.whitespace fix

#core.whitespace A comma separated list of common whitespace problems to notice.
git diff will use color.diff.whitespace to highlight them,
and git apply --whitespace=error will consider them as errors.

You can prefix - to disable any of them (e.g. -trailing-space):

###blank-at-eol treats trailing whitespaces at the end of the line as an error (enabled by default).

###space-before-tab treats a space character that appears immediately before a tab character in the initial indent part of the line as an error (enabled by default).

###indent-with-non-tab treats a line that is indented with space characters instead of the equivalent tabs as an error (not enabled by default).

###tab-in-indent treats a tab character in the initial indent part of the line as an error (not enabled by default).

###blank-at-eof treats blank lines added at the end of file as an error (enabled by default).

###trailing-space is a short-hand to cover both blank-at-eol and blank-at-eof.

###cr-at-eol treats a carriage-return at the end of line as part of the line terminator, i.e. with it, trailing-space does not trigger if the character before such a carriage-return is not a whitespace (not enabled by default).

###tabwidth=<n> tells how many character positions a tab occupies; this is relevant for indent-with-non-tab and when Git fixes tab-in-indent errors. The default tab width is 8. Allowed values are 1 to 63.

like image 183
CodeWizard Avatar answered Sep 25 '22 10:09

CodeWizard