Search code examples
gitwarningswhitespacebinaryfilesgit-diff

Git binary patch as text applying has many whitespace errors


When using git diff -a for text, for example git diff --no-index -a dir1 dir2 > dir.patch, when I apply the patch with git apply I get many whitespace warnings about trailing whitespace and squelching thousands of whitespace errors. Should I be worried about these? diff shows the patched directory is byte identical to its expected patched state, so nothing got corrupted. (I am aware --binary takes care of these errors.)


Solution

  • Should I be worried about [Git's whitespace-error warnings]?

    Only if you agree with Git's rather opinionated stance on whitespace. That is, by default, Git thinks that it is bad to have a line in a file end with whitespace. If we let the symbol $ represent the end of a line, then:

    hello world$
    

    is OK, but:

    hello world $
    

    is not. While most Git commands say nothing about this, git diff highlights the "bad" whitespace, and git apply complains about the "bad" whitespace.

    If you don't agree with Git (and/or if such whitespace is important), you can configure Git, or use command line arguments to git apply, to change what it complains about or to disable complaints. If you think Git is right on, you can turn git apply's complaints into outright errors, rather than warnings, or tell Git to fix such errors (e.g., to strip the trailing blank from the above line).

    For a complete list of whitespace options, see the core.whitespace settings described in the git-config documentation.